Conversion of strings to numbers and numbers to strings.

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

Conversion of strings to numbers and numbers to strings.

David Jones-2
I find that the reference manual for Lua 5.1 is disturbingly vague on
the matter of converting numbers to strings and strings to numbers.  
Pretty much all it says is in section 2.2.1, string to number is done
"following the usual conversion rules", and number to string is done
"in a reasonable format".

I realise that the Lua team cannot reasonably make it absolutely
precise, because even if luaconf.h has not been modified by the user,
the underlying operations depend on the C library.  But I feel that
some more could be said, for example:

Reasonable sized integers will be converted to a string that does not
include a decimal point.

When converting strings to numbers, preceding and trailing whitespace
is stripped.

When converting strings to numbers, an optional minus sign is
acceptable.

Both hexadecimal and decimal (floating point) conversions are possible.

I feel that they would be useful additions to the manual.

By the way, I was surprised by the result of

return '-0x1' + 0

I was even more surprised when it gave a different answer on my PowerPC
OS X machine (where it gives -1) than on an Intel Windows XP machine
(where it gives 4294967295).

David Jones

Reply | Threaded
Open this post in threaded view
|

Re: Conversion of strings to numbers and numbers to strings.

David Jones-2

On May 30, 2006, at 12:04, David Jones wrote:
>
> By the way, I was surprised by the result of
>
> return '-0x1' + 0
>
> I was even more surprised when it gave a different answer on my
> PowerPC OS X machine (where it gives -1) than on an Intel Windows XP
> machine (where it gives 4294967295).

This turns out to be because on OS X strtod handles the conversion of
'-0x1', for which we can thank C99, and on Windows (I assume) strtoul
handles the conversion of '-0x1'.  I find the whole situation rather
repellant.

drj

Reply | Threaded
Open this post in threaded view
|

Re: Conversion of strings to numbers and numbers to strings.

Luiz Henrique de Figueiredo
> > By the way, I was surprised by the result of
> >
> > return '-0x1' + 0

Do no use negative hexadecimal constants. Try this instead.
        return -'0x1' + 0

Perhaps the manual should say "Lua also accepts UNSIGNED integer
hexadecimal constants"... It does sau something like that in the
explanation of tonumber.

--lhf
Reply | Threaded
Open this post in threaded view
|

Re: Conversion of strings to numbers and numbers to strings.

Mike Pall-5-2
In reply to this post by David Jones-2
Hi,

David Jones wrote:
> By the way, I was surprised by the result of
>
> return '-0x1' + 0
>
> I was even more surprised when it gave a different answer on my PowerPC
> OS X machine (where it gives -1) than on an Intel Windows XP machine
> (where it gives 4294967295).

Conversion of negative hex numbers may lead to unpredictable
results. It works in the parser (because it strips off the unary
minus first), but not in implicit coercions or with tonumber():

  http://lua-users.org/lists/lua-l/2006-02/msg00227.html

It's hard to avoid unless your C library is C99 compliant.
Alas, MSVCRT is not and we have 2006 -- complain to M$. ;-)


Summary: Hex numbers only convert well with a C99 compliant libc.
But Lua does not rely on it and has a partial fallback.

The side-effects of the fallback are: Negative hex numbers are
undefined. Positive hex numbers are only defined up to the
minimum precision of an unsigned long and lua_Number (which may
lead to surprises esp. when lua_Number is a float).

Bye,
     Mike
Reply | Threaded
Open this post in threaded view
|

Re: Conversion of strings to numbers and numbers to strings.

Roberto Ierusalimschy
In reply to this post by David Jones-2
> This turns out to be because on OS X strtod handles the conversion of
> '-0x1', for which we can thank C99, and on Windows (I assume) strtoul
> handles the conversion of '-0x1'.  I find the whole situation rather
> repellant.

That is why we were always reluctant to add hexa constants in Lua.
(They entered Lua 5.1 by accident...)

-- Roberto
Reply | Threaded
Open this post in threaded view
|

Re: Conversion of strings to numbers and numbers to strings.

Roberto Ierusalimschy
In reply to this post by David Jones-2
> I find that the reference manual for Lua 5.1 is disturbingly vague on
> the matter of converting numbers to strings and strings to numbers.  
> Pretty much all it says is in section 2.2.1, string to number is done
> "following the usual conversion rules", and number to string is done
> "in a reasonable format".

We explicitly avoid too much restrictions here. If you want anything
specific when converting numbers to strings, you should use
string.format. If you want very specific syntax when converting strings
to numbers, use pattern matching.

-- Roberto
Reply | Threaded
Open this post in threaded view
|

Re: Conversion of strings to numbers and numbers to strings.

Shmuel Zeigerman-2
In reply to this post by Roberto Ierusalimschy
Roberto Ierusalimschy wrote:
>>This turns out to be because on OS X strtod handles the conversion of
>>'-0x1', for which we can thank C99, and on Windows (I assume) strtoul
>>handles the conversion of '-0x1'.  I find the whole situation rather
>>repellant.
>
>
> That is why we were always reluctant to add hexa constants in Lua.
> (They entered Lua 5.1 by accident...)

Please do not remove them!

--
Shmuel