| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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>.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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] | [ ? ] |
cl_I objects are most easily constructed from C integers and from
strings. See 3.4 Conversions.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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] | [ ? ] |
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"; |
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] | [ ? ] |
Non-real cl_N objects are normally constructed through the function
cl_N complex (const cl_R& realpart, const cl_R& imagpart) |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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&)
type operator - (const type&, const type&)
type operator - (const type&)
type plus1 (const type& x)
x + 1.
type minus1 (const type& x)
x - 1.
type operator * (const type&, const type&)
type square (const type& x)
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&)
type recip (const type&)
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)
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)
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)
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)
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)
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] | [ ? ] |
Each of the classes cl_RA, cl_I defines the following operations:
cl_I numerator (const type& x)
x.
cl_I denominator (const type& x)
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] | [ ? ] |
The class cl_N defines the following operation:
cl_N complex (const cl_R& a, const cl_R& b)
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)
x.
cl_R imagpart (const type& x)
x.
type conjugate (const type& x)
x.
We have the relations
x = complex(realpart(x), imagpart(x))
conjugate(x) = complex(realpart(x), -imagpart(x))
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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&)
uint32 equal_hashcode (const type&)
==. This hash code depends on the number's value,
not its type or precision.
cl_boolean zerop (const type& x)
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)
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&)
cl_boolean minusp (const type& x)
x < 0
cl_boolean plusp (const type& x)
x > 0
type max (const type& x, const type& y)
x and y.
type min (const type& x, const type& y)
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 surpris?