Arithmetic Functions¶
YAP implements several arithmetic functions, they are defined as fields in three enumerations, such that there is one enumeration per each different arity:
These are the binary numeric operators currently supported by YAP.
- arith0op defines constants and arity 0 arithmetic functions
endif
- arith1op defines single argument arithmetic functions
endif
- arith2op defines binary arithmetic functions
endif
Arithmetic expressions in YAP may use the following operators:
{pi0}
- pi [ISO]
An approximation to the value of pi, that is, the ratio of a circle's circumference to its diameter.
{e0}
- e
Euler's number, the base of the natural logarithms.
<{#>epsilon</b><p> {#epsilon0}
-
The difference between the float
1.0
and the next largest floating point number. {inf0} -
inf
Infinity according to the IEEE Floating-Point standard. Note that evaluating this term will generate a domain error in the iso
language mode.
Note also that YAP supports +inf
and -inf
{nan0}
- nan (not a number)
Not-a-number according to the IEEE Floating-Point standard. Note that evaluating this term will generate a domain error in the iso
language mode.
- random
A pseudo-random floating point number between 0 and 1.
- signed/integer/random
A pseudo-random integer number with 32 bits.
- unsigned/integer/random
A pseudo-random unsigned integer number with 32 bits.
- cputime
CPU time since YAP was invoked, in seconds.
- heapused
Heap (data-base) space used, in bytes.
- local
- $b
- $env
- $tr
- $free/stack
Amount of free stack space, that is, free space between global and local stacks.
- global
Global stack in use, in bytes.
- exp( X) [ISO]
Natural exponential.
- log( X) [ISO]
Natural logarithm.
- log10( X)
- sqrt( X) [ISO]
- sin( X) [ISO]
- cos( X) [ISO]
- tan( X) [ISO]
- asin( X) [ISO]
- acos( X) [ISO]
- atan( X) [ISO]
- sinh( X)
- cosh( X)
- tanh( X)
- asinh( X)
- acosh( X)
- atanh( X)
- lgamma( X)
- erf( X)
- erfc( X)
Complementary gaussian error function.
- random( X) [ISO]
An integer random number between 0 and X.
In iso
language mode the argument must be a floating point-number, the result is an integer and it the float is equidistant it is rounded up, that is, to the least integer greater than X.
- integer( X)
If X evaluates to a float, the integer between the value of X and 0 closest to the value of X, else if X evaluates to an integer, the value of X.
- float( X) [ISO]
If X evaluates to an integer, the corresponding float, else the float itself.
- float_fractional_part( X) [ISO]
The fractional part of the floating point number X, or 0.0
if X is an integer. In the iso
language mode, X must be an integer.
- float_integer_part( X) [ISO]
The float giving the integer part of the floating point number X, or X if X is an integer. In the iso
language mode, X must be an integer.
- abs( X) [ISO]
- ceiling( X) [ISO]
The integer that is the smallest integral value not smaller than X.
In iso
language mode the argument must be a floating point-number and the result is an integer.
- floor( X) [ISO]
The integer that is the greatest integral value not greater than X.
In iso
language mode the argument must be a floating point-number and the result is an integer.
- round( X) [ISO]
The nearest integral value to X. If X is equidistant to two integers, it will be rounded to the closest even integral value.
In iso
language mode the argument must be a floating point-number, the result is an integer and it the float is equidistant it is rounded up, that is, to the least integer greater than X.
- sign( X) [ISO]
Return 1 if the X evaluates to a positive integer, 0 it if evaluates to 0, and -1 if it evaluates to a negative integer. If X evaluates to a floating-point number return 1.0 for a positive X, 0.0 for 0.0, and -1.0 otherwise.
- truncate( X) [ISO]
The integral value between X and 0 closest to X.
- rational( X)
Convert the expression X to a rational number or integer. The function returns the input on integers and rational numbers. For floating point numbers, the returned rational number exactly represents the float. As floats cannot exactly represent all decimal numbers the results may be surprising. In the examples below, doubles can represent 0.25
and the result is as expected, in contrast to the result of rational(0.1)
. The function @ref rationalize_49 @"rationalize/1"
gives a more intuitive result.
?- A is rational(0.25).
A is 1 rdiv 4
?- A is rational(0.1).
A = 3602879701896397 rdiv 36028797018963968
- rationalize( X)
Convert the expression X to a rational number or integer. The function is vvxu similar to @ref rational_49 "rational/1", but the result is only accurate within the rounding error of floating point numbers, generally producing a much smaller denominator.
?- A is rationalize(0.25).
A = 1 rdiv 4
?- A is rationalize(0.1).
A = 1 rdiv 10
- \ X [ISO]
Integer bitwise negation.
- msb( X)
The most significant bit of the non-negative integer X.
- lsb( X)
The least significant bit of the non-negative integer X.
- popcount( X)
The number of bits set to 1
in the binary representation of the non-negative integer X.
- [ X]
Evaluates to X for expression X. Useful because character strings in Prolog are lists of character codes.
X is Y*10+C- "0"
is the same as
X is Y*10+C-[48].
which would be evaluated as:
X is Y*10+C-48.
-
X+ Y [ISO] Addition, implemented between any two types of numbers
-
X- Y [ISO] Addition, implemented between any two types of numbers.
-
X/ Y [ISO] Integer quotient.
X mod Y [ISO] Integer module operator, always positive.
-
X rem Y [ISO] Integer remainder, similar to
mod
but always has the same sign asX
. -
- X div Y [ISO]*
Integer division, as if defined by
( _X_ - _X_ mod _Y_)// _Y_
.
- X div Y [ISO]*
Integer division, as if defined by
-
X ^ Y [ISO]
X raised to the power of Y, (from the C-Prolog syntax).
X raised to the power of Y (from ISO).
- X /\ Y [ISO]
Integer bitwise conjunction.
- X \/ Y [ISO]
Integer bitwise disjunction.
- X # Y
Integer bitwise exclusive disjunction.
- X >< Y
Integer bitwise exclusive disjunction.
-
xor( __X , Y) [ISO]__ Integer bitwise exclusive disjunction.
-
X << Y
Integer bitwise left logical shift of X by Y places.
- X > Y [ISO]
Integer bitwise right logical shift of X by Y places.
-
gcd( __X, Y) The greatest common divisor of the two integers __X and Y.
-
atan( __X, Y)__ Four-quadrant arc tangent. Also available as
@ref atan2_50 @"atan2/2"
.
Define:¶
Functions:¶
1. static Term float_to_int(Float v USES_REGS):
1. static Float get_float(Term t):
1. static double my_rint(double x):
1. static Int msb(Int inp USES_REGS):
1. Int Yap_msb(Int inp USES_REGS):
1. static Int lsb(Int inp USES_REGS):
1. static Int popcount(Int inp USES_REGS):
1. static Term eva/1(Int fi, Term t USES_REGS):
end of switch
1. Term Yap_eval_unary(Int f, Term t):
1. Atom Yap_NameOfUnOp(int i):
1. static Int p_unary_is(USES_REGS1):
1. static Int p_unary_op_as_integer(USES_REGS1):
1. static Int current_evaluable_property/1(USES_REGS1):
1. static Int is_evaluable_property/1(USES_REGS1):
1. void Yap_InitUnaryExps(void):
1. int Yap_ReInitUnaryExps(void):
1. static Term p_mod(Term t1, Term t2 USES_REGS):
1. static Term p_di/2(Term t1, Term t2 USES_REGS):
1. static Term p_rem(Term t1, Term t2 USES_REGS):
1. static Term p_rdiv(Term t1, Term t2 USES_REGS):
1. static Term p_fdiv(Term t1, Term t2 USES_REGS):
1. static Term p_ata/2(Term t1, Term t2 USES_REGS):
1. static Term p_power(Term t1, Term t2 USES_REGS):
1. static Term p_lo/2(Term t1, Term t2 USES_REGS):
1. static Int ipow(Int x, Int p):
1. static Term p_exp(Term t1, Term t2 USES_REGS):
1. static Int gcd(Int m11, Int m21 USES_REGS):
1. static Term p_gcd(Term t1, Term t2 USES_REGS):
1. static Term p_min(Term t1, Term t2):
1. static Term p_max(Term t1, Term t2):
1. static Term eva/2(Int fi, Term t1, Term t2 USES_REGS):
1. Term Yap_eval_binary(Int f, Term t1, Term t2):
1. static Int current_evaluable_property/2(USES_REGS1):
1. static Int is_evaluable_property/2(USES_REGS1):
1. static Int p_binary_is(USES_REGS1):
1. static bool do_arith/3(arith2_op op USES_REGS):
1. static Int export_p_plus(USES_REGS1):
1. static Int export_p_minus(USES_REGS1):
1. static Int export_p_times(USES_REGS1):
1. static Int export_p_div(USES_REGS1):
1. static Int export_p_and(USES_REGS1):
1. static Int export_p_or(USES_REGS1):
1. static Int export_p_xor(USES_REGS1):
1. static Int export_p_slr(USES_REGS1):
1. static Int export_p_sll(USES_REGS1):
1. static Int p_binary_op_as_integer(USES_REGS1):
1. Atom Yap_NameOfBinaryOp(int i):
1. void Yap_InitBinaryExps(void):
1. int Yap_ReInitBinaryExps(void):
Var:¶
1. InitBinEntry InitBinTab[][]: