[ Arithmetic | Reference Manual | Alphabetic Index ]

rationalize(+Number, -Result)

Converts Number into a compact rational number and unifies it with Result.
A number.
A variable or rational number.


This predicate is used by the ECLiPSe compiler to expand evaluable arithmetic expressions. So the call to rationalize(Number, Result) is equivalent to
    Result is rationalize(Number)
which should be preferred.

When Number is an integer, Result is a rational with denominator 1.

When Number is already a rational, Result is identical to Number.

When Number is a float, Result is a rational whose value approximates the value of the float to the accuracy of the float representation. rationalize/2 usually produces more compact rationals that rational/2. Both rationalize/2 and rational/2 produce results that convert back into the original float. rational/2 is usually faster than rationalize/2.

Bounded reals cannot be converted to rationals.

In coroutining mode, if Number is uninstantiated, the call to rationalize/2 is delayed until this variable is instantiated.

Modes and Determinism


(4) instantiation fault
Number is not instantiated (non-coroutining mode only).
(24) number expected
Number is not of a numeric type.
(141) unimplemented functionality
Number is a bounded real


      rationalize(25, 25_1).
      rationalize(1.5, 3_2).
      rationalize(-6, Result).      (gives Result = -6_1)
      rationalize(0.1, Result).     (gives Result = 1_10)
      rationalize(1, 2_1).
      rationalize(3, 3).
      rationalize(1, r).
      rationalize(A, 1_3).                   (Error 4).
      rationalize(4 + 2, 6_1).               (Error 24).
      rationalize(0.9__1.1, X).              (Error 141).

See Also

rational / 2, is / 2