Just curious: Why is tonumber(nil) not 0?

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

Just curious: Why is tonumber(nil) not 0?

Mike Crowe
Hi folks,

I was briefly caught by doing this test:

    if tonumber(something) > 0 then
       ...
    end

with something=nil.

I'm not advocating a change, but what's the thought-process behind this
behavior?  Wouldn't you consider the numeric value of nil to be 0?

Mike
Reply | Threaded
Open this post in threaded view
|

Re: Just curious: Why is tonumber(nil) not 0?

Pierpaolo Bernardi


On Mon, Apr 27, 2009 at 3:32 PM, Mike Crowe <[hidden email]> wrote:
Hi folks,

I was briefly caught by doing this test:

  if tonumber(something) > 0 then
     ...
  end

with something=nil.
I'm not advocating a change, but what's the thought-process behind this behavior?  Wouldn't you consider the numeric value of nil to be 0?

hmmmm

wasn't april 1st some 26 days ago?

:/

Reply | Threaded
Open this post in threaded view
|

Re: Just curious: Why is tonumber(nil) not 0?

Henk Boom-2
In reply to this post by Mike Crowe
2009/4/27 Mike Crowe <[hidden email]>:

> Hi folks,
>
> I was briefly caught by doing this test:
>
>   if tonumber(something) > 0 then
>      ...
>   end
>
> with something=nil.
> I'm not advocating a change, but what's the thought-process behind this
> behavior?  Wouldn't you consider the numeric value of nil to be 0?

It seems to me that tonumber() aims to operate on things which are
already representations of numbers (numbers and strings in this case),
and returns nil when its argument isn't a representation of a number.
nil represents nothing, and while you could argue that 0 represents
the number of things in nothing, it doesn't represent nothing in the
context of this environment.
Reply | Threaded
Open this post in threaded view
|

Re: Just curious: Why is tonumber(nil) not 0?

Timothy Baldridge
In reply to this post by Mike Crowe
Ahhh, the classic argument. Nil and zero are not the same, zero
represents the lack of quantity of something being counted, nil
represents the lack of data. In some areas of computing (such as the
database work I do) this is a major difference. In my mind,
tonumber(nil) should return nil.

Timothy

On Mon, Apr 27, 2009 at 8:32 AM, Mike Crowe <[hidden email]> wrote:

> Hi folks,
>
> I was briefly caught by doing this test:
>
>   if tonumber(something) > 0 then
>      ...
>   end
>
> with something=nil.
> I'm not advocating a change, but what's the thought-process behind this
> behavior?  Wouldn't you consider the numeric value of nil to be 0?
>
> Mike
>



--
“One of the main causes of the fall of the Roman Empire was
that–lacking zero–they had no way to indicate successful termination
of their C programs.”
(Robert Firth)
Reply | Threaded
Open this post in threaded view
|

Re: Just curious: Why is tonumber(nil) not 0?

Luiz Henrique de Figueiredo
In reply to this post by Mike Crowe
> I was briefly caught by doing this test:
>
>     if tonumber(something) > 0 then
>        ...
>     end
>
> with something=nil.
>
> I'm not advocating a change, but what's the thought-process behind this
> behavior?  Wouldn't you consider the numeric value of nil to be 0?

The current behavior is useful to catch typos:
        myNumber = "123.456"
        print(tonumber(mynumber)) -- note typo

If you really need the numeric value of nil to be 0 just redefine tonumber:
        do
                local _tonumber=tonumber
                function tonumber(x)
                        if x==nil then return 0 else return _tonumber(x) end
                end
        end

If tonumber supported a __tonumber metamethod, this could be simpler.
Reply | Threaded
Open this post in threaded view
|

Re: Just curious: Why is tonumber(nil) not 0?

Mike Crowe
In reply to this post by Timothy Baldridge
Timothy Baldridge wrote:
> Ahhh, the classic argument. Nil and zero are not the same, zero
> represents the lack of quantity of something being counted, nil
> represents the lack of data. In some areas of computing (such as the
> database work I do) this is a major difference. In my mind,
> tonumber(nil) should return nil.
Actually, I'm not arguing that nil == 0, but asking if tonumber(nil) ==
0.  i.e. if you move nil into the numeric domain, should it's value be 0...

Reply | Threaded
Open this post in threaded view
|

Re: Just curious: Why is tonumber(nil) not 0?

Henk Boom-2
2009/4/27 Mike Crowe <[hidden email]>:

> Timothy Baldridge wrote:
>>
>> Ahhh, the classic argument. Nil and zero are not the same, zero
>> represents the lack of quantity of something being counted, nil
>> represents the lack of data. In some areas of computing (such as the
>> database work I do) this is a major difference. In my mind,
>> tonumber(nil) should return nil.
>
> Actually, I'm not arguing that nil == 0, but asking if tonumber(nil) == 0.
>  i.e. if you move nil into the numeric domain, should it's value be 0...

I don't see tonumber as taking something into the numeric domain, I
see it is converting something already in the numeric domain to a
numeric representation. The string "123.45" could already "represent"
a number, it just has a funny storage format. The same isn't really
true of nil.

    Henk
Reply | Threaded
Open this post in threaded view
|

Re: Just curious: Why is tonumber(nil) not 0?

Evan DeMond
In reply to this post by Mike Crowe
Just to look at things another way, then, is it troubling that tonumber() isn't guaranteed to return a number?

Not saying that I think that nil should be convertible to a numeric value, but I'd like to hear others' thoughts on that point. =)

Evan
Reply | Threaded
Open this post in threaded view
|

Re: Just curious: Why is tonumber(nil) not 0?

Philippe Lhoste
In reply to this post by Luiz Henrique de Figueiredo
On 27/04/2009 15:52, Luiz Henrique de Figueiredo wrote:
> If tonumber supported a __tonumber metamethod, this could be simpler.

I was wondering of the use cases, but it can be useful, like:
tonumber("0b01101001")
tonumber("1 456 768") or tonumber("1,456,768") if you prefer
and so on.

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --

Reply | Threaded
Open this post in threaded view
|

Re: Just curious: Why is tonumber(nil) not 0?

Henk Boom-2
In reply to this post by Evan DeMond
2009/4/27 Evan DeMond <[hidden email]>:
> Just to look at things another way, then, is it troubling that tonumber()
> isn't guaranteed to return a number?
> Not saying that I think that nil should be convertible to a numeric value,
> but I'd like to hear others' thoughts on that point. =)

If it were guaranteed to return a number there would be no way to
signal failure. You would need to have an isnumeric() to go along with
it, and then you might find that 'isnumeric(x) and tonumber(x)' would
become a common idiom =).

    Henk
Reply | Threaded
Open this post in threaded view
|

Re: Just curious: Why is tonumber(nil) not 0?

Evan DeMond
On Mon, Apr 27, 2009 at 10:21 AM, Henk Boom <[hidden email]> wrote:
2009/4/27 Evan DeMond <[hidden email]>:
> Just to look at things another way, then, is it troubling that tonumber()
> isn't guaranteed to return a number?
> Not saying that I think that nil should be convertible to a numeric value,
> but I'd like to hear others' thoughts on that point. =)

If it were guaranteed to return a number there would be no way to
signal failure. You would need to have an isnumeric() to go along with
it, and then you might find that 'isnumeric(x) and tonumber(x)' would
become a common idiom =).

   Henk

Yep, and this was one of those times I recognized the obvious immediately after hitting "Send," too. Sorry for the noise. =)
Reply | Threaded
Open this post in threaded view
|

Re: Just curious: Why is tonumber(nil) not 0?

Henk Boom-2
2009/4/27 Evan DeMond <[hidden email]>:
> Yep, and this was one of those times I recognized the obvious immediately
> after hitting "Send," too. Sorry for the noise. =)

It's apparently not obvious to everyone:

http://us3.php.net/manual/en/function.intval.php

Trying to program with that function is really an exercise in frustration.

    Henk
Reply | Threaded
Open this post in threaded view
|

Re: Just curious: Why is tonumber(nil) not 0?

Jerome Vuarand
In reply to this post by Evan DeMond
2009/4/27 Evan DeMond <[hidden email]>:
> Just to look at things another way, then, is it troubling that tonumber()
> isn't guaranteed to return a number?
> Not saying that I think that nil should be convertible to a numeric value,
> but I'd like to hear others' thoughts on that point. =)

tonumber is not taking an arbitrary value and converting it to a
numerical value. It is taking an already numerical value, represented
by a Lua object which type *is not* 'number', and returns the same
numerical value in a Lua object which type *is* 'number'. See it as a
type cast. Just like a C type cast you can lose precision too.

For me it seems perfectly natural that if you give it a value which is
not numerical it is considered an error. It could have returned nil
plus an error message, but I guess there was no real need to be more
specific on the error reason so only nil is returned.
Reply | Threaded
Open this post in threaded view
|

Re: Just curious: Why is tonumber(nil) not 0?

Tommy Pettersson
In reply to this post by Mike Crowe
On Mon, Apr 27, 2009 at 09:32:03AM -0400, Mike Crowe wrote:
>    if tonumber(something) > 0 then

I think an elegant solution is:

  if (tonumber(something) or 0)  >  0 then

This both chooses and shows the numeral representation of non-numbers.


--
Tommy Pettersson <[hidden email]>