4.27.2.6 Arithmetic Functions
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
      • Built-in Predicates
        • Arithmetic
          • General purpose arithmetic
            • Arithmetic Functions
              • -/1
              • +/1
              • +/2
              • -/2
              • */2
              • //2
              • mod/2
              • rem/2
              • ///2
              • div/2
              • rdiv/2
              • gcd/2
              • lcm/2
              • abs/1
              • sign/1
              • copysign/2
              • nexttoward/2
              • roundtoward/2
              • max/2
              • min/2
              • ./2
              • random/1
              • random_float/0
              • round/1
              • integer/1
              • float/1
              • rational/1
              • rationalize/1
              • numerator/1
              • denominator/1
              • float_fractional_part/1
              • float_integer_part/1
              • truncate/1
              • floor/1
              • ceiling/1
              • ceil/1
              • >>/2
              • <</2
              • \//2
              • /\/2
              • xor/2
              • \/1
              • sqrt/1
              • sin/1
              • cos/1
              • tan/1
              • asin/1
              • acos/1
              • atan/1
              • atan2/2
              • atan/2
              • sinh/1
              • cosh/1
              • tanh/1
              • asinh/1
              • acosh/1
              • atanh/1
              • log/1
              • log10/1
              • exp/1
              • **/2
              • ^/2
              • powm/3
              • lgamma/1
              • erf/1
              • erfc/1
              • pi/0
              • e/0
              • epsilon/0
              • inf/0
              • nan/0
              • cputime/0
              • eval/1
              • msb/1
              • lsb/1
              • popcount/1
              • getbit/2
    • Packages
Availability:Arithmetic function (see is/2)
roundtoward(+Expr1, +RoundMode)
Evaluate Expr1 using the floating point rounding mode RoundMode. This provides a local alternative to the Prolog flag float_rounding. This function can be nested. The supported values for RoundMode are the same as the flag values: to_nearest, to_positive, to_negative or to_zero.

Note that floating point arithmetic is provided by the C compiler and C runtime library. Unfortunately most C libraries do not correctly implement the rounding modes for notably the trigonometry and exponential functions. There exist correct libraries such as crlibm, but these libraries are large, most of them are poorly maintained or have an incompatible license. C runtime libraries do a better job using the default to nearest rounding mode. SWI-Prolog now assumes this mode is correct and translates upward rounding to be the nexttoward/2 infinity and downward rounding nexttoward/2 -infinity. If the “to nearest'' rounding mode is correct, this ensures that the true value is between the downward and upward rounded values, although the generated interval is larger than needed. Unfortunately this is not the case as shown in Accuracy of Mathematical Functions in Single, Double, Extended Double and Quadruple Precision by Vincenzo Innocente and Paul Zimmermann.