cgul_bcd.h File Reference

arbitrary precision binary-coded decimal arithmetic More...

#include "cgul_common.h"
#include "cgul_exception.h"
#include "cgul_string.h"
#include "cgul_big_integer.h"
Include dependency graph for cgul_bcd.h:
This graph shows which files directly or indirectly include this file:

Typedefs

typedef typedefCGUL_BEGIN_C struct cgul_bcd * cgul_bcd_t
 

Functions

CGUL_END_C CGUL_BEGIN_C CGUL_EXPORT cgul_bcd_t cgul_bcd__new (cgul_exception_t *cex)
 
CGUL_EXPORT cgul_bcd_t cgul_bcd__new_from_bcd (cgul_exception_t *cex, cgul_bcd_t value)
 
CGUL_EXPORT cgul_bcd_t cgul_bcd__new_from_long (cgul_exception_t *cex, long value)
 
CGUL_EXPORT cgul_bcd_t cgul_bcd__new_from_unsigned_long (cgul_exception_t *cex, unsigned long value)
 
CGUL_EXPORT cgul_bcd_t cgul_bcd__new_from_size_type (cgul_exception_t *cex, size_t value)
 
CGUL_EXPORT cgul_bcd_t cgul_bcd__new_from_string (cgul_exception_t *cex, const char *s)
 
CGUL_EXPORT void cgul_bcd__delete (cgul_bcd_t bcd)
 
CGUL_EXPORT int cgul_bcd__is_positive (cgul_exception_t *cex, cgul_bcd_t bcd)
 
CGUL_EXPORT int cgul_bcd__is_negative (cgul_exception_t *cex, cgul_bcd_t bcd)
 
CGUL_EXPORT int cgul_bcd__is_zero (cgul_exception_t *cex, cgul_bcd_t bcd)
 
CGUL_EXPORT void cgul_bcd__set_to_zero (cgul_exception_t *cex, cgul_bcd_t bcd)
 
CGUL_EXPORT int cgul_bcd__is_one (cgul_exception_t *cex, cgul_bcd_t bcd)
 
CGUL_EXPORT void cgul_bcd__set_to_one (cgul_exception_t *cex, cgul_bcd_t bcd)
 
CGUL_EXPORT int cgul_bcd__is_even (cgul_exception_t *cex, cgul_bcd_t bcd)
 
CGUL_EXPORT int cgul_bcd__is_odd (cgul_exception_t *cex, cgul_bcd_t bcd)
 
CGUL_EXPORT void cgul_bcd__assign (cgul_exception_t *cex, cgul_bcd_t lhs, cgul_bcd_t rhs)
 
CGUL_EXPORT void cgul_bcd__assign_from_long (cgul_exception_t *cex, cgul_bcd_t lhs, long rhs)
 
CGUL_EXPORT void cgul_bcd__assign_from_unsigned_long (cgul_exception_t *cex, cgul_bcd_t lhs, unsigned long rhs)
 
CGUL_EXPORT void cgul_bcd__assign_from_size_type (cgul_exception_t *cex, cgul_bcd_t lhs, size_t rhs)
 
CGUL_EXPORT void cgul_bcd__assign_from_string (cgul_exception_t *cex, cgul_bcd_t lhs, const char *rhs)
 
CGUL_EXPORT void cgul_bcd__negate (cgul_exception_t *cex, cgul_bcd_t lhs, cgul_bcd_t rhs)
 
CGUL_EXPORT void cgul_bcd__abs (cgul_exception_t *cex, cgul_bcd_t lhs, cgul_bcd_t rhs)
 
CGUL_EXPORT void cgul_bcd__add (cgul_exception_t *cex, cgul_bcd_t lhs, cgul_bcd_t rhs1, cgul_bcd_t rhs2)
 
CGUL_EXPORT void cgul_bcd__subtract (cgul_exception_t *cex, cgul_bcd_t lhs, cgul_bcd_t rhs1, cgul_bcd_t rhs2)
 
CGUL_EXPORT void cgul_bcd__multiply (cgul_exception_t *cex, cgul_bcd_t lhs, cgul_bcd_t rhs1, cgul_bcd_t rhs2)
 
CGUL_EXPORT void cgul_bcd__divide (cgul_exception_t *cex, cgul_bcd_t quotient, cgul_bcd_t remainder, cgul_bcd_t dividend, cgul_bcd_t divisor)
 
CGUL_EXPORT void cgul_bcd__power (cgul_exception_t *cex, cgul_bcd_t lhs, cgul_bcd_t base, cgul_bcd_t exponent)
 
CGUL_EXPORT void cgul_bcd__power_modulo (cgul_exception_t *cex, cgul_bcd_t lhs, cgul_bcd_t base, cgul_bcd_t exponent, cgul_bcd_t modulus)
 
CGUL_EXPORT int cgul_bcd__is_probably_prime (cgul_exception_t *cex, cgul_bcd_t bcd, unsigned int count)
 
CGUL_EXPORT void cgul_bcd__shift_left (cgul_exception_t *cex, cgul_bcd_t lhs, cgul_bcd_t rhs, size_t digit_count)
 
CGUL_EXPORT void cgul_bcd__shift_right (cgul_exception_t *cex, cgul_bcd_t lhs, cgul_bcd_t rhs, size_t digit_count)
 
CGUL_EXPORT int cgul_bcd__compare (cgul_exception_t *cex, cgul_bcd_t lhs, cgul_bcd_t rhs)
 
CGUL_EXPORT void cgul_bcd__gcd (cgul_exception_t *cex, cgul_bcd_t lhs, cgul_bcd_t rhs1, cgul_bcd_t rhs2)
 
CGUL_EXPORT void cgul_bcd__lcm (cgul_exception_t *cex, cgul_bcd_t lhs, cgul_bcd_t rhs1, cgul_bcd_t rhs2)
 
CGUL_EXPORT void cgul_bcd__swap (cgul_exception_t *cex, cgul_bcd_t bcd_1, cgul_bcd_t bcd_2)
 
CGUL_EXPORT int cgul_bcd__as_long (cgul_exception_t *cex, cgul_bcd_t bcd, long int *value)
 
CGUL_EXPORT int cgul_bcd__as_unsigned_long (cgul_exception_t *cex, cgul_bcd_t bcd, unsigned long int *value)
 
CGUL_EXPORT void cgul_bcd__as_big_integer (cgul_exception_t *cex, cgul_bcd_t bcd, cgul_big_integer_t big_integer)
 
CGUL_EXPORT char * cgul_bcd__as_binary (cgul_exception_t *cex, cgul_bcd_t bcd)
 
CGUL_EXPORT void cgul_bcd__append_as_binary (cgul_exception_t *cex, cgul_bcd_t bcd, cgul_string_t s)
 
CGUL_EXPORT char * cgul_bcd__as_octal (cgul_exception_t *cex, cgul_bcd_t bcd)
 
CGUL_EXPORT void cgul_bcd__append_as_octal (cgul_exception_t *cex, cgul_bcd_t bcd, cgul_string_t s)
 
CGUL_EXPORT char * cgul_bcd__as_decimal (cgul_exception_t *cex, cgul_bcd_t bcd)
 
CGUL_EXPORT void cgul_bcd__append_as_decimal (cgul_exception_t *cex, cgul_bcd_t bcd, cgul_string_t s)
 
CGUL_EXPORT char * cgul_bcd__as_hexadecimal (cgul_exception_t *cex, cgul_bcd_t bcd)
 
CGUL_EXPORT void cgul_bcd__append_as_hexadecimal (cgul_exception_t *cex, cgul_bcd_t bcd, cgul_string_t s)
 
CGUL_EXPORT void cgul_bcd__print_as_binary (cgul_exception_t *cex, cgul_bcd_t bcd, FILE *f)
 
CGUL_EXPORT void cgul_bcd__print_as_octal (cgul_exception_t *cex, cgul_bcd_t bcd, FILE *f)
 
CGUL_EXPORT void cgul_bcd__print_as_decimal (cgul_exception_t *cex, cgul_bcd_t bcd, FILE *f)
 
CGUL_EXPORT void cgul_bcd__print_as_hexadecimal (cgul_exception_t *cex, cgul_bcd_t bcd, FILE *f)
 

Detailed Description

Basic arbitrary precision arithmetic using binary-coded decimal (BCD). This is especially useful when you already have a large string of binary digits (perhaps in a text file) that you want to perform a few basic operations on before you write out the new value. You can do the arithmetic in binary-coded decimal without having the overhead of converting to base-2 in order to do the arithmetic and then back to base-10 in order to write out the result (at least that is the theory).

Author
Paul Serice

Typedef Documentation

§ cgul_bcd_t

typedef typedefCGUL_BEGIN_C struct cgul_bcd* cgul_bcd_t

Opaque pointer to a cgul_bcd instance.

Function Documentation

§ cgul_bcd__new()

Create a new cgul_bcd instance initialized to positive 0. The caller is responsible for calling cgul_bcd__delete() on the value returned. If memory allocation fails, NULL is returned, and an exception is thrown.

Parameters
[in,out]cexc-style exception
Returns
new cgul_bcd instance

§ cgul_bcd__new_from_bcd()

CGUL_EXPORT cgul_bcd_t cgul_bcd__new_from_bcd ( cgul_exception_t cex,
cgul_bcd_t  value 
)

Create a new cgul_bcd instance initialized to value. The caller is responsible for calling cgul_bcd__delete() on the value returned. If memory allocation fails, NULL is returned, and an exception is thrown.

Parameters
[in,out]cexc-style exception
[in]valueinitial value
Returns
new cgul_bcd instance

§ cgul_bcd__new_from_long()

CGUL_EXPORT cgul_bcd_t cgul_bcd__new_from_long ( cgul_exception_t cex,
long  value 
)

Create a new cgul_bcd instance initialized to value. The caller is responsible for calling cgul_bcd__delete() on the value returned. If memory allocation fails, NULL is returned, and an exception is thrown.

Parameters
[in,out]cexc-style exception
[in]valueinitial value
Returns
new cgul_bcd instance

§ cgul_bcd__new_from_unsigned_long()

CGUL_EXPORT cgul_bcd_t cgul_bcd__new_from_unsigned_long ( cgul_exception_t cex,
unsigned long  value 
)

Create a new cgul_bcd instance initialized to value. The caller is responsible for calling cgul_bcd__delete() on the value returned. If memory allocation fails, NULL is returned, and an exception is thrown.

Parameters
[in,out]cexc-style exception
[in]valueinitial value
Returns
new cgul_bcd instance

§ cgul_bcd__new_from_size_type()

CGUL_EXPORT cgul_bcd_t cgul_bcd__new_from_size_type ( cgul_exception_t cex,
size_t  value 
)

Create a new cgul_bcd instance initialized to value. The caller is responsible for calling cgul_bcd__delete() on the value returned. If memory allocation fails, NULL is returned, and an exception is thrown.

Parameters
[in,out]cexc-style exception
[in]valueinitial value
Returns
new cgul_bcd instance

§ cgul_bcd__new_from_string()

CGUL_EXPORT cgul_bcd_t cgul_bcd__new_from_string ( cgul_exception_t cex,
const char *  s 
)

Create a new cgul_bcd instance initialized from the string s. The caller is responsible for calling cgul_bcd__delete() on the value returned. If an error occurs, NULL is returned, and an exception is thrown.

Parameters
[in,out]cexc-style exception
[in]sinitial value
Returns
new cgul_bcd instance

§ cgul_bcd__delete()

CGUL_EXPORT void cgul_bcd__delete ( cgul_bcd_t  bcd)

Delete the cgul_bcd object in bcd. The client must not use bcd after calling this method.

Parameters
[in]bcdcgul_bcd instance

§ cgul_bcd__is_positive()

CGUL_EXPORT int cgul_bcd__is_positive ( cgul_exception_t cex,
cgul_bcd_t  bcd 
)

Return 1 if bcd is positive. Return 0 if it is not.

Parameters
[in]cexc-style exception
[in]bcdcgul_bcd instance
Returns
whether bcd is positive

§ cgul_bcd__is_negative()

CGUL_EXPORT int cgul_bcd__is_negative ( cgul_exception_t cex,
cgul_bcd_t  bcd 
)

Return 1 if bcd is negative. Return 0 if it is not.

Parameters
[in]cexc-style exception
[in]bcdcgul_bcd instance
Returns
whether bcd is negative

§ cgul_bcd__is_zero()

CGUL_EXPORT int cgul_bcd__is_zero ( cgul_exception_t cex,
cgul_bcd_t  bcd 
)

Return 1 if bcd is zero. Return 0 if it is not.

Parameters
[in]cexc-style exception
[in]bcdcgul_bcd instance
Returns
whether bcd is zero

§ cgul_bcd__set_to_zero()

CGUL_EXPORT void cgul_bcd__set_to_zero ( cgul_exception_t cex,
cgul_bcd_t  bcd 
)

Set the value of bcd to zero.

Parameters
[in]cexc-style exception
[in]bcdcgul_bcd instance

§ cgul_bcd__is_one()

CGUL_EXPORT int cgul_bcd__is_one ( cgul_exception_t cex,
cgul_bcd_t  bcd 
)

Return 1 if bcd is one. Return 0 if it is not.

Parameters
[in]cexc-style exception
[in]bcdcgul_bcd instance
Returns
whether bcd is one

§ cgul_bcd__set_to_one()

CGUL_EXPORT void cgul_bcd__set_to_one ( cgul_exception_t cex,
cgul_bcd_t  bcd 
)

Set the value of bcd to one.

Parameters
[in]cexc-style exception
[in]bcdcgul_bcd instance

§ cgul_bcd__is_even()

CGUL_EXPORT int cgul_bcd__is_even ( cgul_exception_t cex,
cgul_bcd_t  bcd 
)

Return whether bcd is even.

Parameters
[in]cexc-style exception
[in]bcdcgul_bcd instance
Returns
whether bcd is even

§ cgul_bcd__is_odd()

CGUL_EXPORT int cgul_bcd__is_odd ( cgul_exception_t cex,
cgul_bcd_t  bcd 
)

Return whether bcd is odd.

Parameters
[in]cexc-style exception
[in]bcdcgul_bcd instance
Returns
whether bcd is odd

§ cgul_bcd__assign()

CGUL_EXPORT void cgul_bcd__assign ( cgul_exception_t cex,
cgul_bcd_t  lhs,
cgul_bcd_t  rhs 
)

Assign rhs to lhs. It is safe for rhs and lhs to point to the same underlying cgul_bcd object.

Parameters
[in,out]cexc-style exception
[out]lhsleft-hand side
[in]rhsright-hand side

§ cgul_bcd__assign_from_long()

CGUL_EXPORT void cgul_bcd__assign_from_long ( cgul_exception_t cex,
cgul_bcd_t  lhs,
long  rhs 
)

Assign long in rhs to lhs.

Parameters
[in,out]cexc-style exception
[out]lhsleft-hand side
[in]rhsright-hand side

§ cgul_bcd__assign_from_unsigned_long()

CGUL_EXPORT void cgul_bcd__assign_from_unsigned_long ( cgul_exception_t cex,
cgul_bcd_t  lhs,
unsigned long  rhs 
)

Assign unsigned long in rhs to lhs.

Parameters
[in,out]cexc-style exception
[out]lhsleft-hand side
[in]rhsright-hand side

§ cgul_bcd__assign_from_size_type()

CGUL_EXPORT void cgul_bcd__assign_from_size_type ( cgul_exception_t cex,
cgul_bcd_t  lhs,
size_t  rhs 
)

Assign size type in rhs to lhs.

Parameters
[in,out]cexc-style exception
[out]lhsleft-hand side
[in]rhsright-hand side

§ cgul_bcd__assign_from_string()

CGUL_EXPORT void cgul_bcd__assign_from_string ( cgul_exception_t cex,
cgul_bcd_t  lhs,
const char *  rhs 
)

Assign the string of decimal digits in rhs to lhs. Only assigning from a string of decimal digits is supported. If you need to support other common bases, you should use the cgul_big_integer class instead. If an error occurs, an exception is thrown.

Parameters
[in,out]cexc-style exception
[out]lhsleft-hand side
[in]rhsright-hand side
Returns
whether rhs parsed

§ cgul_bcd__negate()

CGUL_EXPORT void cgul_bcd__negate ( cgul_exception_t cex,
cgul_bcd_t  lhs,
cgul_bcd_t  rhs 
)

Assign the negative of rhs to lhs. It is safe and efficient for rhs and lhs to point to the same underlying cgul_bcd object.

Parameters
[in,out]cexc-style exception
[out]lhsleft-hand side
[in]rhsright-hand side

§ cgul_bcd__abs()

CGUL_EXPORT void cgul_bcd__abs ( cgul_exception_t cex,
cgul_bcd_t  lhs,
cgul_bcd_t  rhs 
)

Assign the absolute value of rhs to lhs. It is safe and efficient for rhs and lhs to point to the same underlying cgul_bcd object.

Parameters
[in,out]cexc-style exception
[out]lhsleft-hand side
[in]rhsright-hand side

§ cgul_bcd__add()

CGUL_EXPORT void cgul_bcd__add ( cgul_exception_t cex,
cgul_bcd_t  lhs,
cgul_bcd_t  rhs1,
cgul_bcd_t  rhs2 
)

Add rhs1 to rhs2 and store the result in lhs. It is safe for rhs1 or rhs2 and lhs to point to the same underlying cgul_bcd object.

Parameters
[in,out]cexc-style exception
[out]lhsleft-hand side
[in]rhs1first right-hand side value
[in]rhs2second right-hand side value

§ cgul_bcd__subtract()

CGUL_EXPORT void cgul_bcd__subtract ( cgul_exception_t cex,
cgul_bcd_t  lhs,
cgul_bcd_t  rhs1,
cgul_bcd_t  rhs2 
)

Subtract rhs2 from rhs1 and store the result in lhs. It is safe for rhs1 or rhs2 and lhs to point to the same underlying cgul_bcd object.

Parameters
[in,out]cexc-style exception
[out]lhsleft-hand side
[in]rhs1first right-hand side value
[in]rhs2second right-hand side value

§ cgul_bcd__multiply()

CGUL_EXPORT void cgul_bcd__multiply ( cgul_exception_t cex,
cgul_bcd_t  lhs,
cgul_bcd_t  rhs1,
cgul_bcd_t  rhs2 
)

Multiply rhs1 with rhs2 and store the result in lhs. It is safe for rhs1 or rhs2 and lhs to point to the same underlying cgul_bcd object.

Parameters
[in,out]cexc-style exception
[out]lhsleft-hand side
[in]rhs1first right-hand side value
[in]rhs2second right-hand side value

§ cgul_bcd__divide()

CGUL_EXPORT void cgul_bcd__divide ( cgul_exception_t cex,
cgul_bcd_t  quotient,
cgul_bcd_t  remainder,
cgul_bcd_t  dividend,
cgul_bcd_t  divisor 
)

Divide dividend by divisor and store the result in quotient and remainder. It is safe for dividend or divisor and quotient or remainder to point to the same underlying cgul_bcd object. This method throws an exception if division by zero is attempted or if memory cannot be allocated.

Parameters
[in,out]cexc-style exception
[out]quotientquotient
[out]remainderremainder
[in]dividenddividend
[in]divisordivisor

§ cgul_bcd__power()

CGUL_EXPORT void cgul_bcd__power ( cgul_exception_t cex,
cgul_bcd_t  lhs,
cgul_bcd_t  base,
cgul_bcd_t  exponent 
)

This method calculates base raised to the power of exponent and saves the result in lhs. If exponent is negative, an exception is thrown. If the program runs out of memory, an exception is thrown.

This method is not as effecient as cgul_bcd__power_modulo() when the client is only interested in the modulo and not the power.

Note
For this implementation, pow(0,0) = 1. This is in agreement with some, but others leave it undefined.
Parameters
[in,out]cexc-style exception
[out]lhsleft-hand side
[in]basebase
[in]exponentexponent
See also
cgul_bcd__power_modulo()

§ cgul_bcd__power_modulo()

CGUL_EXPORT void cgul_bcd__power_modulo ( cgul_exception_t cex,
cgul_bcd_t  lhs,
cgul_bcd_t  base,
cgul_bcd_t  exponent,
cgul_bcd_t  modulus 
)

This method calculates the modulo of base to the power of exponent using modulus as the modulus. If exponent is negative, an exception is thrown. If the program runs out of memory, an exception is thrown.

This method is much more effecient than cgul_bcd__power() when the client is only interested in the modulo and not the power because the modulo operation is used at each stage of the calculation to keep the size of the numbers within reason.

Note
For this implementation, pow(0,0) = 1. This is in agreement with some, but others leave it undefined.
Parameters
[in,out]cexc-style exception
[out]lhsleft-hand side
[in]basebase
[in]exponentexponent
[in]modulusmodulus
See also
cgul_bcd__power()

§ cgul_bcd__is_probably_prime()

CGUL_EXPORT int cgul_bcd__is_probably_prime ( cgul_exception_t cex,
cgul_bcd_t  bcd,
unsigned int  count 
)

This method uses the Miller-Rabin Primality Test to determine whether bcd is probably prime. The error per iteration for Miller-Rabin is bound by 1/4. This method performs count iterations. The following table shows the theoretical upper bound for the error associated with different values of count:

    count | error
    ------------------------
       10 | 9.54e-7
       25 | 8.88e-16
       50 | 7.89e-31
      100 | 6.22e-61
      500 | 9.33e-302
     1000 | 8.71e-603

If an exception is not thrown, this method returns 1 if bcd is probably prime and 0 if it is definitely composite.

This method throws an exception if bcd is negative. It also throws an exception if the program runs out of memory. If an exception is thrown, 0 is returned.

Parameters
[in,out]cexc-style exception
[in]bcdcgul_bcd instance
[in]countnumber of times test is independently executed
Returns
whether bcd is probably prime

§ cgul_bcd__shift_left()

CGUL_EXPORT void cgul_bcd__shift_left ( cgul_exception_t cex,
cgul_bcd_t  lhs,
cgul_bcd_t  rhs,
size_t  digit_count 
)

Shift rhs left by digit_count and put the result in lhs. It is safe for rhs and lhs to point to the same underlying cgul_bcd object.

Note that this method is a base-10 shift that shifts individual decimal digits. Contrast this method with cgul_big_integer__shift_left() which shifts individual base-2 bits instead.

This method can be used as a fast way to multiply by a power of 10.

Parameters
[in,out]cexc-style exception
[out]lhsleft-hand side
[in]rhsright-hand side
[in]digit_counthow far to shift rhs

§ cgul_bcd__shift_right()

CGUL_EXPORT void cgul_bcd__shift_right ( cgul_exception_t cex,
cgul_bcd_t  lhs,
cgul_bcd_t  rhs,
size_t  digit_count 
)

Shift rhs right by digit_count and put the result in lhs. It is safe for rhs and lhs to point to the same underlying cgul_bcd object.

Note that this method is a base-10 shift that shifts individual decimal digits. Contrast this method with cgul_big_integer__shift_right() which shifts individual base-2 bits instead.

This method can be used as a fast way to divide by a power of 10.

Parameters
[in,out]cexc-style exception
[out]lhsleft-hand side
[in]rhsright-hand side
[in]digit_counthow far to shift rhs

§ cgul_bcd__compare()

CGUL_EXPORT int cgul_bcd__compare ( cgul_exception_t cex,
cgul_bcd_t  lhs,
cgul_bcd_t  rhs 
)

Return less than zero if lhs is less than rhs, return zero if lhs equals rhs, and return greater than zero if lhs is greater than rhs.

Parameters
[in]cexc-style exception
[in]lhsleft-hand side
[in]rhsright-hand side
Returns
comparison of lhs and rhs

§ cgul_bcd__gcd()

CGUL_EXPORT void cgul_bcd__gcd ( cgul_exception_t cex,
cgul_bcd_t  lhs,
cgul_bcd_t  rhs1,
cgul_bcd_t  rhs2 
)

This method finds the greatest common divisor (GCD) of rhs1 and rhs2 and saves the result in lhs. If an error occurs, an exception is thrown.

Parameters
[in,out]cexc-style exception
[out]lhsleft-hand side
[in]rhs1first right-hand side value
[in]rhs2second right-hand side value

§ cgul_bcd__lcm()

CGUL_EXPORT void cgul_bcd__lcm ( cgul_exception_t cex,
cgul_bcd_t  lhs,
cgul_bcd_t  rhs1,
cgul_bcd_t  rhs2 
)

This method finds the least common multiple (LCM) of rhs1 and rhs2 and saves the result in lhs. The value returned is often used as the lowest common denominator (LCD). If an error occurs, an exception is thrown.

Parameters
[in,out]cexc-style exception
[out]lhsleft-hand side
[in]rhs1first right-hand side value
[in]rhs2second right-hand side value

§ cgul_bcd__swap()

CGUL_EXPORT void cgul_bcd__swap ( cgul_exception_t cex,
cgul_bcd_t  bcd_1,
cgul_bcd_t  bcd_2 
)

Swap the internal data quickly without copying.

Parameters
[in]cexc-style exception
[in]bcd_1first cgul_bcd instance
[in]bcd_2second cgul_bcd instance

§ cgul_bcd__as_long()

CGUL_EXPORT int cgul_bcd__as_long ( cgul_exception_t cex,
cgul_bcd_t  bcd,
long int *  value 
)

Convert the value of bcd to a long. This function returns 1 if the conversion can be accomplished without losing any significant bits; otherwise, it returns 0.

Parameters
[in]cexc-style exception
[in]bcdcgul_bcd instance
[out]valueconverted value
Returns
whether a conversion occurred

§ cgul_bcd__as_unsigned_long()

CGUL_EXPORT int cgul_bcd__as_unsigned_long ( cgul_exception_t cex,
cgul_bcd_t  bcd,
unsigned long int *  value 
)

Convert the value of bcd to an unsigned long. This function returns 1 if the conversion can be accomplished without losing any significant bits; otherwise, it returns 0.

Parameters
[in]cexc-style exception
[in]bcdcgul_bcd instance
[out]valueconverted value
Returns
whether a conversion occurred

§ cgul_bcd__as_big_integer()

CGUL_EXPORT void cgul_bcd__as_big_integer ( cgul_exception_t cex,
cgul_bcd_t  bcd,
cgul_big_integer_t  big_integer 
)

This method converts bcd to a cgul_big_integer object and saves the conversion to big_integer which the caller has already instantiated. If an error occurs, an exception is thrown, and big_integer is not altered.

Parameters
[in,out]cexc-style exception
[in]bcdcgul_bcd instance
[out]big_integercgul_big_integer instance

§ cgul_bcd__as_binary()

CGUL_EXPORT char* cgul_bcd__as_binary ( cgul_exception_t cex,
cgul_bcd_t  bcd 
)

Return a char* object that holds the string representation of bcd as a radix-2 number. The caller is responsible for calling free() on the pointer returned.

Parameters
[in,out]cexc-style exception
[in]bcdcgul_bcd instance
Returns
string representation of bcd as binary

§ cgul_bcd__append_as_binary()

CGUL_EXPORT void cgul_bcd__append_as_binary ( cgul_exception_t cex,
cgul_bcd_t  bcd,
cgul_string_t  s 
)

Append the binary representation of bcd to s.

Parameters
[in,out]cexc-style exception
[in]bcdcgul_bcd instance
[in]swhere to append the string representation

§ cgul_bcd__as_octal()

CGUL_EXPORT char* cgul_bcd__as_octal ( cgul_exception_t cex,
cgul_bcd_t  bcd 
)

Return a char* that holds the string representation of bcd as a radix-8 number. The caller is responsible for calling free() on the pointer returned.

Parameters
[in,out]cexc-style exception
[in]bcdcgul_bcd instance
Returns
string representation of bcd as octal

§ cgul_bcd__append_as_octal()

CGUL_EXPORT void cgul_bcd__append_as_octal ( cgul_exception_t cex,
cgul_bcd_t  bcd,
cgul_string_t  s 
)

Append the octal representation of bcd to s.

Parameters
[in,out]cexc-style exception
[in]bcdcgul_bcd instance
[in]swhere to append the string representation

§ cgul_bcd__as_decimal()

CGUL_EXPORT char* cgul_bcd__as_decimal ( cgul_exception_t cex,
cgul_bcd_t  bcd 
)

Return a char* object that holds the string representation of the decimal digits in bcd. The caller is responsible for calling free() on the pointer returned.

Producing a string in base-10 is extremely efficient because the internal format of this class is binary-coded decimal.

Parameters
[in,out]cexc-style exception
[in]bcdcgul_bcd instance
Returns
string representation of bcd as decimal

§ cgul_bcd__append_as_decimal()

CGUL_EXPORT void cgul_bcd__append_as_decimal ( cgul_exception_t cex,
cgul_bcd_t  bcd,
cgul_string_t  s 
)

Append the decimal digits in bcd to s.

Producing a string in base-10 is extremely efficient because the internal format of this class is binary-coded decimal.

Parameters
[in,out]cexc-style exception
[in]bcdcgul_bcd instance
[in]swhere to append the string representation

§ cgul_bcd__as_hexadecimal()

CGUL_EXPORT char* cgul_bcd__as_hexadecimal ( cgul_exception_t cex,
cgul_bcd_t  bcd 
)

Return a char* object that holds the string representation of bcd as a radix-16 number. The caller is responsible for calling free() on the pointer returned.

Parameters
[in,out]cexc-style exception
[in]bcdcgul_bcd instance
Returns
string representation of bcd as hexadecimal

§ cgul_bcd__append_as_hexadecimal()

CGUL_EXPORT void cgul_bcd__append_as_hexadecimal ( cgul_exception_t cex,
cgul_bcd_t  bcd,
cgul_string_t  s 
)

Append the hexadecimal representation of bcd to s.

Parameters
[in,out]cexc-style exception
[in]bcdcgul_bcd instance
[in]swhere to append the string representation

§ cgul_bcd__print_as_binary()

CGUL_EXPORT void cgul_bcd__print_as_binary ( cgul_exception_t cex,
cgul_bcd_t  bcd,
FILE *  f 
)

Print the string representation of bcd as a radix-2 number on f.

Parameters
[in,out]cexc-style exception
[in]bcdcgul_bcd instance
[in]foutput file

§ cgul_bcd__print_as_octal()

CGUL_EXPORT void cgul_bcd__print_as_octal ( cgul_exception_t cex,
cgul_bcd_t  bcd,
FILE *  f 
)

Print the string representation of bcd as a radix-8 number on f.

Parameters
[in,out]cexc-style exception
[in]bcdcgul_bcd instance
[in]foutput file

§ cgul_bcd__print_as_decimal()

CGUL_EXPORT void cgul_bcd__print_as_decimal ( cgul_exception_t cex,
cgul_bcd_t  bcd,
FILE *  f 
)

Print the string representation of bcd as a radix-10 number on f.

Producing a string in base-10 is extremely efficient because the internal format of this class is binary-coded decimal.

Parameters
[in,out]cexc-style exception
[in]bcdcgul_bcd instance
[in]foutput file

§ cgul_bcd__print_as_hexadecimal()

CGUL_EXPORT void cgul_bcd__print_as_hexadecimal ( cgul_exception_t cex,
cgul_bcd_t  bcd,
FILE *  f 
)

Print the string representation of bcd as a radix-16 number on f.

Parameters
[in,out]cexc-style exception
[in]bcdcgul_bcd instance
[in]foutput file