From: Jim Driscoll Date: 2004-02-08T19:46:11+09:00 Subject: Re: [NUBY] Some simple (?) type questions Hi, On 8 Feb 2004, at 03:55, Silly wrote: > > First, I'm running into troubles with the Doubles. According to the > W3C, the "value space of double consists of the values m � 2^e, where > m is an integer whose absolute value is less than 2^53, and e is an > integer between -1075 and 970, inclusive." Now, I suspect that its > supposed to say "m � 10^e" but that isn't my problem. My problem is > that I simply can't seem to get the smallest values to come out as > anything other than 0. I'm sure its possible I'm just not sure where > to start. > Okay, apparently Ruby's floats are doubles, but that's probably not your problem. The formula is correct as-is, AFAIK, it's an expression of the number of bits available (in a 32-bit value) assigned to the (base-2) exponent. Anyway, I'd guess your only problem is: irb(main):001:0> a=1/(2**970) => 0 irb(main):002:0> b=1.0/(2**970) => 1.00208418e-292 ...how you're expressing creation of your very-small values. The numerator *must* be a float already, otherwise you're doing forced integer maths (Integer#/ not Float#/). By the way, after poking around in IRB, it looks like ruby impliments 64-bit floating point (if necessary), because I can make some extremely big numbers. Someone more knowledgeable can confirm if that's right. > My second problem has to do with the way the W3 defines numbers in > general. In addition to numbers as we all understand, they've added > INF, -INF, NaN and -0. Each of these have very specific meanings, most > insteresting is that -0 < 0 and NaN > INF. Now I was thinking that I > would implement INFClass and NaNClass similar to TrueClass and > FalseClass. However, I have no idea how to handle -0. > All that stuff is built right into the design of the floating point standard (It's an IEEE standard. Google for it), which ruby impliments: irb(main):001:0> a=1.0 => 1.0 irb(main):002:0> a/=0 => Infinity irb(main):003:0> b=0.0 => 0.0 irb(main):004:0> b/=0 => NaN irb(main):005:0> c=-1.0 => -1.0 irb(main):006:0> c/=0 => -Infinity irb(main):007:0> d=1 => 1 irb(main):008:0> d/=c => -0.0 irb(main):009:0> So ruby handles it all for you (even if it doesn't seem to have constants for the more 'interesting' values). See Float#infinity? and Float#nan? too. Jim