Functions with a universal_numeric parameter accept any kind of
number (such as float, integer, etc.).
The bit shifting operations which require a natural for the number of bits
to shift, and leading_part requires an integer as the second parameter.
GCC Ada Equivalent: numerics combines several Ada.Numerics child
packages and type attributes
Predefined numeric constants:
numerics.e
numerics.log2_e
numerics.log10_e
numerics.ln10 - log e 10
numerics.ln2 - log e 2
numerics.pi
numerics.pi_by_2 - pi / 2
numerics.pi_by_4 - pi / 4
numerics.pi_under_1 - 1 / pi
numerics.pi_under_2 - 2 / pi
numerics.sqrt_pi_under_2 - 2 / sqrt( pi ) )
numerics.sqrt_2 - sqrt( 2 )
numerics.sqrt_2_under_1 - 1 / sqrt(2)
The Ada complex number functions are not fully implemented.
Absolute value (abs) is not technically a part of the numerics package
(it's a language construct) but is documented here.
f := abs( e )
return the absolute value of e
Example
f := abs( e );
Parameters
Param
Mode
Type
Default
Description
e
in
universal_numeric
required
the expression to take the absolute value of
f
return value
universal_numeric
required
the result
Exceptions
-
See Also
-
Compare With
Ada: built-in abs function PHP: abs Python: abs/fabs
f := numerics.arccos( e [,cycle] )
trig inverse consine function
Example
f := numerics.arccos( e );
Parameters
Param
Mode
Type
Default
Description
e
in
universal_numeric
required
the expressions to take the arccos of
cycle
in
universal_numeric
numerics.pi*2
the trig period (eg. 360 for degrees), default is radians
f
return value
floating point
required
the result
Exceptions
an argument_error if the absolute value of e exceeds 1
Convert a float value to an integer by truncating the decimal part
and adding one. This is the smallest integer value that is greater than or
equal to Y.
Ada's normalized exponent attribute. It is used to break up a floating
point number into its exponent and fraction parts by giving the gross magnitude
of e.
Convert a float value to an integer by truncating the decimal part.
This is different from truncation. The function yields the largest (most
positive) integral value less than or equal to X. When X is zero, the result
has the sign of X; a zero result otherwise has a positive sign
Calculate a 32-bit number from string s that can be used with hash tables using a Fowler-Null-Vo / FNV-1a hash (used by DNS's). The return value is will be between 1 and the limit l.
Example
f := numerics.fnv_hash_of( "apple", 10 ); -- returns 1 to 10
Ada's fraction attribute. The function yields the value X*T'Machine_Radix**(-k), where k is the normalized exponent of X. It is used to break up a floating
point number into its exponent and fraction parts.
Calculate a 32-bit number from string s that can be used with hash tables using a common standard Bernstein / Knuth / djb2 hash. The return value is will be between 1 and the limit l.
Example
f := numerics.hash_of( "apple", 10 ); -- returns 1 to 10
Ada's leading part attribute. Let v be the value T'Machine_Radix**(k-Radix_Digits), where k is the normalized exponent of X. The function yields the value Floor(X/v)*v, when X is nonnegative and Radix_Digits is positive; Ceiling(X/v)*v, when X is negative and Radix_Digits is positive. Constraint_Error is raised when Radix_Digits is zero or negative. A zero result, which can only occur when X is zero, has the sign of X. This is used for high precision mathematics.
Example
f := numerics.leading_part( x, y );
Parameters
Param
Mode
Type
Default
Description
x,y
in
universal_numeric
required
the expressions
f
return value
universal_numeric
required
the result
Exceptions
-
See Also
-
Compare With
Ada: 'leading_part attribute
f := numerics.log( e [,b] )
logarithm, return a number is the exponent e by which another fixed value, the base b, has to be raised to produce that number. Without base b, the
natural logarithm is returned.
Example
f := numerics.log( e );
Parameters
Param
Mode
Type
Default
Description
e
in
universal_numeric
required
the expressions to take the log of
b
in
universal_numeric
epsilon (I think)
the base of the log
f
return value
universal_numeric
required
the result
Exceptions
an argument_error is raised if the base is negative, 0, 1 or if e is negative
a constraint_error is raised if e is zero
Ada's machine attribute. If e is a machine number of the type T, the function yields e; otherwise, it yields the value obtained by rounding or truncating e to either one of the adjacent machine numbers of the type T. A zero result has the sign of e when a machine has signed zeros.
Example
f := numerics.machine( 10.0 ); -- returns 10.0
Parameters
Param
Mode
Type
Default
Description
e
in
universal_numeric
required
the expression
f
return value
universal_numeric
required
the result
Exceptions
Constraint_Error is raised if rounding or truncating e to the precision of the machine numbers results in a value outside the base range of S.
Calculate a 32-bit number from string s that can be used with hash tables using a Murmur hash (used by memcached). The return value is will be between 1 and the limit l.
Example
f := numerics.murmur_hash_of( "apple", 10 ); -- returns 1 to 10
remainder of a floating point divide of x by y. According to the Ada Reference Manual: For nonzero Y, let v be the value X-n*Y, where n is the integer nearest to the exact value of X/Y; if |n-X/Y|=1/2, then n is chosen to be even. If v is a machine number of the type T, the function yields v; otherwise, it yields zero. Constraint_Error is raised if Y is zero. A zero result has the sign of X when a machine has signed zeros.
Example
f := numerics.remainder( 16.5, 2.0 ); -- return 0.5
Parameters
Param
Mode
Type
Default
Description
x
in
universal_numeric
required
the expression to divide
y
in
universal_numeric
required
the amount to divide by
f
return value
universal_numeric
required
the result
Exceptions
-
See Also
-
Compare With
Ada: 'remainder attribute
f := numerics.rounding( e )
Round a float value to an integer. Round to the farthest from zero
if exactly between two integers. That is, 5.5 will round up to 6 and 6.5
will round to 7.
multiply floating point number by 2 to the power of integer y. Let v be the value X*T'Machine_Radix**Adjustment. If v is a machine number of the type T, or if |v|>=T'Model_Small, The function yields v; otherwise, it yields either one of the machine numbers of the type T adjacent to v. A zero result has the sign of X when a machine has signed zeros. This is used for high precision mathamatics.
Example
f := numerics.scaling( 5.5, 3 ); -- returns 44
Parameters
Param
Mode
Type
Default
Description
x
in
universal_numeric
required
the expression
y
in
integer
required
the power to scale by
f
return value
universal_numeric
required
the result
Exceptions
Constraint_Error may be raised
See Also
-
Compare With
Ada: 'scaling attribute
f := numerics.sdbm_hash_of( s, l )
Calculate a 32-bit number from string s that can be used with hash tables using a Sleepycat / sdbm hash. The return value is will be between 1 and the limit l.
Example
f := numerics.sdbm_hash_of( "apple", 10 ); -- returns 1 to 10
return a natural integer, unique to this session. That is, the first invocation returns zero, the second returns one and so forth. The number returns to zero when it exceeds System.Max_Integer.
Example
f := numerics.serial;
Parameters
Param
Mode
Type
Default
Description
f
return value
natural
required
the serial number result
Exceptions
-
See Also
-
Compare With
-
i := numerics.shift_left( e, b )
shift expression e up to 64 bits left by natural b bits,
each step multiplying by two. The highest bits are discarded. The sign
bit will be overwritten.
Sturge's method: compute a grouping size for data with a low value of l, high value of h, and a sum total of t. This is used for setting the scale of graphs.
Example
f := numerics.sturges( 18, 64, 421 );
Parameters
Param
Mode
Type
Default
Description
l
in
universal_numeric
required
the lowest value
h
in
universal_numeric
required
the highest value
t
in
universal_numeric
required
the sum total
f
return value
universal_numeric
required
the result
Exceptions
-
See Also
-
Compare With
-
Implementation Note
This should probably be moved to the stats package.
f := numerics.tan( e [,cycle] )
trig tangent function
Example
f := numerics.tan( numerics.pi );
Parameters
Param
Mode
Type
Default
Description
e
in
universal_numeric
required
the expression to take the tangent of
cycle
in
universal_numeric
numerics.pi*2
the trig period (eg. 360 for degrees), default is radians
f
return value
floating point
required
the result
Exceptions
a constraint_error when e is an odd multiple of the quarter cycle
See Also
-
Compare With
Ada: Ada.Numerics.Generic_Elementary_Functions.tan PHP: tan Python: tan
Convert a float value to an integer by truncating the decimal part.
This is different from floor. The function yields the value ceiling(e) when
e is negative, and floor(e) otherwise. A zero result has the sign of e when
a machine has signed zeros.
Round a float value to an integer. Round to the nearest even integer
if exactly between two integers. That is, 5.5 will round up to 6 and 6.5
will round down to 6.
Example
f := numerics.unbiased_rouding( 7.5 ); -- returns 8