4.27.2 General purpose arithmetic
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
      • Built-in Predicates
        • Arithmetic
          • General purpose arithmetic
            • >/2
            • </2
            • =</2
            • >=/2
            • =\=/2
            • =:=/2
            • is/2
            • Arithmetic types
            • Rational number examples
            • Rational numbers or floats
            • IEEE 754 floating point arithmetic
            • Floating point arithmetic precision
            • Arithmetic Functions
    • Packages

4.27.2.3 Rational numbers or floats

SWI-Prolog uses rational number arithmetic if the Prolog flag prefer_rationals is true and if this is defined for a function on the given operants. This results in perfectly precise answers. Unfortunately rational numbers can get really large and, if a precise answer is not needed, a big waste of memory and CPU time. In such cases one should use floating point arithmetic. The Prolog flag max_rational_size provides a tripwire to detect cases where rational numbers get big and react on these events.

Floating point arithmetic can be forced by forcing a float into an argument at any point, i.e., the result of a function with at least one float is always float except for the float-to-integer rounding and truncating functions such as round/1, rational/1 or float_integer_part/1.

Float arithmetic is typically forced by using a floating point constant as initial value or operant. Alternatively, the float/1 function forces conversion of the argument.