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.