[ Arithmetic | Reference Manual | Alphabetic Index ]

rationalize(+Number, -Result)

-Result is rationalize(+Number)

Converts Number into a compact rational number and unifies it with Result.
Number
A number.
Result
Output: rational number.

Description

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

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.

This predicate can be used as a function in arithmetic expressions. In coroutining mode, if Number is uninstantiated, the call to rationalize/2 is delayed until this variable is instantiated.

Modes and Determinism

Exceptions

(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

Examples

Success:
    Result is rationalize(25).		% gives Result = 25_1
    Result is rationalize(1.5).		% gives Result = 3_2
    Result is rationalize(3_4).		% gives Result = 3_4
    Result is rationalize(9_12).	% gives Result = 3_4
    Result is rationalize(-6).		% gives Result = -6_1
    Result is rationalize(0.1).		% gives Result = 1_10

Error:
    Result is rationalize(0.9__1.1).	% unimplemented

See Also

rational / 2, is / 2