# Bug in Float? (was: NaN Error)

**URL:** <https://www.ruby-forum.com/t/bug-in-float-was-nan-error/92287>\
**Category:** Ruby\
**Created:** [April 17, 2007, 8:42pm UTC](https://www.ruby-forum.com/t/bug-in-float-was-nan-error/92287 "2007-04-17T20:42:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [April 17, 2007, 8:42pm UTC](https://www.ruby-forum.com/t/bug-in-float-was-nan-error/92287/1 "2007-04-17T20:42:12Z")

</div>

I ran across some peculiar behavior yesterday when working with large  
floating point values. Here is a small script that demonstrates the  
behavior. My question is, is this the expected behavior or is this a  
bug in Float?

> cat float\_bug.rb  
> puts "ruby #{RUBY\_VERSION} " +  
> "(#{RUBY\_RELEASE\_DATE} patchlevel #{RUBY\_PATCHLEVEL}) " +  
> “[#{RUBY\_PLATFORM}]”

puts “Float::MAX = #{Float::MAX}”  
puts “1.0e+300 = #{1.0e+300}”  
puts “1.0e+301 = #{1.0e+301}”  
puts “1.0e+308 = #{1.0e+308}”  
puts “1.0e+700 = #{1.0e+700}”

# EOF

When I run this script on my Windows machine, it produces the output I  
would expect – i.e. numbers above Float::MAX are “Infinity” and  
numbers below Float::MAX are valid floating point numbers.

> ruby -w float\_bug.rb  
> ruby 1.8.5 (2006-12-25 patchlevel 12) [i386-cygwin]  
> Float::MAX = 1.79769313486232e+308  
> 1.0e+300 = 1.0e+300  
> 1.0e+301 = 1.0e+301  
> 1.0e+308 = 1.0e+308  
> 1.0e+700 = Infinity

Now, when I run the exact same script on a Solaris machine, some odd  
things start to happen

> ruby -w float\_bug.rb  
> ruby 1.8.5 (2006-12-25 patchlevel 12) [sparc-solaris2.8]  
> Float::MAX = 1.79769313486232e+308  
> 1.0e+300 = 1.0e+300  
> 1.0e+301 = NaN  
> 1.0e+308 = NaN  
> 1.0e+700 = 8.55180271847103e+116

Whey are valid floats being treated as NaN? Better yet, why does  
1.0e+700 get evaluated to 8.5518027e+116?

Once more on the Windows 1-click installed version of Ruby.

> ruby -w float\_bug.rb  
> ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]  
> Float::MAX = 1.79769313486232e+308  
> 1.0e+300 = 1.0e+300  
> 1.0e+301 = NaN  
> 1.0e+308 = NaN  
> 1.0e+700 = 1.0e+189

Any thoughts on this one? I encourage you to copy the float\_bug.rb  
script and try it out for yourself.

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [April 17, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/bug-in-float-was-nan-error/92287/2 "2007-04-17T21:25:20Z")

</div>

$ ruby float\_bug.rb  
ruby 1.8.5 (2006-08-25) [i486-linux]  
Float::MAX = 1.79769313486232e+308  
1.0e+300 = 1.0e+300  
1.0e+301 = 1.0e+301  
1.0e+308 = 1.0e+308  
1.0e+700 = 1.02189775487683e+36

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [April 17, 2007, 9:46pm UTC](https://www.ruby-forum.com/t/bug-in-float-was-nan-error/92287/3 "2007-04-17T21:46:21Z")

</div>

On Apr 17, 11:41 am, “Tim P.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Once more on the Windows 1-click installed version of Ruby.
> 
> > ruby -w float\_bug.rb
> 
> ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]  
> Float::MAX = 1.79769313486232e+308  
> 1.0e+300 = 1.0e+300  
> 1.0e+301 = NaN  
> 1.0e+308 = NaN  
> 1.0e+700 = 1.0e+189

Further, (Windows, 1-click), same version:

C:\>ruby -v  
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]

C:\>irb -v  
irb 0.9.5(05/04/13)

C:\>irb  
irb(main):001:0\> 1.0e+800  
=\> 1.0e+289  
irb(main):002:0\> 1.0e+900  
=\> NaN  
irb(main):003:0\> 1.0e+1000  
=\> NaN  
irb(main):004:0\> 1.0e+1100  
(irb):4: warning: Float 1.0e+1100 out of range  
=\> Infinity

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [April 18, 2007, 3:42am UTC](https://www.ruby-forum.com/t/bug-in-float-was-nan-error/92287/4 "2007-04-18T03:42:51Z")

</div>

Hi,

At Wed, 18 Apr 2007 03:41:22 +0900,  
Tim P. wrote in [ruby-talk:248272]:

> I ran across some peculiar behavior yesterday when working with large  
> floating point values. Here is a small script that demonstrates the  
> behavior. My question is, is this the expected behavior or is this a  
> bug in Float?

Thank you, fixed now.

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [April 17, 2007, 11:21pm UTC](https://www.ruby-forum.com/t/bug-in-float-was-nan-error/92287/5 "2007-04-17T23:21:38Z")

</div>

On Apr 17, 12:41 pm, “Tim P.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> numbers below Float::MAX are valid floating point numbers.  
> Now, when I run the exact same script on a Solaris machine, some odd
> 
> 1.0e+301 = NaN  
> 1.0e+308 = NaN  
> 1.0e+700 = 1.0e+189
> 
> Any thoughts on this one? I encourage you to copy the float\_bug.rb  
> script and try it out for yourself.
> 
> Blessings,  
> TwP

On OS X 10.4.9

mvb:~ cms$ ruby float\_bug.rb  
ruby 1.8.6 (2007-03-13 patchlevel 0)  
Float::MAX = 1.79769313486232e+308  
1.0e+300 = 1.0e+300  
1.0e+301 = NaN  
1.0e+308 = NaN  
1.0e+700 = 0.0

And my Xubuntu box (pre-RUBY\_PATCHLEVEL apparently):

ruby 1.8.4 (2005-12-24)[i486-linux]  
Float::MAX = 1.79769313486232e+308  
1.0e+300 = 1.0e+300  
1.0e+301 = 1.0e+301  
1.0e+308 = 1.0e+308  
1.0e+700 = 0.0

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [April 18, 2007, 4:57pm UTC](https://www.ruby-forum.com/t/bug-in-float-was-nan-error/92287/6 "2007-04-18T16:57:54Z")

</div>

On 4/17/07, Nobuyoshi N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

Thank you very much, Nobu.

TwP
