# Re: Work around for "Bignum out of Float range"?

**URL:** <https://www.ruby-forum.com/t/re-work-around-for-bignum-out-of-float-range/100437>\
**Category:** Ruby\
**Created:** [February 2, 2006, 8:35pm UTC](https://www.ruby-forum.com/t/re-work-around-for-bignum-out-of-float-range/100437 "2006-02-02T20:35:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 2, 2006, 8:35pm UTC](https://www.ruby-forum.com/t/re-work-around-for-bignum-out-of-float-range/100437/1 "2006-02-02T20:35:42Z")

</div>

Dear Sam,

your problem is somewhat insolvable, since the square root of 5 has  
an infinite number of digits, so it cannot be represented correctly  
on any computer (in decimal or binary notation).  
For arbitrary precision calculations with Fixnum exponents, there’s  
bigdecimal.

require “bigdecimal”

def calc(n,prec)

# prec is the precision of the sqrt calculations

res=(BigDecimal.new(“2”) \*\* n)\*BigDecimal(“5”).sqrt(prec)  
end

puts calc(10000) =\> 0.44610[several lines of digits]\*10^3011 (for  
prec=10)

However, don’t believe in too many of these digits…  
If you still want to multiply by square roots accurately, it might be a  
good  
idea to look at continued fractions - every square root has a continued  
fraction representation that eventually ends in a periodic pattern.  
There is an introduction to arithmetic with them at  
\_[http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/cfINTRO.html\_](http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/cfINTRO.html_)  
([http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/cfINTRO.html](http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/cfINTRO.html))

Hope that helps,

Axel

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 3, 2006, 12:25am UTC](https://www.ruby-forum.com/t/re-work-around-for-bignum-out-of-float-range/100437/2 "2006-02-03T00:25:15Z")

</div>

removed\_email\_address@domain.invalid wrote:

> def calc(n,prec)  
> However, don’t believe in too many of these digits…  
> If you still want to multiply by square roots accurately, it might be a good  
> idea to look at continued fractions - every square root has a continued  
> fraction representation that eventually ends in a periodic pattern.  
> There is an introduction to arithmetic with them at  
> \_[http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/cfINTRO.html\_](http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/cfINTRO.html_)  
> ([http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/cfINTRO.html](http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/cfINTRO.html))
> 
> Hope that helps,
> 
> Axel

Thank you, Axel.  
Actually I came across this problem while reading an article about  
fibonacci.  
See  
[http://epsilondelta.wordpress.com/2006/01/29/programming-like-a-mathematician-i-closures/](http://epsilondelta.wordpress.com/2006/01/29/programming-like-a-mathematician-i-closures/)  
…  
There’s a formula for fibonacci and the writer showed 1000000th  
fibonacci.  
I wanted to calculate it using the formula in Ruby.

I’ll read the docs you mentioned.

Thanks.  
Sam
