Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Is 2.0 Integer or Float?

Reply
Thread Tools

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?

 
Reply With Quote
 
 
 
 
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

 
Reply With Quote
 
 
 
 
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" <srobertja...@gmail.com> 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?


 
Reply With Quote
 
Tim Pease
Guest
Posts: n/a
 
      11-14-2006
On 11/14/06, 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?
>


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

 
Reply With Quote
 
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
instead of floating point? For example using units of pennies instead
of dollars to model monetary values.

Gary Wright




 
Reply With Quote
 
Wilson Bilkovich
Guest
Posts: n/a
 
      11-14-2006
On 11/14/06, <> 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.

 
Reply With Quote
 
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

 
Reply With Quote
 
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

 
Reply With Quote
 
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

 
Reply With Quote
 
Sander Land
Guest
Posts: n/a
 
      11-14-2006
On 11/14/06, Joel VanderWerf <> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I add an Integer to another Integer? Sebastian Stelzer Java 6 04-07-2010 07:03 PM
CType(x,Integer) vs. Integer.Parse(x) =?Utf-8?B?Sm9l?= ASP .Net 7 02-07-2006 02:30 AM
how do I make Class.forName("Integer") returning java.lang.Integer? Johannes Zellner Java 22 12-19-2005 11:22 AM
How do I add an Integer to another Integer? Sebastian Stelzer Java 2 10-15-2004 01:17 PM
No Math.min(Integer, Integer)? =?ISO-8859-1?Q?Thomas_Gagn=E9?= Java 0 07-29-2003 07:46 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57