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

14 messages
Open this post in threaded view
|

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

 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
Open this post in threaded view
|

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

 On Mon, Apr 27, 2009 at 3:32 PM, Mike Crowe 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?hmmmmwasn't april 1st some 26 days ago? :/
Open this post in threaded view
|

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

 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.
Open this post in threaded view
|

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

 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)
Open this post in threaded view
|

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

 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.
Open this post in threaded view
|

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

 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...
Open this post in threaded view
|

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

 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
Open this post in threaded view
|

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

 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
Open this post in threaded view
|

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

 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--  --  --  --  --  --  --  --  --  --  --  --  --  --
Open this post in threaded view
|

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

 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
Open this post in threaded view
|

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

 On Mon, Apr 27, 2009 at 10:21 AM, Henk Boom 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. =)
Open this post in threaded view
|

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

 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.phpTrying to program with that function is really an exercise in frustration.     Henk