compat.norational
- Rational-less arithmetic ¶Until release 0.8.7, Gauche didn’t have exact rational numbers.
It was able to read the rational number literals such as
2/3
, but they are immediately coerced to inexact real
numbers (except when it represents a whole integer).
And if you divided an exact integer by another exact integer,
the result could be coerced to an inexact real if the result
wasn’t a whole integer.
As of 0.8.8, this is not the case anymore. Exact division always yields exact result, except when the divisor is zero.
(/ 2 3) ⇒ 2/3 (/ 5) ⇒ 1/5 (/ 4 2) ⇒ 2
This is more precise, but has one drawback: exact rational arithmetic is much slower than the integer and inexact real arithmetic. If you inadvertently produce a rational number in the early stage of calculation, and continue to apply exact arithmetic, performance would be degraded miserably.
The proper way to solve this is to insert exact->inexact
to appropriate places. However, to ease the transition, you can
just import this module and the division /
behaves
in the way it used to.
(use compat.norational) (/ 2 3) ⇒ 0.6666666666666666 (/ 5) ⇒ 0.2 (/ 4 2) ⇒ 2
The effect is not global, but only to the modules you explicitly
import compat.norational
.
This module only redefines /
. So if your code has
exact rational literals, they are treated as exact rationals
rather than coerced to inexact reals. You should prefix
rational literals with #i
to force Gauche to coerce
them to inexact reals:
gosh> 1/3 1/3 gosh> #i1/3 0.3333333333333333