String to number conversions

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

String to number conversions

Robert Virding-2
Where is it possible to read exactly how and when strings are converted to numbers?in Lua 5.3. So in some case a string integer becomes a float:

> "123" + 1
124.0
> math.abs("123")
123.0

while in some other cases no conversion is done:

> math.max("123", -2.3, 1)
attempt to compare string with number
stack traceback:
       [C]: in function 'math.max'
       stdin:1: in main chunk
       [C]: in ?

and this in the math module which sometimes does conversion?

Robert

Reply | Threaded
Open this post in threaded view
|

Re: String to number conversions

Egor Skriptunoff-2
On Thu, Nov 28, 2019 at 1:28 PM Robert Virding wrote:
So in some case a string integer becomes a float:

> "123" + 1
124.0
> math.abs("123")
123.0

while in some other cases no conversion is done:

> math.max("123", -2.3, 1)
attempt to compare string with number



BTW, I've asked similar question about S2N-conversion anarchy in Lua
See question #5 in my post http://lua-users.org/lists/lua-l/2019-02/msg00049.html

Reply | Threaded
Open this post in threaded view
|

Re: String to number conversions

Roberto Ierusalimschy
> On Thu, Nov 28, 2019 at 1:28 PM Robert Virding wrote:
>
> > So in some case a string integer becomes a float:
> >
> > > "123" + 1
> > 124.0
> > > math.abs("123")
> > 123.0
> >
> > while in some other cases no conversion is done:
> >
> > > math.max("123", -2.3, 1)
> > attempt to compare string with number
> >
> >
>
> BTW, I've asked similar question about S2N-conversion anarchy in Lua
> See question #5 in my post
> http://lua-users.org/lists/lua-l/2019-02/msg00049.html

As you said in your message, "using [S2N-coercion] is considered a bad
practice". Currently, they exist in Lua for compatibility only. The
trend is to remove them in the future. (Lua 5.4 already removed them
from bitwise operators, which themselves are somewhat new, and it
moved the arithmetic coercions from the core to a library.)

So, the rule is: when in doubt, don't use it. We will try to convey
that in the manual.

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: String to number conversions

D Burgess-4
Does that mean we get a "tointeger()" function in Lua?

Reply | Threaded
Open this post in threaded view
|

Re: String to number conversions

nobody
On 29/11/2019 00.20, D Burgess wrote:
> Does that mean we get a "tointeger()" function in Lua?

There already is `math.tointeger`, which is happy to eat strings (both
in 5.3 and the current 5.4 development state).

Lua 5.4.0(git-6f1c033d)  Copyright (C) 1994-2019 Lua.org, PUC-Rio
> math.tointeger( 1.0 )
1
> math.tointeger( "1.0" )
1
> math.tointeger( "0x1234" )
4660
> math.tointeger( "0x1234p-2" )
1165
> math.tointeger( "0x1234p-3" )
nil

Besides, tonumber() picks the right format.  (I.e., if it looks floaty
or is too big to fit in an integer, you'll get a float.  Otherwise,
you'll get an int.)  And on top of that, functions silently coerce
compatible values across subtypes – so even if you get the wrong
subtype, there won't be a problem – which means in practice you can just
use tonumber().

Lua 5.4.0(git-6f1c033d)  Copyright (C) 1994-2019 Lua.org, PUC-Rio
> function test(v) local n = tonumber(v) ; return n, math.type(n) end
> test "9223372036854775807"
9223372036854775807 integer
> test "9223372036854775808"
9.2233720368548e+18 float
> test "1"
1 integer
> test "1.0"
1.0 float
> tonumber "1.0" ~ 3
2
> tonumber "0"
0
> math.cos( tonumber "0" )
1.0
> ("%d"):format( 1.0 )
1
> ("%.2f"):format( 1 )
1.00
> ("%d"):format( 1.2 )
stdin:1: bad argument #1 to 'format' (number has no integer representation)
stack traceback:
        [C]: in function 'string.format'
        stdin:1: in main chunk
        [C]: in ?

-- nobody