[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Float.finite ?

aldric[removeme]

4/10/2009 2:18:00 PM

How does one use .finite? effectively? I tried with (1.0/3).finite? => true...
I tried with BigDecimal.. Still true. I'm pretty sure these numbers aren't
finite. How do I make this work?

--
Aldric Giacomoni<br>
Time does not count itself. You have only to look at a circle and this is
apparent.

-- Leto II (The Tyrant)
5 Answers

Jeff Schwab

4/10/2009 2:25:00 PM

0

ITYM Float#finite?

Aldric Giacomoni wrote:
> How does one use .finite? effectively?

> I tried with (1.0/3).finite? => true...
> I tried with BigDecimal.. Still true. I'm pretty sure these numbers aren't
> finite.

They are finite.

> How do I make this work?

#finite? doesn't tell you whether the decimal (or any other)
representation of a number requires finite space. #finite? tells you
only whether the value is finite, i.e. not infinite.

irb(main):002:0> 1.0/3.0
=> 0.333333333333333
irb(main):003:0> _.finite?
=> true
irb(main):004:0> 1.0/0
=> Infinity
irb(main):005:0> _.finite?
=> false
irb(main):006:0>

Todd Benson

4/10/2009 2:35:00 PM

0

On Fri, Apr 10, 2009 at 9:20 AM, Aldric Giacomoni
<"aldric[removeme]"@trevoke.net> wrote:
> How does one use .finite? effectively? I tried with (1.0/3).finite? => true...
> I tried with BigDecimal.. Still true. I'm pretty sure these numbers aren't
> finite. How do I make this work?

1.0/3 is most certainly finite. Here is how #finite? works...

irb(main):001:0> 1.0/0
=> Infiinity
irb(main):002:0> (1.0/0).finite?
=> false
irb(main):003:0> (-1)**0.5
=> NaN
irb(main):004:0> ((-1)**0.5).finite?
=> false

Todd

Stefano Crocco

4/10/2009 3:14:00 PM

0

Alle Friday 10 April 2009, Aldric Giacomoni ha scritto:
> How does one use .finite? effectively? I tried with (1.0/3).finite? =>
> true... I tried with BigDecimal.. Still true. I'm pretty sure these numbers
> aren't finite. How do I make this work?

According to the ri documentation, Float#finite? only returns false if self is
not inifinite and self.nan? is false. For example,

(1/0.0).finite?
=> false

What you wanted to know is whether the number has a finite or infinite number
of decimal digits. Unfortunately, I don't think such a method exists. You
can't do that with floats, because all floats have a finite number of decimal
digits (you can't very well store infinite digits in your computer's memory,
after all). I suppose it can be done with the Rational class, but you'd have
to write the method yourself. Here's a simple example of how this could be
done:

require 'rational'

class Rational

def finite_digits?
d = denominator
return false if d % 2 !=0 and d % 5 != 0
d = d / 2 while d % 2 ==0
d = d / 5 while d % 5 == 0
d == 1
end

end

Stefano

aldric[removeme]

4/10/2009 8:08:00 PM

0

Ah! Understood. Thank you.. Big help :)

Stefano Crocco wrote:
> Alle Friday 10 April 2009, Aldric Giacomoni ha scritto:
>> How does one use .finite? effectively? I tried with (1.0/3).finite? =>
>> true... I tried with BigDecimal.. Still true. I'm pretty sure these numbers
>> aren't finite. How do I make this work?
>
> According to the ri documentation, Float#finite? only returns false if self is
> not inifinite and self.nan? is false. For example,
>
> (1/0.0).finite?
> => false
>
> What you wanted to know is whether the number has a finite or infinite number
> of decimal digits. Unfortunately, I don't think such a method exists. You
> can't do that with floats, because all floats have a finite number of decimal
> digits (you can't very well store infinite digits in your computer's memory,
> after all). I suppose it can be done with the Rational class, but you'd have
> to write the method yourself. Here's a simple example of how this could be
> done:
>
> require 'rational'
>
> class Rational
>
> def finite_digits?
> d = denominator
> return false if d % 2 !=0 and d % 5 != 0
> d = d / 2 while d % 2 ==0
> d = d / 5 while d % 5 == 0
> d == 1
> end
>
> end
>
> Stefano
>

--
Aldric Giacomoni<br>
Sometimes one should just look at things and think about things without
doing things. -- Calvin

aldric[removeme]

4/10/2009 8:08:00 PM

0

I see. Thank you..I misunderstood what the function did :)

Jeff Schwab wrote:
> ITYM Float#finite?
>
> Aldric Giacomoni wrote:
>> How does one use .finite? effectively?
>
>> I tried with (1.0/3).finite? => true...
>> I tried with BigDecimal.. Still true. I'm pretty sure these numbers
>> aren't
>> finite.
>
> They are finite.
>
>> How do I make this work?
>
> #finite? doesn't tell you whether the decimal (or any other)
> representation of a number requires finite space. #finite? tells you
> only whether the value is finite, i.e. not infinite.
>
> irb(main):002:0> 1.0/3.0
> => 0.333333333333333
> irb(main):003:0> _.finite?
> => true
> irb(main):004:0> 1.0/0
> => Infinity
> irb(main):005:0> _.finite?
> => false
> irb(main):006:0>

--
Aldric Giacomoni<br>
One meets his destiny often on the road he takes to avoid it.