Forum: Ruby Work around for "Bignum out of Float range"?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ca0b18ec9e11dc777b2b8084fe5d5f90?d=identicon&s=25 Sam Kong (Guest)
on 2006-02-02 19:29
(Received via mailing list)
Hi!

First, some codes.

-----------
def calc(n)
	(2 ** n) * (5 ** 0.5)
end

puts calc(10000)
=>warning: Bignum out of Float range
-----------

I understand what the problem is.
If I want to get the result, how can I work around the barrier?

Thanks.
Sam
Ddbfebb47432f6599da361df6a135c7c?d=identicon&s=25 Adam Shelly (Guest)
on 2006-02-02 20:32
(Received via mailing list)
On 2/2/06, Sam Kong <sam.s.kong@gmail.com> wrote:
> If I want to get the result, how can I work around the barrier?

here's some hacky code which returns a 2-element array
the first number is the significant digits and the second number is
the exponent:

def calc(n)
  exp=0
  val = (2 ** n)
  while val*3 > 1e290.to_i
    val/=10
    exp+=1
  end
  val *=(5**0.5)
  unless val.to_s.grep(/(.*)e\+(.*)/).empty?
    val = $1.to_f
    exp+=$2.to_i
  end
  [val, exp]
end

p calc 1
p calc 10
p calc 100
p calc 1000
p calc 10000

[4.47213595499958, 0]
[2289.73360895978, 0]
[2.83455291382873, 30]
[2.39596608414461, 301]
[4.46109674874798, 3010]

-Adam
This topic is locked and can not be replied to.