Arithmetic in YAP¶
YAP supports the following numeric types:
-
Tagged integers
YAP supports integers of up to almost the CPU word size: 28 bits on 32-bit machines, and 60-bits on 64-bit machines.The engine tags smaller integers are tagged so that they fit in a single word. These are the so called tagged integers.
- Large integers
Integers that cannot fit a tag, but who can fit a word are represented by adding a special functor, and a special end cell. The difference between these integers and tagged integers should be transparent to the programmer.
- Multiple Precision Integers
When YAP is built using the GNU multiple precision arithmetic library (GMP), integer arithmetic is unbounded, which means that the size of integers is only limited by available memory. The type of integer support can be detected using the Prolog flags bounded, min_integer and max_integer. As the use of GMP is default, most of the following descriptions assume unbounded integer arithmetic.
- Rational numbers (Q)
Rational numbers are quotients of two integers. Rational arithmetic is provided if GMP is used. Rational numbers that are returned from is_50 "is/2" are canonical, which means the denominator M is positive and that the numerator N and M have no common divisors. Rational numbers are introduced in the computation using the @ref rational_49 @"rational/1", @ref rationalize_49 @"rationalize/1" or the @ref rdiv_50 @"rdiv/2" (rational division) function.
- Floating point numbers
Floating point numbers are represented using the C-type double. On most today platforms these are 64-bit IEEE-754 floating point numbers. YAP now includes the built-in predicates @ref isinf_49 @"isinf/1" and to @ref isnan_49 @"isnan/1" tests.
Arithmetic functions that require integer arguments accept, in addition to integers, rational numbers with denominator 1 and floating point numbers that can be accurately converted to integers. If the required argument is a float the argument is converted to float. Note that conversion of integers to floating point numbers may raise an overflow exception. In all other cases, arguments are converted to the same type using the order integer to rational number to floating point number.
Evaluation generates the following Call exceptions:
(instantiation_error, Call )1 if not ground
(evaluable( V ), Call)` if not evaluable term
(integer( V ), Call)` if must be integer
(float( V ), Call)` if must be float
(out_of_range( V ), Call)` if argument invalid
(not_less_than_zero( V ), Call)` if argument must be positive or zero
(undefined( V ), Call)` result is not defined (nan)
(overflow( V ), Call)` result is arithmetic overflow
Define:¶
Functions:¶
1. yap_error_number Yap_MathException__(USES_REGS1):
1. void Yap_InitConstExps(void):
1. void Yap_InitUnaryExps(void):
1. void Yap_InitBinaryExps(void):
1. int Yap_ReInitConstExps(void):
1. int Yap_ReInitUnaryExps(void):
1. int Yap_ReInitBinaryExps(void):
1. Term Yap_eval_unary(Int, Term):
1. Term Yap_eval_binary(Int, Term, Term):
1. Term Yap_Eval__(Term t USES_REGS):
1. static void Yap_ClearExs(void):
1. static yap_error_number Yap_FoundArithError__(USES_REGS1):
1. static Term takeIndicator(Term t):
1. Atom Yap_NameOfUnaryOp(int i):
1. Atom Yap_NameOfBinaryOp(int i):
1. static expression_t ETypeOfTerm(Term t):
1. char * Yap_mpz_to_string(MP_INT b, char s, size_t sz, int base):
1. Term Yap_gmq_rdiv_int_int(Int, Int):
1. Term Yap_gmq_rdiv_int_big(Int, Term):
1. Term Yap_gmq_rdiv_big_int(Term, Int):
1. Term Yap_gmq_rdiv_big_big(Term, Term):
1. Term Yap_gmp_add_ints(Int, Int):
1. Term Yap_gmp_sub_ints(Int, Int):
1. Term Yap_gmp_mul_ints(Int, Int):
1. Term Yap_gmp_sll_ints(Int, Int):
1. Term Yap_gmp_add_int_big(Int, Term):
1. Term Yap_gmp_sub_int_big(Int, Term):
1. Term Yap_gmp_sub_big_int(Term, Int):
1. Term Yap_gmp_mul_int_big(Int, Term):
1. Term Yap_gmp_div_int_big(Int, Term):
1. Term Yap_gmp_div_big_int(Term, Int):
1. Term Yap_gmp_div2_big_int(Term, Int):
1. Term Yap_gmp_fdiv_int_big(Int, Term):
1. Term Yap_gmp_fdiv_big_int(Term, Int):
1. Term Yap_gmp_and_int_big(Int, Term):
1. Term Yap_gmp_ior_int_big(Int, Term):
1. Term Yap_gmp_xor_int_big(Int, Term):
1. Term Yap_gmp_sll_big_int(Term, Int):
1. Term Yap_gmp_add_big_big(Term, Term):
1. Term Yap_gmp_sub_big_big(Term, Term):
1. Term Yap_gmp_mul_big_big(Term, Term):
1. Term Yap_gmp_div_big_big(Term, Term):
1. Term Yap_gmp_div2_big_big(Term, Term):
1. Term Yap_gmp_fdiv_big_big(Term, Term):
1. Term Yap_gmp_and_big_big(Term, Term):
1. Term Yap_gmp_ior_big_big(Term, Term):
1. Term Yap_gmp_xor_big_big(Term, Term):
1. Term Yap_gmp_mod_big_big(Term, Term):
1. Term Yap_gmp_mod_big_int(Term, Int):
1. Term Yap_gmp_mod_int_big(Int, Term):
1. Term Yap_gmp_rem_big_big(Term, Term):
1. Term Yap_gmp_rem_big_int(Term, Int):
1. Term Yap_gmp_rem_int_big(Int, Term):
1. Term Yap_gmp_exp_int_int(Int, Int):
1. Term Yap_gmp_exp_int_big(Int, Term):
1. Term Yap_gmp_exp_big_int(Term, Int):
1. Term Yap_gmp_exp_big_big(Term, Term):
1. Term Yap_gmp_gcd_int_big(Int, Term):
1. Term Yap_gmp_gcd_big_big(Term, Term):
1. Term Yap_gmp_big_from_64bits(YAP_LONG_LONG):
1. Term Yap_gmp_float_to_big(Float):
1. Term Yap_gmp_float_to_rational(Float):
1. Term Yap_gmp_float_rationalize(Float):
1. Float Yap_gmp_to_float(Term):
1. Term Yap_gmp_add_float_big(Float, Term):
1. Term Yap_gmp_sub_float_big(Float, Term):
1. Term Yap_gmp_sub_big_float(Term, Float):
1. Term Yap_gmp_mul_float_big(Float, Term):
1. Term Yap_gmp_fdiv_float_big(Float, Term):
1. Term Yap_gmp_fdiv_big_float(Term, Float):
1. int Yap_gmp_cmp_big_int(Term, Int):
1. int Yap_gmp_cmp_int_big(Int, Term):
1. int Yap_gmp_cmp_big_float(Term, Float):
1. int Yap_gmp_cmp_big_big(Term, Term):
1. int Yap_gmp_tcmp_big_int(Term, Int):
1. int Yap_gmp_tcmp_int_big(Int, Term):
1. int Yap_gmp_tcmp_big_float(Term, Float):
1. int Yap_gmp_tcmp_big_big(Term, Term):
1. Term Yap_gmp_abs_big(Term):
1. Term Yap_gmp_neg_big(Term):
1. Term Yap_gmp_unot_big(Term):
1. Term Yap_gmp_ceiling(Term):
1. Term Yap_gmp_float_fractional_part(Term):
1. Term Yap_gmp_float_integer_part(Term):
1. Term Yap_gmp_popcount(Term):
1. char * Yap_gmp_to_string(Term, char *, size_t, int):
1. size_t Yap_gmp_to_size(Term, int):
1. int Yap_term_to_existing_big(Term, MP_INT *):
1. int Yap_term_to_existing_rat(Term, MP_RAT *):
1. void Yap_gmp_set_bit(Int i, Term t):
1. INLINE_ONLY Term __Yap_Mk64IntegerTerm(YAP_LONG_LONG USES_REGS):
1. INLINE_ONLY Term __Yap_Mk64IntegerTerm(YAP_LONG_LONG i USES_REGS):
1. static Term add_int(Int i, Int j USES_REGS):
1. Int Yap_msb(Int inp USES_REGS):
1. static Term p_plus(Term t1, Term t2 USES_REGS):
Typedef:¶
1. typedef enum etype_enum expression_t:
Enum:¶
1. arith0_op: : constant operators
specifies the available arithmetic`constants".
1. arith1_op: : unary operators
specifies the available unary arithmetic operators
1. arith2_op: : binary operators
specifies the available binary arithmetic operators