Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Isn't 00.0 the same as 0.0?

Tags:

numbers

ruby

I just found a strange problem in the programming language ruby, it isn't a big problem, but I just can't understand why it happens. It would interest me if someone knows the problem for my problem.

In ruby you can write 0 or 00, that doesn't matter, it comes to the same result.
If you run 0 === 00 you also get true meaning that the two inputs are exactly the same.

0.0 also equals 0, so logically 00.0 should also equal 0.0 but the problem is, that if you try to use the number 00.0 then you'll just get an error. If you run for example:

a = 00.0

You get this error:

syntax error, unexpected tINTEGER

Of course I know this is a small problem, but as said I'd like to understand why the computer doesn't treat 00.0 the same as 0.0?

like image 717
evotopid Avatar asked Apr 16 '12 18:04

evotopid


2 Answers

The thing is that when parsing and ruby sees that a number with more than two digits starts with the character 0, it parses it as an octal integer number. Thus, when it parses 00, it is 0 in octal which is the same as 0 in decimal. But if it finds a . then it is an invalid integer and that is the error it shows.

like image 144
Santiago Alessandri Avatar answered Nov 02 '22 07:11

Santiago Alessandri


I tried "a = 00.0" in http://tryruby.com, and got:

SyntaxError: no .<digit> floating literal anymore put 0 before dot. near line 1: ""

Clearly the Ruby lexer isn't expecting that form of float.

like image 35
Ned Batchelder Avatar answered Nov 02 '22 08:11

Ned Batchelder