Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Bug in Float? (was: NaN Error)

Reply
Thread Tools

Bug in Float? (was: NaN Error)

 
 
Tim Pease
Guest
Posts: n/a
 
      04-17-2007
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

 
Reply With Quote
 
 
 
 
Dan Stevens (IAmAI)
Guest
Posts: n/a
 
      04-17-2007
$ 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


On 17/04/07, Tim Pease <> wrote:
> 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
>
>


 
Reply With Quote
 
 
 
 
Phrogz
Guest
Posts: n/a
 
      04-17-2007
On Apr 17, 11:41 am, "Tim Pease" <tim.pe...@gmail.com> 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

 
Reply With Quote
 
Chris Shea
Guest
Posts: n/a
 
      04-17-2007
On Apr 17, 12:41 pm, "Tim Pease" <tim.pe...@gmail.com> wrote:
> 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


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

 
Reply With Quote
 
Nobuyoshi Nakada
Guest
Posts: n/a
 
      04-18-2007
Hi,

At Wed, 18 Apr 2007 03:41:22 +0900,
Tim Pease 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.

--
Nobu Nakada

 
Reply With Quote
 
Tim Pease
Guest
Posts: n/a
 
      04-18-2007
On 4/17/07, Nobuyoshi Nakada <> wrote:
> Hi,
>
> At Wed, 18 Apr 2007 03:41:22 +0900,
> Tim Pease 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.
>


Thank you very much, Nobu.

TwP

 
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
converting int to float gives nan (int value is definitely non-nan) googlegroups11@mailinator.com C Programming 6 03-19-2009 03:59 PM
*bug* *bug* *bug* David Raleigh Arnold Firefox 12 04-02-2007 03:13 AM
max(NaN,0) should be NaN norb1@yahoo.com C Programming 61 09-08-2006 05:41 PM
How to check if a String input is integer, float or NAN kt Java 3 04-22-2005 03:00 PM
Point2D.Distance method returns NaN, how is this possible? DiscoStu Java 9 08-05-2003 04:42 AM



Advertisments