integer displayed as .0 float in Lua-5.3
Dear list, I have the following sample \starttext \startTEXpage[offset=1em] Pages: \ctxlua{context([[\lastpage]])}. Next page: \ctxlua{context([[\lastpage]] + 1)}. (Lua \luaversion) \stopTEXpage \stoptext With Lua-5.2 (ConTeXt with JIT), I get the expected results. But with Lua-5.3 (standard ConTeXt), I get 2.0 as the value for the next page. I’m afraid that there is a bug here. I was told that the integer type has been introduced in Lua-5.3. I had experienced the same issue with the handlecsv module. In my very limited understanding of the issue, integers are displayed as .0 floats. I wonder which would be the way to fix it. Is this a real bug or am I missing something? Many thanks for your help, Pablo -- http://www.ousia.tk
Use tonumber. %%%%%% \starttext \cldcontext{\lastpage + 1} \cldcontext{"\lastpage" + 1} \cldcontext{tonumber("\lastpage") + 1} \stoptext %%%%%% Wolfgang Pablo Rodriguez schrieb am 09.08.18 um 20:02:
Dear list,
I have the following sample
\starttext \startTEXpage[offset=1em] Pages: \ctxlua{context([[\lastpage]])}.
Next page: \ctxlua{context([[\lastpage]] + 1)}.
(Lua \luaversion) \stopTEXpage \stoptext
With Lua-5.2 (ConTeXt with JIT), I get the expected results. But with Lua-5.3 (standard ConTeXt), I get 2.0 as the value for the next page.
I’m afraid that there is a bug here. I was told that the integer type has been introduced in Lua-5.3.
I had experienced the same issue with the handlecsv module. In my very limited understanding of the issue, integers are displayed as .0 floats. I wonder which would be the way to fix it.
Is this a real bug or am I missing something?
Many thanks for your help,
Pablo
On 08/09/2018 08:12 PM, Wolfgang Schuster wrote:
Use tonumber.
Many thanks for your reply, Wolfgang. The issue is in Lua itself: print(5+"5") In versions prior to 5.3, result is "10".
From version 5.3, result is "10.0",
I would say this might be a bug. According to the “Lua 5.3 Reference Manual" (https://www.lua.org/manual/5.3/manual.html#3.1): A numeric constant with a radix point or an exponent denotes a float; otherwise, if its value fits in an integer, it denotes an integer. Well, "10.0" contains the radix point, but with no arithmetical relevance. My background is in humanities and I don’t understand the exponent for being a float ("10²" contains an exponent [https://www.m-w.com/dictionary/exponent], but I would say is an integer in all possible worlds [or all the worlds I know ]). Could anyone explain me what am I missing here or confirm whether this is a bug? Many thanks for your help, Pablo -- http://www.ousia.tk
The lua manual also states that one should NOT rely on the implicit conversion of a string to its numerical value, and suggests the systematic use of tonumber().
Alan
On Thu, 9 Aug 2018 21:20:54 +0200
Pablo Rodriguez
On 08/09/2018 08:12 PM, Wolfgang Schuster wrote:
Use tonumber.
Many thanks for your reply, Wolfgang.
The issue is in Lua itself:
print(5+"5")
In versions prior to 5.3, result is "10".
From version 5.3, result is "10.0",
I would say this might be a bug. According to the “Lua 5.3 Reference Manual" (https://www.lua.org/manual/5.3/manual.html#3.1):
A numeric constant with a radix point or an exponent denotes a float; otherwise, if its value fits in an integer, it denotes an integer.
Well, "10.0" contains the radix point, but with no arithmetical relevance.
My background is in humanities and I don’t understand the exponent for being a float ("10²" contains an exponent [https://www.m-w.com/dictionary/exponent], but I would say is an integer in all possible worlds [or all the worlds I know ]).
Could anyone explain me what am I missing here or confirm whether this is a bug?
Many thanks for your help,
Pablo
On 08/09/2018 09:34 PM, Alan Braslau wrote:
The lua manual also states that one should NOT rely on the implicit conversion of a string to its numerical value, and suggests the systematic use of tonumber().
Many thanks for your reply, Alan. I guess that backwards compatibility should be important here, but I hope there are stronger reasons for breaking it. Many thanks for your help, Pablo -- http://www.ousia.tk
On Thu, 9 Aug 2018 22:00:48 +0200
Pablo Rodriguez
I guess that backwards compatibility should be important here, but I hope there are stronger reasons for breaking it.
tonumber() provides backwards compatibility. And the recommendation to use it was in the third edition (of the lua book), if not before, referring to 5.2. Alan
On 08/09/2018 10:16 PM, Alan Braslau wrote:
On Thu, 9 Aug 2018 22:00:48 +0200 Pablo Rodriguez
wrote: I guess that backwards compatibility should be important here, but I hope there are stronger reasons for breaking it.
tonumber() provides backwards compatibility. And the recommendation to use it was in the third edition (of the lua book), if not before, referring to 5.2.
I realize that the fault is mine, but with backwards compatibility I meant that the same operation (10 + "10") gives different results with different Lua versions. Pablo -- http://www.ousia.tk
On Sat, 11 Aug 2018 11:29:41 +0200
Pablo Rodriguez
On 08/09/2018 10:16 PM, Alan Braslau wrote:
On Thu, 9 Aug 2018 22:00:48 +0200 Pablo Rodriguez
wrote: I guess that backwards compatibility should be important here, but I hope there are stronger reasons for breaking it.
tonumber() provides backwards compatibility. And the recommendation to use it was in the third edition (of the lua book), if not before, referring to 5.2.
I realize that the fault is mine, but with backwards compatibility I meant that the same operation (10 + "10") gives different results with different Lua versions.
Pablo
"Bad" coding can be expected to give unpredictable results. The creators of lua thought that automatic string conversion would be useful (and I believe that they got this feature from "awk", which itself may have gotten this from some previous scripting language). But they learned that automatic string conversion often creates more problems than it solves, so they recommend using explicit conversion through tonumber(). Lua is a very beautiful and powerful language, and it gives me surprises all of the time. Much of what Hans does I like to call "lua magic" meaning that I sort-of understand it but am impressed each time. Does this make lua a poor (problematic) scripting language? I think like all powerful tools, one must take care in their use. Alan
On Sat, Aug 11, 2018 at 11:29:41AM +0200, Pablo Rodriguez wrote:
I realize that the fault is mine, but with backwards compatibility I meant that the same operation (10 + "10") gives different results with different Lua versions.
It doesn’t, it returns 10 in both cases. The difference is in the behaviour of the print function. Try print(10.0) in Lua 5.2 and 5.3: the former prints 10, the latter 10.0. That is consistent with the part of the specification you quote in another email: in Lua >= 5.3, a number with a decimal point in it is always interpreted as a float. If the number to be printed is the result of some computation, however (instead of lexical analysis), a choice needs to be made: that is where the change occurred, since in Lua 5.2 print displayed the shortest possible representation, while in Lua 5.3 it chose to interpret the number as a float. It’s a reasonable choice and breaking compatibility for an elementary function such a print is to be expected when such a change in the language occurred; even desirable, in my opinion. Best, Arthur
On 8/9/2018 10:00 PM, Pablo Rodriguez wrote:
On 08/09/2018 09:34 PM, Alan Braslau wrote:
The lua manual also states that one should NOT rely on the implicit conversion of a string to its numerical value, and suggests the systematic use of tonumber().
Many thanks for your reply, Alan.
I guess that backwards compatibility should be important here, but I hope there are stronger reasons for breaking it.
Many thanks for your help, In addition to what others already explained, you should not depend on features that are implementation dependent or might disappear. That said ...
\startTEXpage[offset=1em] \ctxlua{context([[\lastpage]])}. \stopTEXpage Why do you convert \lastpage to a string using [[\lastpage]] which will use the string to number conversion. In fact this automatic cast from string to number in an addition might disappear from the language in the future so don't rely on that. You can just do: \startTEXpage[offset=1em] \ctxlua{context(\lastpage + 1)}. \stopTEXpage Or when in dount about what \lastpage is \startTEXpage[offset=1em] \ctxlua{context(\number\lastpage + 1)}. \stopTEXpage or if you really want to [[ ]] \startTEXpage[offset=1em] \the\numexpr\ctxlua{context([[lastpage]] + 1)}\relax . \stopTEXpage endless possibities here. Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
On 08/09/2018 10:25 PM, Hans Hagen wrote:
In addition to what others already explained, you should not depend on features that are implementation dependent or might disappear.
Many thanks for your detailed explanation, Hans. I had no idea that the automatic conversion from string to number where so dependent on the Lua version.
That said ...
\startTEXpage[offset=1em] \ctxlua{context([[\lastpage]])}. \stopTEXpage
Why do you convert \lastpage to a string using [[\lastpage]] which will use the string to number conversion?
Well, I must confess that this is a new knowledge to me. I thought that "[[ ]]" was the form to escape what I think it is the escape character in Lua. How I came to that assumption? Probably because I tried to add a raw command to \ctxlua or inside a \startlua...\stoplua.
You can just do:
\startTEXpage[offset=1em] \ctxlua{context(\lastpage + 1)}. \stopTEXpage
From this sample, I understand that "\" doesn’t need to be escaped inside context(), does it?
or if you really want to [[ ]]
\startTEXpage[offset=1em] \the\numexpr\ctxlua{context([[lastpage]] + 1)}\relax . \stopTEXpage
endless possibities here.
I don’t need "[[ ]]", I only thought I needed them. Many thanks for your help, Pablo -- http://www.ousia.tk
On 9 Aug 2018, at 21:20, Pablo Rodriguez
wrote: A numeric constant with a radix point or an exponent denotes a float; otherwise, if its value fits in an integer, it denotes an integer.
Well, "10.0" contains the radix point, but with no arithmetical relevance.
My background is in humanities and I don’t understand the exponent for being a float ("10²" contains an exponent [https://www.m-w.com/dictionary/exponent], but I would say is an integer in all possible worlds [or all the worlds I know ]).
Could anyone explain me what am I missing here or confirm whether this is a bug?
It may refer to a floating point number syntax as in C++ [1], where the three cases top there say that there must be a point '.' preceded or followed by at least one digit, or at least one digit followed by an exponent starting with 'e' or 'E'. There are also some examples on that page. Also note that there are no negative numbers, which is because a leading + or - are treated as operators that apply not only to numbers. 1. https://en.cppreference.com/w/cpp/language/floating_literal
On 08/09/2018 10:20 PM, Hans Åberg wrote:
On 9 Aug 2018, at 21:20, Pablo Rodriguez wrote: [...] My background is in humanities and I don’t understand the exponent for being a float ("10²" contains an exponent [https://www.m-w.com/dictionary/exponent], but I would say is an integer in all possible worlds [or all the worlds I know ]).
It may refer to a floating point number syntax as in C++ [1], where the three cases top there say that there must be a point '.' preceded or followed by at least one digit, or at least one digit followed by an exponent starting with 'e' or 'E'.
Many thanks for your explanation, Hans. I thought there should be some kind of restriction when referring to the exponent, but this is why technical explanations aren’t always very clear. I mean, they have too many restrictions attached to them. Pablo -- http://www.ousia.tk
On 11 Aug 2018, at 11:33, Pablo Rodriguez
wrote: On 08/09/2018 10:20 PM, Hans Åberg wrote:
On 9 Aug 2018, at 21:20, Pablo Rodriguez wrote: [...] My background is in humanities and I don’t understand the exponent for being a float ("10²" contains an exponent [https://www.m-w.com/dictionary/exponent], but I would say is an integer in all possible worlds [or all the worlds I know ]).
It may refer to a floating point number syntax as in C++ [1], where the three cases top there say that there must be a point '.' preceded or followed by at least one digit, or at least one digit followed by an exponent starting with 'e' or 'E'.
Many thanks for your explanation, Hans.
You are welcome.
I thought there should be some kind of restriction when referring to the exponent, but this is why technical explanations aren’t always very clear. I mean, they have too many restrictions attached to them.
The C++ description allows for fast reading with just one lookahead character at a time, which is was important in C when it appeared in 1972 on the not so powerful computers of the day. Then roundoff may make floating point numbers look like integers even they are not. For example, 1.0 could be 0.99999999 or 1.0000001. So don't check floating numbers for equality, instead use say abs(x - y) < a for some small number a. When adding exact integers and inexact floating point numbers, it is easiest to always make that inexact numbers, as exactness cannot always be guaranteed.
On 8/11/2018 11:33 AM, Pablo Rodriguez wrote:
On 08/09/2018 10:20 PM, Hans Åberg wrote:
On 9 Aug 2018, at 21:20, Pablo Rodriguez wrote: [...] My background is in humanities and I don’t understand the exponent for being a float ("10²" contains an exponent [https://www.m-w.com/dictionary/exponent], but I would say is an integer in all possible worlds [or all the worlds I know ]).
It may refer to a floating point number syntax as in C++ [1], where the three cases top there say that there must be a point '.' preceded or followed by at least one digit, or at least one digit followed by an exponent starting with 'e' or 'E'.
Many thanks for your explanation, Hans.
I thought there should be some kind of restriction when referring to the exponent, but this is why technical explanations aren’t always very clear. I mean, they have too many restrictions attached to them. small numbers can get expresses in nEm notation instead of 0.00000... which can also bite you (in context we intercept this when needed)
Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
participants (6)
-
Alan Braslau
-
Arthur Reutenauer
-
Hans Hagen
-
Hans Åberg
-
Pablo Rodriguez
-
Wolfgang Schuster