[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 4. Functions on numbers

Each of the number classes declares its mathematical operations in the corresponding include file. For example, if your code operates with objects of type `cl_I`, it should `#include <cln/integer.h>`.

4.1 Constructing numbers |  |
4.2 Elementary functions |  |
4.3 Elementary rational functions |  |
4.4 Elementary complex functions |  |
4.5 Comparisons |  |
4.6 Rounding functions |  |
4.7 Roots |  |
4.8 Transcendental functions |  |
4.9 Functions on integers |  |
4.10 Functions on floating-point numbers |  |
4.11 Conversion functions |  |
4.12 Random number generators |  |
4.13 Obfuscating operators |  |

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 4.1 Constructing numbers

Here is how to create number objects "from nothing".

4.1.1 Constructing integers |  |
4.1.2 Constructing rational numbers |  |
4.1.3 Constructing floating-point numbers |  |
4.1.4 Constructing complex numbers |  |

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 4.1.1 Constructing integers

`cl_I` objects are most easily constructed from C integers and from strings. See 3.4 Conversions.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 4.1.2 Constructing rational numbers

`cl_RA` objects can be constructed from strings. The syntax for rational numbers is described in 5.1 Internal and printed representation. Another standard way to produce a rational number is through application of ``operator /`' or ``recip`' on integers.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 4.1.3 Constructing floating-point numbers

`cl_F` objects with low precision are most easily constructed from C ``float`' and ``double`'. See 3.4 Conversions.

To construct a `cl_F` with high precision, you can use the conversion from ``const char *`', but you have to specify the desired precision within the string. (See 5.1 Internal and printed representation.) Example:

       cl_F e = "0.271828182845904523536028747135266249775724709369996e+1_40";

will set ``e`' to the given value, with a precision of 40 decimal digits.

The programmatic way to construct a `cl_F` with high precision is through the `cl_float` conversion function, see 4.11.1 Conversion to floating-point numbers. For example, to compute `e` to 40 decimal places, first construct 1.0 to 40 decimal places and then apply the exponential function:

       float_format_t precision = float_format(40);
       cl_F e = exp(cl_float(1,precision));

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 4.1.4 Constructing complex numbers

Non-real `cl_N` objects are normally constructed through the function

       cl_N complex (const cl_R& realpart, const cl_R& imagpart)

See 4.4 Elementary complex functions.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 4.2 Elementary functions

Each of the classes `cl_N`, `cl_R`, `cl_RA`, `cl_I`, `cl_F`, `cl_SF`, `cl_FF`, `cl_DF`, `cl_LF` defines the following operations:

`type operator + (const type&, const type&)`
  Addition.

`type operator - (const type&, const type&)`
  Subtraction.

`type operator - (const type&)`
  Returns the negative of the argument.

`type plus1 (const type& x)`
  Returns `x + 1`.

`type minus1 (const type& x)`
  Returns `x - 1`.

`type operator * (const type&, const type&)`
  Multiplication.

`type square (const type& x)`
  Returns `x * x`.

Each of the classes `cl_N`, `cl_R`, `cl_RA`, `cl_F`, `cl_SF`, `cl_FF`, `cl_DF`, `cl_LF` defines the following operations:

`type operator / (const type&, const type&)`
  Division.

`type recip (const type&)`
  Returns the reciprocal of the argument.

The class `cl_I` doesn't define a ``/`' operation because in the C/C++ language this operator, applied to integral types, denotes the ``floor`' or ``truncate`' operation (which one of these, is implementation dependent). (See section 4.6 Rounding functions.) Instead, `cl_I` defines an "exact quotient" function:

`cl_I exquo (const cl_I& x, const cl_I& y)`
  Checks that `y` divides `x`, and returns the quotient `x`/`y`.

The following exponentiation functions are defined:

`cl_I expt_pos (const cl_I& x, const cl_I& y)`
`cl_RA expt_pos (const cl_RA& x, const cl_I& y)`
  `y` must be > 0. Returns `x^y`.

`cl_RA expt (const cl_RA& x, const cl_I& y)`
`cl_R expt (const cl_R& x, const cl_I& y)`
`cl_N expt (const cl_N& x, const cl_I& y)`
  Returns `x^y`.

Each of the classes `cl_R`, `cl_RA`, `cl_I`, `cl_F`, `cl_SF`, `cl_FF`, `cl_DF`, `cl_LF` defines the following operation:

`type abs (const type& x)`
  Returns the absolute value of `x`. This is `x` if `x >= 0`, and `-x` if `x <= 0`.

The class `cl_N` implements this as follows:

`cl_R abs (const cl_N x)`
  Returns the absolute value of `x`.

Each of the classes `cl_N`, `cl_R`, `cl_RA`, `cl_I`, `cl_F`, `cl_SF`, `cl_FF`, `cl_DF`, `cl_LF` defines the following operation:

`type signum (const type& x)`
  Returns the sign of `x`, in the same number format as `x`. This is defined as `x / abs(x)` if `x` is non-zero, and `x` if `x` is zero. If `x` is real, the value is either 0 or 1 or -1.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 4.3 Elementary rational functions

Each of the classes `cl_RA`, `cl_I` defines the following operations:

`cl_I numerator (const type& x)`
  Returns the numerator of `x`.

`cl_I denominator (const type& x)`
  Returns the denominator of `x`.

The numerator and denominator of a rational number are normalized in such a way that they have no factor in common and the denominator is positive.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 4.4 Elementary complex functions

The class `cl_N` defines the following operation:

`cl_N complex (const cl_R& a, const cl_R& b)`
  Returns the complex number `a+bi`, that is, the complex number with real part `a` and imaginary part `b`.

Each of the classes `cl_N`, `cl_R` defines the following operations:

`cl_R realpart (const type& x)`
  Returns the real part of `x`.

`cl_R imagpart (const type& x)`
  Returns the imaginary part of `x`.

`type conjugate (const type& x)`
  Returns the complex conjugate of `x`.

We have the relations

- `x = complex(realpart(x), imagpart(x))`
- `conjugate(x) = complex(realpart(x), -imagpart(x))`

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 4.5 Comparisons

Each of the classes `cl_N`, `cl_R`, `cl_RA`, `cl_I`, `cl_F`, `cl_SF`, `cl_FF`, `cl_DF`, `cl_LF` defines the following operations:

`bool operator == (const type&, const type&)`
`bool operator != (const type&, const type&)`
  Comparison, as in C and C++.

`uint32 equal_hashcode (const type&)`
  Returns a 32-bit hash code that is the same for any two numbers which are the same according to `==`. This hash code depends on the number's value, not its type or precision.

`cl_boolean zerop (const type& x)`
  Compare against zero: `x == 0`

Each of the classes `cl_R`, `cl_RA`, `cl_I`, `cl_F`, `cl_SF`, `cl_FF`, `cl_DF`, `cl_LF` defines the following operations:

`cl_signean compare (const type& x, const type& y)`
  Compares `x` and `y`. Returns +1 if `x`>`y`, -1 if `x`<`y`, 0 if `x`=`y`.

`bool operator <= (const type&, const type&)`
`bool operator < (const type&, const type&)`
`bool operator >= (const type&, const type&)`
`bool operator > (const type&, const type&)`
  Comparison, as in C and C++.

`cl_boolean minusp (const type& x)`
  Compare against zero: `x < 0`

`cl_boolean plusp (const type& x)`
  Compare against zero: `x > 0`

`type max (const type& x, const type& y)`
  Return the maximum of `x` and `y`.

`type min (const type& x, const type& y)`
  Return the minimum of `x` and `y`.

When a floating point number and a rational number are compared, the float is first converted to a rational number using the function `rational`. Since a floating point number actually represents an interval of real numbers, the result might be surprising. For example, `(cl_F)(cl_R)"1/3" == (cl_R)"1/3"` returns false because there is no floating point number whose value is exactly `1/3`.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 4.6 Rounding functions

When a real number is to be converted to an integer, there is no "best" rounding. The desired rounding function depends on the application. The Common Lisp and ISO Lisp standards offer four rounding functions:

`floor(x)`
  This is the largest integer <=`x`.

`ceiling(x)`
  This is the smallest integer >=`x`.

`truncate(x)`
  Among the integers between 0 and `x` (inclusive) the one nearest to `x`.

`round(x)`
  The integer nearest to `x`. If `x` is exactly halfway between two integers, choose the even one.

These functions have different advantages:

`floor` and `ceiling` are translation invariant: `floor(x+n) = floor(x) + n` and `ceiling(x+n) = ceiling(x) + n` for every `x` and every integer `n`.

On the other hand, `truncate` and `round` are symmetric: `truncate(-x) = -truncate(x)` and `round(-x) = -round(x)`, and furthermore `round` is unbiased: on the "average", it rounds down exactly as often as it rounds up.

The functions are related like this:

- `ceiling(m/n) = floor((m+n-1)/n) = floor((m-1)/n)+1` for rational numbers `m/n` (`m`, `n` integers, `n`>0), and
- `truncate(x) = sign(x) * floor(abs(x))`

Each of the classes `cl_R`, `cl_RA`, `cl_F`, `cl_SF`, `cl_FF`, `cl_DF`, `cl_LF` defines the following operations:

`cl_I floor1 (const type& x)`
  Returns `floor(x)`.
`cl_I ceiling1 (const type& x)`
  Returns `ceiling(x)`.
`cl_I truncate1 (const type& x)`
  Returns `truncate(x)`.
`cl_I round1 (const type& x)`
  Returns `round(x)`.

Each of the classes `cl_R`, `cl_RA`, `cl_I`, `cl_F`, `cl_SF`, `cl_FF`, `cl_DF`, `cl_LF` defines the following operations:

`cl_I floor1 (const type& x, const type& y)`
  Returns `floor(x/y)`.
`cl_I ceiling1 (const type& x, const type& y)`
  Returns `ceiling(x/y)`.
`cl_I truncate1 (const type& x, const type& y)`
  Returns `truncate(x/y)`.
`cl_I round1 (const type& x, const type& y)`
  Returns `round(x/y)`.

These functions are called ``floor1`', ... here instead of ``floor`', ..., because on some systems, system dependent include files define ``floor`' and ``ceiling`' as macros.

In many cases, one needs both the quotient and the remainder of a division. It is more efficient to compute both at the same time than to perform two divisions, one for quotient and the next one for the remainder. The following functions therefore return a structure containing both the quotient and the remainder. The suffix ``2`' indicates the number of "return values". The remainder is defined as follows:

- for the computation of `quotient = floor(x)`, `remainder = x - quotient`,
- for the computation of `quotient = floor(x,y)`, `remainder = x - quotient*y`,

and similarly for the other three operations.

Each of the classes `cl_R`, `cl_RA`, `cl_F`, `cl_SF`, `cl_FF`, `cl_DF`, `cl_LF` defines the following operations:

`struct type_div_t { cl_I quotient; type remainder; };`
`type_div_t floor2 (const type& x)`
`type_div_t ceiling2 (const type& x)`
`type_div_t truncate2 (const type& x)`
`type_div_t round2 (const type& x)`

Each of the classes `cl_R`, `cl_RA`, `cl_I`, `cl_F`, `cl_SF`, `cl_FF`, `cl_DF`, `cl_LF` defines the following operations:

`struct type_div_t { cl_I quotient; type remainder; };`
`type_div_t floor2 (const type& x, const type& y)`
`type_div_t ceiling2 (const type& x, const type& y)`
`type_div_t truncate2 (const type& x, const type& y)`
`type_div_t round2 (const type& x, const type& y)`

Sometimes, one wants the quotient as a floating-point number (of the same format as the argument, if the argument is a float) instead of as an integer. The prefix ``f`' indicates this.

Each of the classes `cl_F`, `cl_SF`, `cl_FF`, `cl_DF`, `cl_LF` defines the following operations:

`type ffloor (const type& x)`
`type fceiling (const type& x)`
`type ftruncate (const type& x)`
`type fround (const type& x)`

and similarly for class `cl_R`, but with return type `cl_F`.

The class `cl_R` defines the following operations:

`cl_F ffloor (const type& x, const type& y)`
`cl_F fceiling (const type& x, const type& y)`
`cl_F ftruncate (const type& x, const type& y)`
`cl_F fround (const type& x, const type& y)`

These functions also exist in versions which return both the quotient and the remainder. The suffix ``2`' indicates this.

Each of the classes `cl_F`, `cl_SF`, `cl_FF`, `cl_DF`, `cl_LF` defines the following operations:

`struct type_fdiv_t { type quotient; type remainder; };`
`type_fdiv_t ffloor2 (const type& x)`
`type_fdiv_t fceiling2 (const type& x)`
`type_fdiv_t ftruncate2 (const type& x)`
`type_fdiv_t fround2 (const type& x)`

and similarly for class `cl_R`, but with quotient type `cl_F`.

The class `cl_R` defines the following operations:

`struct type_fdiv_t { cl_F quotient; cl_R remainder; };`
`type_fdiv_t ffloor2 (const type& x, const type& y)`
`type_fdiv_t fceiling2 (const type& x, const type& y)`
`type_fdiv_t ftruncate2 (const type& x, const type& y)`
`type_fdiv_t fround2 (const type& x, const type& y)`

Other applications need only the remainder of a division. The remainder of ``floor`' and ``ffloor`' is called ``mod`' (abbreviation of "modulo"). The remainder ``truncate`' and ``ftruncate`' is called ``rem`' (abbreviation of "remainder").

- `mod(x,y) = floor2(x,y).remainder = x - floor(x/y)*y`
- `rem(x,y) = truncate2(x,y).remainder = x - truncate(x/y)*y`

If `x` and `y` are both >= 0, `mod(x,y) = rem(x,y) >= 0`. In general, `mod(x,y)` has the sign of `y` or is zero, and `rem(x,y)` has the sign of `x` or is zero.

The classes `cl_R`, `cl_I` define the following operations:

`type mod (const type& x, const type& y)`
`type rem (const type& x, const type& y)`

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 4.7 Roots

Each of the classes `cl_R`, `cl_F`, `cl_SF`, `cl_FF`, `cl_DF`, `cl_LF` defines the following operation:

`type sqrt (const type& x)`
  `x` must be >= 0. This function returns the square root of `x`, normalized to be >= 0. If `x` is the square of a rational number, `sqrt(x)` will be a rational number, else it will return a floating-point approximation.

The classes `cl_RA`, `cl_I` define the following operation:

`cl_boolean sqrtp (const type& x, type* root)`
  This tests whether `x` is a perfect square. If so, it returns true and the exact square root in `*root`, else it returns false.

Furthermore, for integers, similarly:

`cl_boolean isqrt (const type& x, type* root)`
  `x` should be >= 0. This function sets `*root` to `floor(sqrt(x))` and returns the same value as `sqrtp`: the boolean value `(expt(*root,2) == x)`.

For `n`th roots, the classes `cl_RA`, `cl_I` define the following operation:

`cl_boolean rootp (const type& x, const cl_I& n, type* root)`
  `x` must be >= 0. `n` must be > 0. This tests whether `x` is an `n`th power of a rational number. If so, it returns true and the exact root in `*root`, else it returns false.

The only square root function which accepts negative numbers is the one for class `cl_N`:

`cl_N sqrt (const cl_N& z)`
  Returns the square root of `z`, as defined by the formula `sqrt(z) = exp(log(z)/2)`. Conversion to a floating-point type or to a complex number are done if necessary. The range of the result is the right half plane `realpart(sqrt(z)) >= 0` including the positive imaginary axis and 0, but excluding the negative imaginary axis. The result is an exact number only if `z` is an exact number.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 4.8 Transcendental functions

The transcendental functions return an exact result if the argument is exact and the result is exact as well. Otherwise they must return inexact numbers even if the argument is exact. For example, `cos(0) = 1` returns the rational number `1`.

4.8.1 Exponential and logarithmic functions |  |
4.8.2 Trigonometric functions |  |
4.8.3 Hyperbolic functions |  |
4.8.4 Euler gamma |  |
4.8.5 Riemann zeta |  |

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 4.8.1 Exponential and logarithmic functions

`cl_R exp (const cl_R& x)`
`cl_N exp (const cl_N& x)`
  Returns the exponential function of `x`. This is `e^x` where `e` is the base of the natural logarithms. The range of the result is the entire complex plane excluding 0.

`cl_R ln (const cl_R& x)`
  `x` must be > 0. Returns the (natural) logarithm of x.

`cl_N log (const cl_N& x)`
  Returns the (natural) logarithm of x. If `x` is real and positive, this is `ln(x)`. In general, `log(x) = log(abs(x)) + i*phase(x)`. The range of the result is the strip in the complex plane `-pi < imagpart(log(x)) <= pi`.

`cl_R phase (const cl_N& x)`
  Returns the angle part of `x` in its polar representation as a complex number. That is, `phase(x) = atan(realpart(x),imagpart(x))`. This is also the imaginary part of `log(x)`. The range of the result is the interval `-pi < phase(x) <= pi`. The result will be an exact number only if `zerop(x)` or if `x` is real and positive.

`cl_R log (const cl_R& a, const cl_R& b)`
  `a` and `b` must be > 0. Returns the logarithm of `a` with respect to base `b`. `log(a,b) = ln(a)/ln(b)`. The result can be exact only if `a = 1` or if `a` and `b` are both rational.

`cl_N log (const cl_N& a, const cl_N& b)`
  Returns the logarithm of `a` with respect to base `b`. `log(a,b) = log(a)/log(b)`.

`cl_N expt (const cl_N& x, const cl_N& y)`
  Exponentiation: Returns `x^y = exp(y*log(x))`.

The constant e = exp(1) = 2.71828... is returned by the following functions:

`cl_F exp1 (float_format_t f)`
  Returns e as a float of format `f`.

`cl_F exp1 (const cl_F& y)`
  Returns e in the float format of `y`.

`cl_F exp1 (void)`
  Returns e as a float of format `default_float_format`.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 4.8.2 Trigonometric functions

`cl_R sin (const cl_R& x)`
  Returns `sin(x)`. The range of the result is the interval `-1 <= sin(x) <= 1`.

`cl_N sin (const cl_N& z)`
  Returns `sin(z)`. The range of the result is the entire complex plane.

`cl_R cos (const cl_R& x)`
  Returns `cos(x)`. The range of the result is the interval `-1 <= cos(x) <= 1`.

`cl_N cos (const cl_N& x)`
  Returns `cos(z)`. The range of the result is the entire complex plane.

`struct cos_sin_t { cl_R cos; cl_R sin; };`
`cos_sin_t cos_sin (const cl_R& x)`
  Returns both `sin(x)` and `cos(x)`. This is more efficient than computing them separately. The relation `cos^2 + sin^2 = 1` will hold only approximately.

`cl_R tan (const cl_R& x)`
`cl_N tan (const cl_N& x)`
  Returns `tan(x) = sin(x)/cos(x)`.

`cl_N cis (const cl_R& x)`
`cl_N cis (const cl_N& x)`
  Returns `exp(i*x)`. The name ``cis`' means "cos + i sin", because `e^(i*x) = cos(x) + i*sin(x)`.

`cl_N asin (const cl_N& z)`
  Returns `arcsin(z)`. This is defined as `arcsin(z) = log(iz+sqrt(1-z^2))/i` and satisfies `arcsin(-z) = -arcsin(z)`. The range of the result is the strip in the complex domain `-pi/2 <= realpart(arcsin(z)) <= pi/2`, excluding the numbers with `realpart = -pi/2` and `imagpart < 0` and the numbers with `realpart = pi/2` and `imagpart > 0`.

`cl_N acos (const cl_N& z)`
  Returns `arccos(z)`. This is defined as `arccos(z) = pi/2 - arcsin(z) = log(z+i*sqrt(1-z^2))/i` and satisfies `arccos(-z) = pi - arccos(z)`. The range of the result is the strip in the complex domain `0 <= realpart(arcsin(z)) <= pi`, excluding the numbers with `realpart = 0` and `imagpart < 0` and the numbers with `realpart = pi` and `imagpart > 0`.

`cl_R atan (const cl_R& x, const cl_R& y)`
  Returns the angle of the polar representation of the complex number `x+iy`. This is `atan(y/x)` if `x>0`. The range of the result is the interval `-pi < atan(x,y) <= pi`. The result will be an exact number only if `x > 0` and `y` is the exact `0`. WARNING: In Common Lisp, this function is called as `(atan y x)`, with reversed order of arguments.

`cl_R atan (const cl_R& x)`
  Returns `arctan(x)`. This is the same as `atan(1,x)`. The range of the result is the interval `-pi/2 < atan(x) < pi/2`. The result will be an exact number only if `x` is the exact `0`.

`cl_N atan (const cl_N& z)`
  Returns `arctan(z)`. This is defined as `arctan(z) = (log(1+iz)-log(1-iz)) / 2i` and satisfies `arctan(-z) = -arctan(z)`. The range of the result is the strip in the complex domain `-pi/2 <= realpart(arctan(z)) <= pi/2`, excluding the numbers with `realpart = -pi/2` and `imagpart >= 0` and the numbers with `realpart = pi/2` and `imagpart <= 0`.

Archimedes' constant pi = 3.14... is returned by the following functions:

`cl_F pi (float_format_t f)`
  Returns pi as a float of format `f`.

`cl_F pi (const cl_F& y)`
  Returns pi in the float format of `y`.

`cl_F pi (void)`
  Returns pi as a float of format `default_float_format`.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 4.8.3 Hyperbolic functions

`cl_R sinh (const cl_R& x)`
  Returns `sinh(x)`.

`cl_N sinh (const cl_N& z)`
  Returns `sinh(z)`. The range of the result is the entire complex plane.

`cl_R cosh (const cl_R& x)`
  Returns `cosh(x)`. The range of the result is the interval `cosh(x) >= 1`.

`cl_N cosh (const cl_N& z)`
  Returns `cosh(z)`. The range of the result is the entire complex plane.

`struct cosh_sinh_t { cl_R cosh; cl_R sinh; };`
`cosh_sinh_t cosh_sinh (const cl_R& x)`
  Returns both `sinh(x)` and `cosh(x)`. This is more efficient than computing them separately. The relation `cosh^2 - sinh^2 = 1` will hold only approximately.

`cl_R tanh (const cl_R& x)`
`cl_N tanh (const cl_N& x)`
  Returns `tanh(x) = sinh(x)/cosh(x)`.

`cl_N asinh (const cl_N& z)`
  Returns `arsinh(z)`. This is defined as `arsinh(z) = log(z+sqrt(1+z^2))` and satisfies `arsinh(-z) = -arsinh(z)`. The range of the result is the strip in the complex domain `-pi/2 <= imagpart(arsinh(z)) <= pi/2`, excluding the numbers with `imagpart = -pi/2` and `realpart > 0` and the numbers with `imagpart = pi/2` and `realpart < 0`.

`cl_N acosh (const cl_N& z)`
  Returns `arcosh(z)`. This is defined as `arcosh(z) = 2*log(sqrt((z+1)/2)+sqrt((z-1)/2))`. The range of the result is the half-strip in the complex domain `-pi < imagpart(arcosh(z)) <= pi, realpart(arcosh(z)) >= 0`, excluding the numbers with `realpart = 0` and `-pi < imagpart < 0`.

`cl_N atanh (const cl_N& z)`
  Returns `artanh(z)`. This is defined as `artanh(z) = (log(1+z)-log(1-z)) / 2` and satisfies `artanh(-z) = -artanh(z)`. The range of the result is the strip in the complex domain `-pi/2 <= imagpart(artanh(z)) <= pi/2`, excluding the numbers with `imagpart = -pi/2` and `realpart <= 0` and the numbers with `imagpart = pi/2` and `realpart >= 0`.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 4.8.4 Euler gamma

Euler's constant C = 0.577... is returned by the following functions:

`cl_F eulerconst (float_format_t f)`
  Returns Euler's constant as a float of format `f`.

`cl_F eulerconst (const cl_F& y)`
  Returns Euler's constant in the float format of `y`.

`cl_F eulerconst (void)`
  Returns Euler's constant as a float of format `default_float_format`.

Catalan's constant G = 0.915... is returned by the following functions:

`cl_F catalanconst (float_format_t f)`
  Returns Catalan's constant as a float of format `f`.

`cl_F catalanconst (const cl_F& y)`
  Returns Catalan's constant in the float format of `y`.

`cl_F catalanconst (void)`
  Returns Catalan's constant as a float of format `default_float_format`.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 4.8.5 Riemann zeta

Riemann's zeta function at an integral point `s>1` is returned by the following functions:

`cl_F zeta (int s, float_format_t f)`
  Returns Riemann's zeta function at `s` as a float of format `f`.

`cl_F zeta (int s, const cl_F& y)`
  Returns Riemann's zeta function at `s` in the float format of `y`.

`cl_F zeta (int s)`
  Returns Riemann's zeta function at `s` as a float of format `default_float_format`.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 4.9 Functions on integers

4.9.1 Logical functions |  |
4.9.2 Number theoretic functions |  |
4.9.3 Combinatorial functions |  |

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 4.9.1 Logical functions

Integers, when viewed as in two's complement notation, can be thought as infinite bit strings where the bits' values eventually are constant. For example,

        17 = ......00010001
        -6 = ......11111010

The logical operations view integers as such bit strings and operate on each of the bit positions in parallel.

`cl_I lognot (const cl_I& x)`
`cl_I operator ~ (const cl_I& x)`
  Logical not, like `~x` in C. This is the same as `-1-x`.

`cl_I logand (const cl_I& x, const cl_I& y)`
`cl_I operator & (const cl_I& x, const cl_I& y)`
  Logical and, like `x & y` in C.

`cl_I logior (const cl_I& x, const cl_I& y)`
`cl_I operator | (const cl_I& x, const cl_I& y)`
  Logical (inclusive) or, like `x | y` in C.

`cl_I logxor (const cl_I& x, const cl_I& y)`
`cl_I operator ^ (const cl_I& x, const cl_I& y)`
  Exclusive or, like `x ^ y` in C.

`cl_I logeqv (const cl_I& x, const cl_I& y)`
  Bitwise equivalence, like `~(x ^ y)` in C.

`cl_I lognand (const cl_I& x, const cl_I& y)`
  Bitwise not and, like `~(x & y)` in C.

`cl_I lognor (const cl_I& x, const cl_I& y)`
  Bitwise not or, like `~(x | y)` in C.

`cl_I logandc1 (const cl_I& x, const cl_I& y)`
  Logical and, complementing the first argument, like `~x & y` in C.

`cl_I logandc2 (const cl_I& x, const cl_I& y)`
  Logical and, complementing the second argument, like `x & ~y` in C.

`cl_I logorc1 (const cl_I& x, const cl_I& y)`
  Logical or, complementing the first argument, like `~x | y` in C.

`cl_I logorc2 (const cl_I& x, const cl_I& y)`
  Logical or, complementing the second argument, like `x | ~y` in C.

These operations are all available though the function

`cl_I boole (cl_boole op, const cl_I& x, const cl_I& y)`

where `op` must have one of the 16 values (each one stands for a function which combines two bits into one bit): `boole_clr`, `boole_set`, `boole_1`, `boole_2`, `boole_c1`, `boole_c2`, `boole_and`, `boole_ior`, `boole_xor`, `boole_eqv`, `boole_nand`, `boole_nor`, `boole_andc1`, `boole_andc2`, `boole_orc1`, `boole_orc2`.

Other functions that view integers as bit strings:

`cl_boolean logtest (const cl_I& x, const cl_I& y)`
  Returns true if some bit is set in both `x` and `y`, i.e. if `logand(x,y) != 0`.

`cl_boolean logbitp (const cl_I& n, const cl_I& x)`
  Returns true if the `n`th bit (from the right) of `x` is set. Bit 0 is the least significant bit.

`uintL logcount (const cl_I& x)`
  Returns the number of one bits in `x`, if `x` >= 0, or the number of zero bits in `x`, if `x` < 0.

The following functions operate on intervals of bits in integers. The type

    struct cl_byte { uintL size; uintL position; };

represents the bit interval containing the bits `position`...`position+size-1` of an integer. The constructor `cl_byte(size,position)` constructs a `cl_byte`.

`cl_I ldb (const cl_I& n, const cl_byte& b)`
  extracts the bits of `n` described by the bit interval `b` and returns them as a nonnegative integer with `b.size` bits.

`cl_boolean ldb_test (const cl_I& n, const cl_byte& b)`
  Returns true if some bit described by the bit interval `b` is set in `n`.

`cl_I dpb (const cl_I& newbyte, const cl_I& n, const cl_byte& b)`
  Returns `n`, with the bits described by the bit interval `b` replaced by `newbyte`. Only the lowest `b.size` bits of `newbyte` are relevant.

The functions `ldb` and `dpb` implicitly shift. The following functions are their counterparts without shifting:

`cl_I mask_field (const cl_I& n, const cl_byte& b)`
  returns an integer with the bits described by the bit interval `b` copied from the corresponding bits in `n`, the other bits zero.

`cl_I deposit_field (const cl_I& newbyte, const cl_I& n, const cl_byte& b)`
  returns an integer where the bits described by the bit interval `b` come from `newbyte` and the other bits come from `n`.

The following relations hold:

- `ldb (n, b) = mask_field(n, b) >> b.position`,
- `dpb (newbyte, n, b) = deposit_field (newbyte << b.position, n, b)`,
- `deposit_field(newbyte,n,b) = n ^ mask_field(n,b) ^ mask_field(new_byte,b)`.

The following operations on integers as bit strings are efficient shortcuts for common arithmetic operations:

`cl_boolean oddp (const cl_I& x)`
  Returns true if the least significant bit of `x` is 1. Equivalent to `mod(x,2) != 0`.

`cl_boolean evenp (const cl_I& x)`
  Returns true if the least significant bit of `x` is 0. Equivalent to `mod(x,2) == 0`.

`cl_I operator << (const cl_I& x, const cl_I& n)`
  Shifts `x` by `n` bits to the left. `n` should be >=0. Equivalent to `x * expt(2,n)`.

`cl_I operator >> (const cl_I& x, const cl_I& n)`
  Shifts `x` by `n` bits to the right. `n` should be >=0. Bits shifted out to the right are thrown away. Equivalent to `floor(x / expt(2,n))`.

`cl_I ash (const cl_I& x, const cl_I& y)`
  Shifts `x` by `y` bits to the left (if `y`>=0) or by `-y` bits to the right (if `y`<=0). In other words, this returns `floor(x * expt(2,y))`.

`uintL integer_length (const cl_I& x)`
  Returns the number of bits (excluding the sign bit) needed to represent `x` in two's complement notation. This is the smallest n >= 0 such that -2^n <= x < 2^n. If x > 0, this is the unique n > 0 such that 2^(n-1) <= x < 2^n.

`uintL ord2 (const cl_I& x)`
  `x` must be non-zero. This function returns the number of 0 bits at the right of `x` in two's complement notation. This is the largest n >= 0 such that 2^n divides `x`.

`uintL power2p (const cl_I& x)`
  `x` must be > 0. This function checks whether `x` is a power of 2. If `x` = 2^(n-1), it returns n. Else it returns 0. (See also the function `logp`.)

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 4.9.2 Number theoretic functions

`uint32 gcd (uint32 a, uint32 b)`
`cl_I gcd (const cl_I& a, const cl_I& b)`
  This function returns the greatest common divisor of `a` and `b`, normalized to be >= 0.

`cl_I xgcd (const cl_I& a, const cl_I& b, cl_I* u, cl_I* v)`
  This function ("extended gcd") returns the greatest common divisor `g` of `a` and `b` and at the same time the representation of `g` as an integral linear combination of `a` and `b`: `u` and `v` with `u*a+v*b = g`, `g` >= 0. `u` and `v` will be normalized to be of smallest possible absolute value, in the following sense: If `a` and `b` are non-zero, and `abs(a) != abs(b)`, `u` and `v` will satisfy the inequalities `abs(u) <= abs(b)/(2*g)`, `abs(v) <= abs(a)/(2*g)`.

`cl_I lcm (const cl_I& a, const cl_I& b)`
  This function returns the least common multiple of `a` and `b`, normalized to be >= 0.

`cl_boolean logp (const cl_I& a, const cl_I& b, cl_RA* l)`
`cl_boolean logp (const cl_RA& a, const cl_RA& b, cl_RA* l)`
  `a` must be > 0. `b` must be >0 and != 1. If log(a,b) is rational number, this function returns true and sets *l = log(a,b), else it returns false.

`int jacobi (sint32 a, sint32 b)`
`int jacobi (const cl_I& a, const cl_I& b)`
  Returns the Jacobi symbol (a/b), `a,b` must be integers, `b>0` and odd. The result is 0 iff gcd(a,b)>1.

`cl_boolean isprobprime (const cl_I& n)`
  Returns true if `n` is a small prime or passes the Miller-Rabin primality test. The probability of a false positive is 1:10^30.

`cl_I nextprobprime (const cl_R& x)`
  Returns the smallest probable prime >=`x`.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 4.9.3 Combinatorial functions

`cl_I factorial (uintL n)`
  `n` must be a small integer >= 0. This function returns the factorial `n`! = `1*2*...*n`.

`cl_I doublefactorial (uintL n)`
  `n` must be a small integer >= 0. This function returns the doublefactorial `n`!! = `1*3*...*n` or `n`!! = `2*4*...*n`, respectively.

`cl_I binomial (uintL n, uintL k)`
  `n` and `k` must be small integers >= 0. This function returns the binomial coefficient (`n` choose `k`) = `n`! / `k`! `(n-k)`! for 0 <= k <= n, 0 else.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 4.10 Functions on floating-point numbers

Recall that a floating-point number consists of a sign `s`, an exponent `e` and a mantissa `m`. The value of the number is `(-1)^s * 2^e * m`.

Each of the classes `cl_F`, `cl_SF`, `cl_FF`, `cl_DF`, `cl_LF` defines the following operations.

`type scale_float (const type& x, sintL delta)`
`type scale_float (const type& x, const cl_I& delta)`
  Returns `x*2^delta`. This is more efficient than an explicit multiplication because it copies `x` and modifies the exponent.

The following functions provide an abstract interface to the underlying representation of floating-point numbers.

`sintL float_exponent (const type& x)`
  Returns the exponent `e` of `x`. For `x = 0.0`, this is 0. For `x` non-zero, this is the unique integer with `2^(e-1) <= abs(x) < 2^e`.

`sintL float_radix (const type& x)`
  Returns the base of the floating-point representation. This is always `2`.

`type float_sign (const type& x)`
  Returns the sign `s` of `x` as a float. The value is 1 for `x` >= 0, -1 for `x` < 0.

`uintL float_digits (const type& x)`
  Returns the number of mantissa bits in the floating-point representation of `x`, including the hidden bit. The value only depends on the type of `x`, not on its value.

`uintL float_precision (const type& x)`
  Returns the number of significant mantissa bits in the floating-point representation of `x`. Since denormalized numbers are not supported, this is the same as `float_digits(x)` if `x` is non-zero, and 0 if `x` = 0.

The complete internal representation of a float is encoded in the type `decoded_float` (or `decoded_sfloat`, `decoded_ffloat`, `decoded_dfloat`, `decoded_lfloat`, respectively), defined by

    struct decoded_typefloat {
            type mantissa; cl_I exponent; type sign;
    };

and returned by the function

`decoded_typefloat decode_float (const type& x)`
  For `x` non-zero, this returns `(-1)^s`, `e`, `m` with `x = (-1)^s * 2^e * m` and `0.5 <= m < 1.0`. For `x` = 0, it returns `(-1)^s`=1, `e`=0, `m`=0. `e` is the same as returned by the function `float_exponent`.

A complete decoding in terms of integers is provided as type

    struct cl_idecoded_float {
            cl_I mantissa; cl_I exponent; cl_I sign;
    };

by the following function:

`cl_idecoded_float integer_decode_float (const type& x)`
  For `x` non-zero, this returns `(-1)^s`, `e`, `m` with `x = (-1)^s * 2^e * m` and `m` an integer with `float_digits(x)` bits. For `x` = 0, it returns `(-1)^s`=1, `e`=0, `m`=0. WARNING: The exponent `e` is not the same as the one returned by the functions `decode_float` and `float_exponent`.

Some other function, implemented only for class `cl_F`:

`cl_F float_sign (const cl_F& x, const cl_F& y)`
  This returns a floating point number whose precision and absolute value is that of `y` and whose sign is that of `x`. If `x` is zero, it is treated as positive. Same for `y`.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 4.11 Conversion functions

4.11.1 Conversion to floating-point numbers |  |
4.11.2 Conversion to rational numbers |  |

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 4.11.1 Conversion to floating-point numbers

The type `float_format_t` describes a floating-point format.

`float_format_t float_format (uintL n)`
  Returns the smallest float format which guarantees at least `n` decimal digits in the mantissa (after the decimal point).

`float_format_t float_format (const cl_F& x)`
  Returns the floating point format of `x`.

`float_format_t default_float_format`
  Global variable: the default float format used when converting rational numbers to floats.

To convert a real number to a float, each of the types `cl_R`, `cl_F`, `cl_I`, `cl_RA`, `int`, `unsigned int`, `float`, `double` defines the following operations:

`cl_F cl_float (const type&x, float_format_t f)`
  Returns `x` as a float of format `f`.
`cl_F cl_float (const type&x, const cl_F& y)`
  Returns `x` in the float format of `y`.
`cl_F cl_float (const type&x)`
  Returns `x` as a float of format `default_float_format` if it is an exact number, or `x` itself if it is already a float.

Of course, converting a number to a float can lose precision.

Every floating-point format has some characteristic numbers:

`cl_F most_positive_float (float_format_t f)`
  Returns the largest (most positive) floating point number in float format `f`.

`cl_F most_negative_float (float_format_t f)`
  Returns the smallest (most negative) floating point number in float format `f`.

`cl_F least_positive_float (float_format_t f)`
  Returns the least positive floating point number (i.e. > 0 but closest to 0) in float format `f`.

`cl_F least_negative_float (float_format_t f)`
  Returns the least negative floating point number (i.e. < 0 but closest to 0) in float format `f`.

`cl_F float_epsilon (float_format_t f)`
  Returns the smallest floating point number e > 0 such that `1+e != 1`.

`cl_F float_negative_epsilon (float_format_t f)`
  Returns the smallest floating point number e > 0 such that `1-e != 1`.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 4.11.2 Conversion to rational numbers

Each of the classes `cl_R`, `cl_RA`, `cl_F` defines the following operation:

`cl_RA rational (const type& x)`
  Returns the value of `x` as an exact number. If `x` is already an exact number, this is `x`. If `x` is a floating-point number, the value is a rational number whose denominator is a power of 2.

In order to convert back, say, `(cl_F)(cl_R)"1/3"` to `1/3`, there is the function

`cl_RA rationalize (const cl_R& x)`
  If `x` is a floating-point number, it actually represents an interval of real numbers, and this function returns the rational number with smallest denominator (and smallest numerator, in magnitude) which lies in this interval. If `x` is already an exact number, this function returns `x`.

If `x` is any float, one has

- `cl_float(rational(x),x) = x`
- `cl_float(rationalize(x),x) = x`

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 4.12 Random number generators

A random generator is a machine which produces (pseudo-)random numbers. The include file `<cln/random.h>` defines a class `random_state` which contains the state of a random generator. If you make a copy of the random number generator, the original one and the copy will produce the same sequence of random numbers.

The following functions return (pseudo-)random numbers in different formats. Calling one of these modifies the state of the random number generator in a complicated but deterministic way.

The global variable

    random_state default_random_state

contains a default random number generator. It is used when the functions below are called without `random_state` argument.

`uint32 random32 (random_state& randomstate)`
`uint32 random32 ()`
  Returns a random unsigned 32-bit number. All bits are equally random.

`cl_I random_I (random_state& randomstate, const cl_I& n)`
`cl_I random_I (const cl_I& n)`
  `n` must be an integer > 0. This function returns a random integer `x` in the range `0 <= x < n`.

`cl_F random_F (random_state& randomstate, const cl_F& n)`
`cl_F random_F (const cl_F& n)`
  `n` must be a float > 0. This function returns a random floating-point number of the same format as `n` in the range `0 <= x < n`.

`cl_R random_R (random_state& randomstate, const cl_R& n)`
`cl_R random_R (const cl_R& n)`
  Behaves like `random_I` if `n` is an integer and like `random_F` if `n` is a float.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 4.13 Obfuscating operators

The modifying C/C++ operators `+=`, `-=`, `*=`, `/=`, `&=`, `|=`, `^=`, `<<=`, `>>=` are not available by default because their use tends to make programs unreadable. It is trivial to get away without them. However, if you feel that you absolutely need these operators to get happy, then add

    #define WANT_OBFUSCATING_OPERATORS

to the beginning of your source files, before the inclusion of any CLN include files. This flag will enable the following operators:

For the classes `cl_N`, `cl_R`, `cl_RA`, `cl_F`, `cl_SF`, `cl_FF`, `cl_DF`, `cl_LF`:

`type& operator += (type&, const type&)`
`type& operator -= (type&, const type&)`
`type& operator *= (type&, const type&)`
`type& operator /= (type&, const type&)`

For the class `cl_I`:

`type& operator += (type&, const type&)`
`type& operator -= (type&, const type&)`
`type& operator *= (type&, const type&)`
`type& operator &= (type&, const type&)`
`type& operator |= (type&, const type&)`
`type& operator ^= (type&, const type&)`
`type& operator <<= (type&, const type&)`
`type& operator >>= (type&, const type&)`

For the classes `cl_N`, `cl_R`, `cl_RA`, `cl_I`, `cl_F`, `cl_SF`, `cl_FF`, `cl_DF`, `cl_LF`:

`type& operator ++ (type& x)`
  The prefix operator `++x`.

`void operator ++ (type& x, int)`
  The postfix operator `x++`.

`type& operator -- (type& x)`
  The prefix operator `--x`.

`void operator -- (type& x, int)`
  The postfix operator `x--`.

Note that by using these obfuscating operators, you wouldn't gain efficiency: In CLN ``x += y;`' is exactly the same as ``x = x+y;`', not more efficient.

[ << ] | [ >> ] |  |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

This document was generated by Richard B. Kreckel on October, 22 2005 using texi2html