From: "Berger, Daniel" Date: 2005-04-05T02:50:16+09:00 Subject: Re: Math.sqrt(NaN) behaves differently on windows and linux > -----Original Message----- > From: Dave Baldwin [mailto:dave.baldwin@3dlabs.com] > Sent: Monday, April 04, 2005 6:50 AM > To: ruby-talk ML > Subject: Re: Math.sqrt(NaN) behaves differently on windows and linux > > > > On 4 Apr 2005, at 12:48, Geert Fannes wrote: > > > Hello, the following statement gives different results on > windows and > > linux: > > Linux: > > > > irb(main):003:0> Math.sqrt(0.0/0.0) > > > > * NaN > > > Mac OS X gives this result. > > > Windows: > > > > irb(main):002:0> Math.sqrt(0.0/0.0) > > > > Errno::EDOM: Domain error - sqrt > > > > from (irb):2:in `sqrt' > > > > from (irb):2 > > > > I guess this is not as it should be. Which is the correct way to > > handle NaNs? > > > > How can I generate NaN without using the trick 0.0/0.0. Why > does 0/0 > > give a ZeroDivisionError and 0.0/0.0 a NaN > > > > > 0/0 is integer division and all bit patterns are used. > 0.0/0.0 is floating point division and for IEEE floating > point numbers > the result is defined by the standard to be NaN. Bit > patterns has been > reserved to all information like this to be encoded in the number. > > Can't help you with what Windows is doing or how to generate > a NaN more > cleanly from Ruby. It would be very simple to generate one > as part of > a C extension. > > Dave. > > > > > Greetings, > > > > Geert Fannes. From what I'm reading at http://www.opengroup.org/onlinepubs/009695399/functions/sqrt.html, it appears that in this case, Windows is, in fact, doing the right thing (though see below). From the text: "For finite values of x < -0, a domain error shall occur, and either a NaN (if supported), or an implementation-defined value shall be returned." It looks to me like *nix is only returning NaN, but isn't actually raising an error while Windows is raising an error, but is not returning NaN. begin x = Math.sqrt(0.0/0.0) rescue Errno::EDOM => err puts "X is: #{x}" # Should be NaN puts "Error: #{err}" raise end I'd say this is a bug all the way around. Regards, Dan