In Ruby (1.9), are numbers always considered exact? I know Ruby does some fairly complex stuff with numbers under the hood, since you can do crazy things like ask for 1 trillion to the power of 1 trillion and you will get an answer (after waiting many moons for it to compute).
In Scheme, part of the specification dictates that implementations should indicate whether or not the implementation’s internal representation of the number is “exact” or not. For example, 1/2 is always exact, 1/3 is exact, 0.3333 is exact etc etc. But the result of an imprecise mathematical operation on an exact number may produce a number that the Scheme implementation knows to be inexact (due to floating point precision).
(exact? (/ 0.33333 2))
=> #f
That’s false, so it’s not exact.
Is there a way to deduce the same information in Ruby? If I always use the Complex (or Rational) representation of a number during mathematical operations, will it always be exact in Ruby, or just extremely close to exact?
Complex("0.33333") / 2
Is that exact, or not?
Ruby’s class
Floatis not exact. ClassIntegeron the other hand more or less “is” since it shifts seamlessly fromFixnumtoBignumIt looks like others would love to see the test you are asking about, namely
Numeric#exact?— see this feature request.