From: Warren Brown Date: 2004-10-16T04:50:39+09:00 Subject: Re: "nan".to_f ? Matz, > Although we can tell whether a float value is NaN by > using isnan(), but as far as I know there's no > portably way to generate NaN. I think it's not > guaranteed that 0.0/0.0 generate NaN. > > If I'm wrong, feel free to correct me. From what I can tell from Googling, the following operations are *guaranteed* by IEEE Standard 754 to produce NaNs (where Inf = 1.0 / 0.0): Inf - Inf Inf * 0 0 / 0 Inf / Inf x % 0 Inf % x sqrt(x) (where x < 0) One particularly authoritative document is at http://grouper.ieee.org/groups/754. The first bullet near the bottom is a link to David Goldberg's article "What Every Computer Scientist Should Know about Floating-Point Arithmetic". Page 199 gives a table similar to this one. Of course, not every floating point package is IEEE Standard 754, but these operations should produce NaNs regardless of the standard used, since there are no other legal values available. Do we know of any other floating-point standards that Ruby needs to support? - Warren Brown