From: Dale Martenson Date: 2006-11-18T06:15:12+09:00 Subject: Re: Is 2.0 Integer or Float? Paul Lutus wrote: > Dale Martenson wrote: > >> Paul Lutus wrote: > > All your examples prove is that floats are not integers. But, hear me on > this, integers really are integers, no matter where they are located, > assuming they do not overflow the range of the variable in which they > are > stored. > > ---------------------------------------- > #!/usr/bin/ruby -w > > 0.upto(1e6) do |i| > fi = i.to_f > puts "Error: #{i}" if(i != fi) > end > ---------------------------------------- > > The silence was deafening. I lost patience with more repetitions, but I > think I know the outcome. > > You should know that many projects store integers in float variables in > cases where the float variable has more available bits of precision that > the platform's integer data type. Those who do this know there is no > downside -- the integer stored in the float variable won't start > misbehaving itself just because of its new, upscale quarters. Yes, you can store integers in floats. This is usually done with the exponent left at zero and the size of the mantissa limits the range of valid integers. If the exponent must be used, it becomes a computed value that is an approximation. Using irb, irb(main):163:0> 1e6.class => Float irb(main):164:0> 1e22.to_i => 10000000000000000000000 irb(main):165:0> 1e23.to_i => 99999999999999991611392 -- Posted via http://www.ruby-forum.com/.