Velocity Reviews > Ruby > Is 2.0 Integer or Float?

# Is 2.0 Integer or Float?

S. Robert James
Guest
Posts: n/a

 11-14-2006
I'd like to be able to do:

x = 2.0
assert x.integral?

the :integer method returns false in this case.
What would be a good way to write a different method to check?

James Edward Gray II
Guest
Posts: n/a

 11-14-2006
On Nov 14, 2006, at 10:35 AM, S. Robert James wrote:

> I'd like to be able to do:
>
> x = 2.0
> assert x.integral?
>
> the :integer method returns false in this case.
> What would be a good way to write a different method to check?

>> 2.0.is_a? Float

=> true
>> 2.0.is_a? Integer

=> false

James Edward Gray II

Szymon Rozga
Guest
Posts: n/a

 11-14-2006
Hope the following helps:

irb(main):001:0> 2.0.class
=> Float
irb(main):002:0> 2.class
=> Fixnum
irb(main):003:0> 2.0.is_a?(Float)
=> true
irb(main):004:0> 2.0.is_a?(Fixnum)
=> false
irb(main):005:0> 2.is_a?(Float)
=> false
irb(main):006:0> 2.is_a?(Fixnum)
=> true

-Szymon

On Nov 14, 11:34 am, "S. Robert James" <(E-Mail Removed)> wrote:
> I'd like to be able to do:
>
> x = 2.0
> assert x.integral?
>
> the :integer method returns false in this case.
> What would be a good way to write a different method to check?

Tim Pease
Guest
Posts: n/a

 11-14-2006
On 11/14/06, S. Robert James <(E-Mail Removed)> wrote:
> I'd like to be able to do:
>
> x = 2.0
> assert x.integral?
>
> the :integer method returns false in this case.
> What would be a good way to write a different method to check?
>

class Numeric
def is_integer?
Integer(self) == self
end
end

2.0.is_integer? #=> true
2.1.is_integer? #=> false
2.is_integer? #=> true

Blessings,
TwP

gwtmp01@mac.com
Guest
Posts: n/a

 11-14-2006

On Nov 14, 2006, at 11:35 AM, S. Robert James wrote:

> I'd like to be able to do:
>
> x = 2.0
> assert x.integral?
>
> the :integer method returns false in this case.
> What would be a good way to write a different method to check?

Maybe it is just me, I don't have a lot of experience with floating
point
issues, but this request has a bad smell to it. Once you've decided to
model something with floating point values it seems a bit unusual to
want
to know if any particular value happens to be the exact floating point
representation of an integer. Isn't the entire notion of equality
somewhat
ambiguous in the floating point world? I thought this is usually
handled by
asking if two values are within some delta (also represented as a
floating
point value) of each other.

Any numerical computation folks out there who can share their wisdom?

Maybe the value should be modeled as an integer with different units
of dollars to model monetary values.

Gary Wright

Wilson Bilkovich
Guest
Posts: n/a

 11-14-2006
On 11/14/06, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:
>
> On Nov 14, 2006, at 11:35 AM, S. Robert James wrote:
>
> > I'd like to be able to do:
> >
> > x = 2.0
> > assert x.integral?
> >
> > the :integer method returns false in this case.
> > What would be a good way to write a different method to check?

>
> Maybe it is just me, I don't have a lot of experience with floating
> point
> issues, but this request has a bad smell to it. Once you've decided to
> model something with floating point values it seems a bit unusual to
> want
> to know if any particular value happens to be the exact floating point
> representation of an integer. Isn't the entire notion of equality
> somewhat
> ambiguous in the floating point world? I thought this is usually
> handled by
> asking if two values are within some delta (also represented as a
> floating
> point value) of each other.
>
> Any numerical computation folks out there who can share their wisdom?
>
> Maybe the value should be modeled as an integer with different units
> instead of floating point? For example using units of pennies instead
> of dollars to model monetary values.
>

IEEE floating point can store an exact representation of any integer
that the fp number is large enough to contain.
I've never used it this way, but supposedly that is used to handle
53-bit ints on platforms that have 64-bit floats and only 32-bit ints.

That may or may not be what the OP is trying to do.

James Edward Gray II
Guest
Posts: n/a

 11-14-2006
On Nov 14, 2006, at 2:21 PM, Wilson Bilkovich wrote:

> IEEE floating point can store an exact representation of any integer
> that the fp number is large enough to contain.
> I've never used it this way, but supposedly that is used to handle
> 53-bit ints on platforms that have 64-bit floats and only 32-bit ints.
>
> That may or may not be what the OP is trying to do.

I'm pretty sure Lua uses floats for everything, for exactly this reason.

James Edward Gray II

Joel VanderWerf
Guest
Posts: n/a

 11-14-2006
James Edward Gray II wrote:
> On Nov 14, 2006, at 2:21 PM, Wilson Bilkovich wrote:
>
>> IEEE floating point can store an exact representation of any integer
>> that the fp number is large enough to contain.
>> I've never used it this way, but supposedly that is used to handle
>> 53-bit ints on platforms that have 64-bit floats and only 32-bit ints.
>>
>> That may or may not be what the OP is trying to do.

>
> I'm pretty sure Lua uses floats for everything, for exactly this reason.

How does that work? Is there an 11-bit pattern that signifies "use the
remaining 53 bits as a integer"? Do math library functions respect this?

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

James Edward Gray II
Guest
Posts: n/a

 11-14-2006
On Nov 14, 2006, at 2:29 PM, Joel VanderWerf wrote:

> James Edward Gray II wrote:
>> On Nov 14, 2006, at 2:21 PM, Wilson Bilkovich wrote:
>>> IEEE floating point can store an exact representation of any integer
>>> that the fp number is large enough to contain.
>>> I've never used it this way, but supposedly that is used to handle
>>> 53-bit ints on platforms that have 64-bit floats and only 32-bit
>>> ints.
>>>
>>> That may or may not be what the OP is trying to do.

>> I'm pretty sure Lua uses floats for everything, for exactly this
>> reason.

>
> How does that work? Is there an 11-bit pattern that signifies "use
> the remaining 53 bits as a integer"? Do math library functions
> respect this?

I have no idea how they do what they do:

http://lua-users.org/wiki/FloatingPoint

James Edward Gray II

Sander Land
Guest
Posts: n/a

 11-14-2006
On 11/14/06, Joel VanderWerf <(E-Mail Removed)> wrote:
> How does that work? Is there an 11-bit pattern that signifies "use the
> remaining 53 bits as a integer"? Do math library functions respect this?

53 bits is just the mantissa size.
imagine using scientific notation a * 10^b with a<1 and 3 decimal
digits, this gives you accurate integers up to 1000.
999 => 0.999 * 10^3 exact
1001=> 0.100 * 10^4 overflowed