offbrand
A collection of generic, reference counted datastructures in C for C
 All Classes Files Functions Variables Typedefs Macros Groups
Functions | Variables
obint.c File Reference

obint Method Implementation More...

#include "../../include/obint.h"
#include "../../include/private/obint_private.h"

Functions

obintobint_new (int64_t num)
 Creates a new obint with the given integer. More...
 
int64_t obint_value (const obint *a)
 Returns the value stored in obint as an integer. More...
 
obintobint_from_string (const obstring *numstr)
 Creates a new obint with value indicated by given string. More...
 
obstringobint_to_string (const obint *a)
 Creates an instance of obstring containing a string representation of the value of an obint. More...
 
obintobint_copy (const obint *a)
 Creates an instance of obint with the same value as the argument obint. More...
 
uint8_t obint_is_zero (const obint *a)
 Checks if an instance of obint is zero and returns the boolean truth value. More...
 
uint8_t obint_is_negative (const obint *a)
 Checks if an instance of obint is negative and returns the boolean truth value. More...
 
obintobint_add (const obint *a, const obint *b)
 Creates a new obint containing the sum of two obint values. More...
 
obintobint_add_primitive (const obint *a, int64_t b)
 Creates a new obint containing the sum of an obint and a machine integer. More...
 
obintobint_subtract (const obint *a, const obint *b)
 Creates a new obint containing the difference of two obint values. More...
 
obintobint_subtract_primitive (const obint *a, int64_t b)
 Creates a new obint containing the difference of an obint and a machine integer. More...
 
obintobint_multiply (const obint *a, const obint *b)
 Creates a new obint containing the product of two obint values. More...
 
obintobint_multiply_primitive (const obint *a, int64_t b)
 Creates a new obint containing the product of an obint and a machine integer. More...
 
obintobint_divide (const obint *a, const obint *b)
 Creates a new obint containing the quotient resule of integer division between two obint values,. More...
 
obintobint_divide_primitive (const obint *a, int64_t b)
 Creates a new obint containing the quotient of an obint and a machine integer. More...
 
obintobint_mod (const obint *a, const obint *b)
 Creates a new obint containing the remainder of integer division between two obint values,. More...
 
obintobint_mod_primitive (const obint *a, int64_t b)
 Creates a new obint containing the remainder of integer division of an obint and a machine integer. More...
 
obintobint_create_default (uint64_t num_digits)
 Default constructor for obint. More...
 
ob_hash_t obint_hash (const obj *to_hash)
 Hash function for obint. More...
 
int8_t obint_compare (const obj *a, const obj *b)
 Compares two instances of obint. More...
 
int8_t obint_compare_magnitudes (const obint *a, const obint *b)
 Compares magnitudes of two instances of obint. More...
 
void obint_display (const obj *to_print)
 Descriptor for an instance of obint, prints relevant information about the class to stderr. More...
 
void obint_destroy (obj *to_dealloc)
 Destructor for obint. More...
 
obintobint_add_unsigned (const obint *a, const obint *b)
 Creates a new integer as a result of addition between two obints with sign value ignored. More...
 
obintobint_subtract_unsigned (const obint *a, const obint *b)
 Creates a new integer as a result of subtraction between two obints with sign ignored. More...
 
obintobint_multiply_unsigned (const obint *a, const obint *b)
 Creates a new integer as a result of multiplication between two obints with sign ignored. More...
 
obintobint_reduce (const obint *a, const obint *b, const obint *approx, uint8_t quotient)
 Creates a new integer as a result of divison or modulus between two obints with sign ignored. More...
 
uint64_t obint_most_sig (const obint *a)
 Returns the index where the most significant non-zero digit occurs, or zero if there are no non-zero digits. More...
 
void obint_split (const obint *a, uint64_t i, obint **b1, obint **b0)
 Splits the obint into two obint instances at given index. More...
 
void obint_shift (obint *a, uint64_t m)
 Shifts the obint the given number of digits to the left, in place, essentially unsigned multiplication by 10^m. More...
 
int64_t obint_magnitude (const obint *a)
 Returns the unsigned value of the obint. More...
 
void obint_set_max_digits (uint8_t digits)
 Sets the number of digits operated on in a primitive from 1 to 17. More...
 

Variables

uint8_t int64_max_digits = 17
 

Detailed Description

obint Method Implementation

Author
theck

Function Documentation

obint* obint_add ( const obint a,
const obint b 
)

Creates a new obint containing the sum of two obint values.

Parameters
aA non-NULL pointer to type obint
bA non-NULL pointer to type obint
Returns
An instance of obint with value given by a+b
obint* obint_add_primitive ( const obint a,
int64_t  b 
)

Creates a new obint containing the sum of an obint and a machine integer.

Parameters
aA non-NULL pointer to type obint
bA signed 64 bit machine integer
Returns
An instance of obint with value given by a+b
obint* obint_add_unsigned ( const obint a,
const obint b 
)

Creates a new integer as a result of addition between two obints with sign value ignored.

Parameters
aA non-NULL pointer to type obint
bA non-NULL pointer to type obint
Returns
An instance of obint, which may or may not have proper sign
int8_t obint_compare ( const obj a,
const obj b 
)

Compares two instances of obint.

Parameters
aA non-NULL obj pointer to type obint
bA non-NULL obj pointer to type obint
Return values
OB_LESS_THANobj a is less than b
OB_GREATER_THANobj a is greater than b
OB_EQUAL_TOobj a is equal to b
int8_t obint_compare_magnitudes ( const obint a,
const obint b 
)

Compares magnitudes of two instances of obint.

Parameters
aA non-NULL pointer to type obint
bA non-NULL pointer to type obint
Return values
OB_LESS_THANMagnitude of a a is less than b
OB_GREATER_THANMagnitude of a is greater than to b
OB_EQUAL_TOMagnitude of a is equal to b
obint* obint_copy ( const obint a)

Creates an instance of obint with the same value as the argument obint.

Parameters
aA non-NULL pointer to type obint
Returns
A copy of the argument obint
obint* obint_create_default ( uint64_t  num_digits)

Default constructor for obint.

Parameters
num_digitsApproximate number of digits required to store the entire, always overestimate to ensure enough space is reserved
Returns
An instance of class obint
Warning
All public constructors should call this constructor and intialize individual members as needed, so that all base data is initialized properly.
void obint_destroy ( obj to_dealloc)

Destructor for obint.

Parameters
to_deallocAn obj pointer to an instance of obint with reference count of 0
Warning
Do not call manually, release will call automatically when the instances reference count drops to 0!
void obint_display ( const obj to_print)

Descriptor for an instance of obint, prints relevant information about the class to stderr.

Parameters
to_printA non-NULL obj pointer to an instance of type obint
obint* obint_divide ( const obint a,
const obint b 
)

Creates a new obint containing the quotient resule of integer division between two obint values,.

Parameters
aA non-NULL pointer to type obint
bA non-NULL pointer to type obint
Returns
An instance of obint with value given by a/b
obint* obint_divide_primitive ( const obint a,
int64_t  b 
)

Creates a new obint containing the quotient of an obint and a machine integer.

Parameters
aA non-NULL pointer to type obint
bA signed 64 bit machine integer
Returns
An instance of obint with value given by a/b
obint* obint_from_string ( const obstring numstr)

Creates a new obint with value indicated by given string.

Parameters
numstrA non-NULL pointer to an obstring instance containing only digit characters and possibly one '-' character at the beginning of the string to indicate a negative value
Returns
An instance of obint with value given by numstr
ob_hash_t obint_hash ( const obj to_hash)

Hash function for obint.

Parameters
to_hashAn obj pointer to an instance of obint
Returns
Key value (hash) for the given obj pointer to a obint
uint8_t obint_is_negative ( const obint a)

Checks if an instance of obint is negative and returns the boolean truth value.

Parameters
aA non-NULL pointer to type obint
Return values
0The argument is positive
non-zeroThe argument is negative
uint8_t obint_is_zero ( const obint a)

Checks if an instance of obint is zero and returns the boolean truth value.

Parameters
aA non-NULL pointer to type obint
Return values
0The argument is non-zero
non-zeroThe argument is zero
int64_t obint_magnitude ( const obint a)

Returns the unsigned value of the obint.

Parameters
aA non-NULL pointer to type obint
Returns
a positive int64_t representing the value of the argument obint
obint* obint_mod ( const obint a,
const obint b 
)

Creates a new obint containing the remainder of integer division between two obint values,.

Parameters
aA non-NULL pointer to type obint
bA non-NULL pointer to type obint
Returns
An instance of obint with value given by ab
obint* obint_mod_primitive ( const obint a,
int64_t  b 
)

Creates a new obint containing the remainder of integer division of an obint and a machine integer.

Parameters
aA non-NULL pointer to type obint
bA signed 64 bit machine integer
Returns
An instance of obint with value given by ab
uint64_t obint_most_sig ( const obint a)

Returns the index where the most significant non-zero digit occurs, or zero if there are no non-zero digits.

Parameters
aA non-NULL pointer to type obint
Returns
An integer index where the most significant non-zero digit in a can be found
obint* obint_multiply ( const obint a,
const obint b 
)

Creates a new obint containing the product of two obint values.

Parameters
aA non-NULL pointer to type obint
bA non-NULL pointer to type obint
Returns
An instance of obint with value given by a*b
obint* obint_multiply_primitive ( const obint a,
int64_t  b 
)

Creates a new obint containing the product of an obint and a machine integer.

Parameters
aA non-NULL pointer to type obint
bA signed 64 bit machine integer
Returns
An instance of obint with value given by a*b
obint* obint_multiply_unsigned ( const obint a,
const obint b 
)

Creates a new integer as a result of multiplication between two obints with sign ignored.

Parameters
aA non-NULL pointer to type obint
bA non-NULL pointer to type obint
Returns
An instance of obint, which may or may not have proper sign

Multiplication performed using the Karatsuba multiplcation algorithm

obint* obint_new ( int64_t  num)

Creates a new obint with the given integer.

Parameters
numInteger value for new obint
Returns
An instance of obint with value given by num
obint* obint_reduce ( const obint a,
const obint b,
const obint approx,
uint8_t  quotient 
)

Creates a new integer as a result of divison or modulus between two obints with sign ignored.

Parameters
aA non-NULL pointer to type obint
bA non-NULL pointer to type obint
approxA non-NULL pointer to type obint used to accumulate a quotient value
quotientA boolean value, 0 indicates that a remainder should be returned, non-zero indicates a quotient should be returned
Returns
An instance of obint, which may or may not have proper sign
void obint_set_max_digits ( uint8_t  digits)

Sets the number of digits operated on in a primitive from 1 to 17.

Parameters
digitsAn integer between 2 and 17 inclusive
Warning
If digits does not fall in the 2 to 17 inclusive range nothing is done
The default value of 17 is most efficient, this method exists for testing purposes only!
void obint_shift ( obint a,
uint64_t  m 
)

Shifts the obint the given number of digits to the left, in place, essentially unsigned multiplication by 10^m.

int

Parameters
aA non-NULL pointer to type obint
mNumber of digits to shift
void obint_split ( const obint a,
uint64_t  i,
obint **  b1,
obint **  b0 
)

Splits the obint into two obint instances at given index.

Parameters
aA non-NULL pointer to type obint to be split
iThe integer index where the split occurs, where a-digits[i] is placed in the most significant section of the split
b1A reference to a pointer to type obint, used to return the most significant section of the obint being split
b0A reference to a pointer to type obint, used to return the least significant section of the obint being split

If index i extends beyond the bounds of a then b1 is set to point to a 0 obint and b2 is set to point to a copy of a. a is treated as unsigned for this operation

obint* obint_subtract ( const obint a,
const obint b 
)

Creates a new obint containing the difference of two obint values.

Parameters
aA non-NULL pointer to type obint
bA non-NULL pointer to type obint
Returns
An instance of obint with value given by a-b
obint* obint_subtract_primitive ( const obint a,
int64_t  b 
)

Creates a new obint containing the difference of an obint and a machine integer.

Parameters
aA non-NULL pointer to type obint
bA signed 64 bit machine integer
Returns
An instance of obint with value given by a-b
obint* obint_subtract_unsigned ( const obint a,
const obint b 
)

Creates a new integer as a result of subtraction between two obints with sign ignored.

Parameters
aA non-NULL pointer to type obint, larger or equal to b
bA non-NULL pointer to type obint, smaller or equal to a
Returns
An instance of obint, which may or may not have proper sign
Warning
Proper results guaranteed only for a >= b, a < b will not work and probably will cause segmentation faults
obstring* obint_to_string ( const obint a)

Creates an instance of obstring containing a string representation of the value of an obint.

Parameters
aA non-NULL pointer to type obint
Returns
An instance of obstring
int64_t obint_value ( const obint a)

Returns the value stored in obint as an integer.

Parameters
aA non-NULL pointer to type obint
Returns
A signed 64 bit integer representing value stored in a
Warning
If the value of the obint cannot be represented in 64 bits then returned value will not represent the true value of the obint

Variable Documentation

uint8_t int64_max_digits = 17

maximum number of decimal digits to operate on as one int64_t