From: Paul Lutus Date: 2006-11-17T09:10:08+09:00 Subject: Re: Is 2.0 Integer or Float? Dale Martenson wrote: > Paul Lutus wrote: / ... >> The OP is trying to establish whether the _content_ of the float >> variable is >> an integer, not whether the variable has float type. That is relatively >> easy to establish. > > Sorry to join this conversation late, but to me this is a strange > question. Floats are never integers. That depends on how we are defining our terms. In a mathematical sense, of course, yes. In a computer-science sense however, one can place an integer in a float variable, and it will behave itself as though it were an integer -- for example, regardless of its value (within the allowed range), it will permit itself to be converted between bases without any of the well-documented gotchas to which true floats are prone. Also, additions, subtractions and multiplies will be carried out with the expected integer results. > Integers are counting numbers with > exact values over a certain range determined by their internal > representation. And such an integer can be stored in a float variable type. > While floating point numbers are approximations which > suffer from errors of precision. This refers to floats in float variables, not integers in float variables. > They may approach an integer bound and > yet never achieve it. So the question is what approximation is adequate > to say that a float is the integer that it approaches. What is close > enough? No, as a matter of fact, an integer stored in a float variable type doesn't lose its identity as an integer. It is still an integer, and behaves itself in a way that a float cannot. 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. -- Paul Lutus http://www.arachnoid.com