[ Arithmetic | The ECLiPSe Built-In Predicates | Reference Manual | Alphabetic Index ]

rational(+Number, -Result)

Converts Number into a 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 rational(Number, Result) is equivalent to
    Result is rational(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 is exactly equal to the value of the floating-point number. Since floats are usually approximations of the intended value, the results may look unintuitive and have unnecessarily large numerators and denominators. Use rationalize/2 to produce the most compact rational that still converts 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 rational/2 is delayed until this variable is instantiated.

Modes and Determinism


(4) instantiation fault
Number is not instantiated (non-coroutining mode only).
(5) type error
Result is a number but not of type rational.
(24) number expected
Number is not of a numeric type.
(24) number expected
Result is neither a number nor a variable.
(141) unimplemented functionality
Number is a bounded real


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

See Also

rationalize / 2, is / 2