[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
3.4 Conversions
Conversions from any class to any its superclasses (“base classes” in C++ terminology) is done automatically.
Conversions from the C built-in types ‘long’ and ‘unsigned long’
are provided for the classes cl_I
, cl_RA
, cl_R
,
cl_N
and cl_number
.
Conversions from the C built-in types ‘int’ and ‘unsigned int’
are provided for the classes cl_I
, cl_RA
, cl_R
,
cl_N
and cl_number
. However, these conversions emphasize
efficiency. On 32-bit systems, their range is therefore limited:
- - The conversion from ‘int’ works only if the argument is < 2^29 and >= -2^29.
- - The conversion from ‘unsigned int’ works only if the argument is < 2^29.
In a declaration like ‘cl_I x = 10;’ the C++ compiler is able to
do the conversion of 10
from ‘int’ to ‘cl_I’ at compile time
already. On the other hand, code like ‘cl_I x = 1000000000;’ is
in error on 32-bit machines.
So, if you want to be sure that an ‘int’ whose magnitude is not guaranteed
to be < 2^29 is correctly converted to a ‘cl_I’, first convert it to a
‘long’. Similarly, if a large ‘unsigned int’ is to be converted to a
‘cl_I’, first convert it to an ‘unsigned long’. On 64-bit machines
there is no such restriction. There, conversions from arbitrary 32-bit ‘int’
values always works correctly.
Conversions from the C built-in type ‘float’ are provided for the classes
cl_FF
, cl_F
, cl_R
, cl_N
and cl_number
.
Conversions from the C built-in type ‘double’ are provided for the classes
cl_DF
, cl_F
, cl_R
, cl_N
and cl_number
.
Conversions from ‘const char *’ are provided for the classes
cl_I
, cl_RA
,
cl_SF
, cl_FF
, cl_DF
, cl_LF
, cl_F
,
cl_R
, cl_N
.
The easiest way to specify a value which is outside of the range of the
C++ built-in types is therefore to specify it as a string, like this:
cl_I order_of_rubiks_cube_group = "43252003274489856000";
Note that this conversion is done at runtime, not at compile-time.
Conversions from cl_I
to the C built-in types ‘int’,
‘unsigned int’, ‘long’, ‘unsigned long’ are provided through
the functions
int cl_I_to_int (const cl_I& x)
unsigned int cl_I_to_uint (const cl_I& x)
long cl_I_to_long (const cl_I& x)
unsigned long cl_I_to_ulong (const cl_I& x)
-
Returns
x
as element of the C type ctype. Ifx
is not representable in the range of ctype, a runtime error occurs.
Conversions from the classes cl_I
, cl_RA
,
cl_SF
, cl_FF
, cl_DF
, cl_LF
, cl_F
and
cl_R
to the C built-in types ‘float’ and ‘double’ are provided through
the functions
float float_approx (const type& x)
double double_approx (const type& x)
-
Returns an approximation of
x
of C type ctype. Ifabs(x)
is too close to 0 (underflow), 0 is returned. Ifabs(x)
is too large (overflow), an IEEE infinity is returned.
Conversions from any class to any of its subclasses (“derived classes” in
C++ terminology) are not provided. Instead, you can assert and check
that a value belongs to a certain subclass, and return it as element of that
class, using the ‘As’ and ‘The’ macros.
As(type)(value)
checks that value belongs to
type and returns it as such.
The(type)(value)
assumes that value belongs to
type and returns it as such. It is your responsibility to ensure
that this assumption is valid. Since macros and namespaces don’t go
together well, there is an equivalent to ‘The’: the template
‘the’.
Example:
cl_I x = …; if (!(x >= 0)) abort(); cl_I ten_x_a = The(cl_I)(expt(10,x)); // If x >= 0, 10^x is an integer. // In general, it would be a rational number. cl_I ten_x_b = the<cl_I>(expt(10,x)); // The same as above.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on August 27, 2013 using texi2html 5.0.