Module arith

The default decimal number operations are unlimited precision. We provide here additional predicates mp_decimal/3, mp_add/3, mp_sub/3, mp_mul/3, mp_slash/3 and mp_int_pow/3 that provide limited precision basic arithmetic operations. The predicate mp_decimal/3 reduces the argument to the requested precision by using the proposed rounding. It can do this for integer, float and decimal numbers.

Examples:
?- X is mp_decimal(1<<10, new_context(3)).
X = 0d1.02E+3
?- X is mp_decimal(pi, new_context(3)). X = 0d3.14
?- X is mp_decimal(0d2.7183, new_context(3)). X = 0d2.72

The arithmetic predicates mp_add/3, mp_sub/3, mp_mul/3, mp_slash/3 and mp_int_pow/3 proceed in that they first round the given arguments to the requested precision. Then they compute the arithmetic operation up to the requested precision. The predicate mp_int_pow/3 internally computes with additional precision. Except for the argument and result rounding, they don’t introduce additional errors in the computation.

Examples:
?- X is mp_decimal(0d2.7183*0d2.7183, new_context(3)).
X = 0d7.39
?- X is mp_mul(0d2.7183, 0d2.7183, new_context(3)). X = 0d7.40

The arithmetic predicates fall back to the ordinary operations if none of the arguments are decimals and they might thus also produce integer and float results. There is one exception to this rule for the predicate mp_slash/3 which always produces a decimal.

The following arith predicates are provided:

mp_decimal(X, P, Z):
Predicate succeeds in Z with X converted to decimal with context P.
mp_add(X, Y, P, Z):
The predicate succeeds in Z with the addition of two number X and Y with some con-text P.
mp_sub(X, Y, P, Z):
The predicate succeeds in Z with the number X subtracted by the number Y with some context P.
mp_mul(X, Y, P, Z):
The predicate succeeds in Z with the multiplication of two numbers X and Y with some context P.
mp_slash(X, Y, P, Z):
The predicate succeeds in Z with the number X divided by the number Y with some context P.
mp_int_pow(X, N, P, Z):
The predicate succeeds in Z with the number X rose to the power of the integer N with some context P.

Comments