paul_bignumbers.h

Includes:
<stdint.h>
<stdlib.h>
<string.h>
<stdbool.h>
<stdio.h>
<math.h>
<limits.h>
<ctype.h>

Introduction

Arbitrary precision arithmetic library for C/C++.

Discussion

Implementation is included when PAUL_BIGNUMBERS_IMPLEMENTATION or PAUL_IMPLEMENTATION is defined.

Updated:
Saturday, July 19, 2025


Functions

_bigfloat_to_bigint

Internal function to convert big float to big integer.

_bigint_to_bigfloat

Internal function to convert big integer to big float.

bigcomplex_absolute

Computes the absolute value (magnitude) of a big complex number.

bigcomplex_add

Adds two big complex numbers.

bigcomplex_conjugate

Computes the conjugate of a big complex number.

bigcomplex_divide

Divides two big complex numbers.

bigcomplex_equals

Checks if two big complex numbers are equal.

bigcomplex_free

Frees the memory associated with a big complex.

bigcomplex_is_zero

Checks if a big complex number is zero.

bigcomplex_multiply

Multiplies two big complex numbers.

bigcomplex_new

Creates a new big complex number initialized to zero.

bigcomplex_new_copy

Creates a copy of an existing big complex.

bigcomplex_new_from_doubles

Creates a new big complex from double values.

bigcomplex_new_from_floats

Creates a new big complex from real and imaginary big floats.

bigcomplex_new_from_string

Creates a new big complex from a string representation.

bigcomplex_subtract

Subtracts two big complex numbers.

bigcomplex_to_string

Converts a big complex number to a string representation.

bigfloat_absolute

Computes the absolute value of a big float.

bigfloat_acos

Computes the arccosine of a big float.

bigfloat_add(const REAL *, const REAL *, REAL *)

Adds two big floats.

bigfloat_add(const REAL *, const REAL *, REAL *)

Adds two big floats.

bigfloat_asin

Computes the arcsine of a big float.

bigfloat_atan

Computes the arctangent of a big float.

bigfloat_atan2

Computes the arctangent of y/x.

bigfloat_ceil

Computes the ceiling of a big float.

bigfloat_compare

Compares two big floats.

bigfloat_copy

Copies one big float to another.

bigfloat_cos

Computes the cosine of a big float.

bigfloat_cosh

Computes the hyperbolic cosine of a big float.

bigfloat_divide

Divides two big floats.

bigfloat_equals

Checks if two big floats are equal.

bigfloat_exp

Computes the exponential function of a big float.

bigfloat_floor

Computes the floor of a big float.

bigfloat_free

Frees the memory associated with a big float.

bigfloat_greater_than

Checks if the first big float is greater than the second.

bigfloat_is_negative

Checks if a big float is negative.

bigfloat_is_zero

Checks if a big float is zero.

bigfloat_less_than

Checks if the first big float is less than the second.

bigfloat_log

Computes the natural logarithm of a big float.

bigfloat_log10

Computes the base-10 logarithm of a big float.

bigfloat_log2

Computes the base-2 logarithm of a big float.

bigfloat_max

Returns the maximum of two big floats.

bigfloat_min

Returns the minimum of two big floats.

bigfloat_multiply

Multiplies two big floats.

bigfloat_negate

Negates a big float.

bigfloat_new(size_t)

Creates a new big float initialized to zero.

bigfloat_new(size_t)

Creates a new big float initialized to zero.

bigfloat_new_copy

Creates a copy of an existing big float.

bigfloat_new_from_bigint

Creates a new big float from a big integer.

bigfloat_new_from_double

Creates a new big float from a double value.

bigfloat_new_from_string

Creates a new big float from a string representation.

bigfloat_pow

Computes the power of a big float raised to another.

bigfloat_power

Raises a big float to a power.

bigfloat_round

Rounds a big float to the nearest integer.

bigfloat_set_from_bigint

Sets a big float from a big integer.

bigfloat_set_from_double

Sets a big float from a double value.

bigfloat_set_from_string

Sets a big float from a string representation.

bigfloat_set_precision

Sets the precision of a big float.

bigfloat_sign

Returns the sign of a big float.

bigfloat_sin

Computes the sine of a big float.

bigfloat_sinh

Computes the hyperbolic sine of a big float.

bigfloat_sqrt

Computes the square root of a big float.

bigfloat_subtract

Subtracts two big floats.

bigfloat_tan

Computes the tangent of a big float.

bigfloat_tanh

Computes the hyperbolic tangent of a big float.

bigfloat_to_bigint

Converts a big float to a big integer.

bigfloat_to_double

Converts a big float to a double.

bigfloat_to_string

Converts a big float to a string representation.

bigfloat_trunc

Truncates a big float to an integer.

bigint_absolute

Computes the absolute value of a big integer.

bigint_add

Adds two big integers.

bigint_and

Performs bitwise AND on two big integers.

bigint_compare(const INT *, const INT *)

Compares two big integers.

bigint_compare(const INT *, const INT *)

Compares two big integers.

bigint_create_power

Creates a big integer representing base raised to an exponent.

bigint_divide

Divides two big integers.

bigint_ensure_capacity

Ensures a big integer has sufficient capacity.

bigint_equals

Checks if two big integers are equal.

bigint_factorial

Computes the factorial of a big integer.

bigint_free

Frees the memory associated with a big integer.

bigint_gcd

Computes the greatest common divisor of two big integers.

bigint_greater_than

Checks if the first big integer is greater than the second.

bigint_is_negative

Checks if a big integer is negative.

bigint_is_zero

Checks if a big integer is zero.

bigint_isprime

Checks if a big integer is prime.

bigint_lcm

Computes the least common multiple of two big integers.

bigint_less_than

Checks if the first big integer is less than the second.

bigint_modulo

Computes the modulo of two big integers.

bigint_multiply

Multiplies two big integers.

bigint_negate

Negates a big integer.

bigint_new

Creates a new big integer initialized to zero.

bigint_new_copy

Creates a copy of an existing big integer.

bigint_new_from_int64

Creates a new big integer from a 64-bit integer value.

bigint_new_from_string

Creates a new big integer from a string representation.

bigint_not

Performs bitwise NOT on a big integer.

bigint_or

Performs bitwise OR on two big integers.

bigint_power

Raises a big integer to a power.

bigint_set_from_int64

Sets a big integer from a 64-bit integer.

bigint_set_from_string

Sets a big integer from a string representation.

bigint_shift_left

Shifts a big integer left by a specified number of bits.

bigint_shift_right

Shifts a big integer right by a specified number of bits.

bigint_subtract

Subtracts two big integers.

bigint_to_bigfloat

Converts a big integer to a big float.

bigint_to_int64

Converts a big integer to a 64-bit integer.

bigint_to_string

Converts a big integer to a string representation.

bigint_to_string_base

Converts a big integer to a string in a specified base.

bigint_xor

Performs bitwise XOR on two big integers.

bignum_error_string

Returns a string description of a bignum error code.


_bigfloat_to_bigint


Internal function to convert big float to big integer.

bignum_err_t _bigfloat_to_bigint(
    const REAL *bigfloat,
    INT *result);  
Parameters
bigfloat

The big float to convert.

result

Pointer to store the big integer.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Truncates a big float to a big integer.


_bigint_to_bigfloat


Internal function to convert big integer to big float.

bignum_err_t _bigint_to_bigfloat(
    const INT *bigint,
    REAL *result);  
Parameters
bigint

The big integer to convert.

result

Pointer to store the big float.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Converts a big integer to a big float representation.


bigcomplex_absolute


Computes the absolute value (magnitude) of a big complex number.

Parameters
a

The input complex number.

result

Pointer to store the magnitude.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Calculates |a| and stores the result as a big float.


bigcomplex_add


Adds two big complex numbers.

bignum_err_t bigcomplex_add(
    const COMPLEX *a,
    const COMPLEX *b,
    COMPLEX *result);  
Parameters
a

The first operand.

b

The second operand.

result

Pointer to store the sum.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Computes a + b and stores the result in the provided buffer.


bigcomplex_conjugate


Computes the conjugate of a big complex number.

Parameters
a

The input complex number.

result

Pointer to store the conjugate.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Flips the sign of the imaginary part.


bigcomplex_divide


Divides two big complex numbers.

bignum_err_t bigcomplex_divide(
    const COMPLEX *a,
    const COMPLEX *b,
    COMPLEX *result);  
Parameters
a

The dividend.

b

The divisor.

result

Pointer to store the quotient.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Computes a / b and stores the result in the provided buffer.


bigcomplex_equals


Checks if two big complex numbers are equal.

bool bigcomplex_equals(
    const COMPLEX *a,
    const COMPLEX *b);  
Parameters
a

The first complex number.

b

The second complex number.

Return Value

True if equal, false otherwise.

Discussion

Returns true if a == b, false otherwise.


bigcomplex_free


Frees the memory associated with a big complex.

void bigcomplex_free(
    COMPLEX *complex);  
Parameters
complex

The big complex to free.

Discussion

Releases all resources allocated for the big complex. The pointer becomes invalid after this call.


bigcomplex_is_zero


Checks if a big complex number is zero.

bool bigcomplex_is_zero(
    const COMPLEX *a);  
Parameters
a

The complex number to check.

Return Value

True if zero, false otherwise.

Discussion

Returns true if both real and imaginary parts are zero.


bigcomplex_multiply


Multiplies two big complex numbers.

bignum_err_t bigcomplex_multiply(
    const COMPLEX *a,
    const COMPLEX *b,
    COMPLEX *result);  
Parameters
a

The first operand.

b

The second operand.

result

Pointer to store the product.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Computes a * b and stores the result in the provided buffer.


bigcomplex_new


Creates a new big complex number initialized to zero.

COMPLEX *bigcomplex_new(
    size_t precision);  
Parameters
precision

The number of significant decimal digits.

Return Value

A pointer to the new big complex, or NULL on failure.

Discussion

Allocates memory for a new COMPLEX structure and initializes it to 0+0i.


bigcomplex_new_copy


Creates a copy of an existing big complex.

Parameters
src

The big complex to copy.

Return Value

A pointer to the new copy, or NULL on failure.

Discussion

Allocates a new COMPLEX and copies all data from the source.


bigcomplex_new_from_doubles


Creates a new big complex from double values.

COMPLEX *bigcomplex_new_from_doubles(
    double real,
    double imag,
    size_t precision);  
Parameters
real

The real part as double.

imag

The imaginary part as double.

precision

The number of significant decimal digits.

Return Value

A pointer to the new big complex, or NULL on failure.

Discussion

Allocates a new COMPLEX with real and imaginary parts from doubles.


bigcomplex_new_from_floats


Creates a new big complex from real and imaginary big floats.

COMPLEX *bigcomplex_new_from_floats(
    const REAL *real,
    const REAL *imag,
    size_t precision);  
Parameters
real

The real part.

imag

The imaginary part.

precision

The number of significant decimal digits.

Return Value

A pointer to the new big complex, or NULL on failure.

Discussion

Allocates a new COMPLEX with the given real and imaginary parts.


bigcomplex_new_from_string


Creates a new big complex from a string representation.

COMPLEX *bigcomplex_new_from_string(
    const char *str,
    size_t precision);  
Parameters
str

The string representation (e.g., "1+2i").

precision

The number of significant decimal digits.

Return Value

A pointer to the new big complex, or NULL on failure.

Discussion

Parses the input string and creates a big complex.


bigcomplex_subtract


Subtracts two big complex numbers.

bignum_err_t bigcomplex_subtract(
    const COMPLEX *a,
    const COMPLEX *b,
    COMPLEX *result);  
Parameters
a

The minuend.

b

The subtrahend.

result

Pointer to store the difference.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Computes a - b and stores the result in the provided buffer.


bigcomplex_to_string


Converts a big complex number to a string representation.

char *bigcomplex_to_string(
    const COMPLEX *complex);  
Parameters
complex

The big complex to convert.

Return Value

A pointer to the string, or NULL on failure. The caller must free the string.

Discussion

Returns a dynamically allocated string representing the complex number.


bigfloat_absolute


Computes the absolute value of a big float.

bignum_err_t bigfloat_absolute(
    const REAL *a,
    REAL *result);  
Parameters
a

The input big float.

result

Pointer to store the absolute value.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Takes the absolute value of the input big float and stores it in the result buffer.


bigfloat_acos


Computes the arccosine of a big float.

bignum_err_t bigfloat_acos(
    const REAL *a,
    REAL *result);  
Parameters
a

The value (must be in [-1, 1]).

result

Pointer to store the result in radians.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Calculates acos(a) and stores the result.


bigfloat_add(const REAL *, const REAL *, REAL *)


Adds two big floats.

bignum_err_t bigfloat_add(
    const REAL *a,
    const REAL *b,
    REAL *result);  
Parameters
a

The first operand.

b

The second operand.

result

Pointer to store the sum.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Computes a + b and stores the result in the provided result buffer.


bigfloat_add(const REAL *, const REAL *, REAL *)


Adds two big floats.

bignum_err_t bigfloat_add(
    const REAL *a,
    const REAL *b,
    REAL *result);  
Parameters
a

The first operand.

b

The second operand.

result

Pointer to store the sum.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Computes a + b and stores the result in the provided result buffer.


bigfloat_asin


Computes the arcsine of a big float.

bignum_err_t bigfloat_asin(
    const REAL *a,
    REAL *result);  
Parameters
a

The value (must be in [-1, 1]).

result

Pointer to store the result in radians.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Calculates asin(a) and stores the result.


bigfloat_atan


Computes the arctangent of a big float.

bignum_err_t bigfloat_atan(
    const REAL *a,
    REAL *result);  
Parameters
a

The value.

result

Pointer to store the result in radians.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Calculates atan(a) and stores the result.


bigfloat_atan2


Computes the arctangent of y/x.

bignum_err_t bigfloat_atan2(
    const REAL *y,
    const REAL *x,
    REAL *result);  
Parameters
y

The y-coordinate.

x

The x-coordinate.

result

Pointer to store the result in radians.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Calculates atan2(y, x) and stores the result.


bigfloat_ceil


Computes the ceiling of a big float.

bignum_err_t bigfloat_ceil(
    const REAL *a,
    REAL *result);  
Parameters
a

The input big float.

result

Pointer to store the ceiling value.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Rounds up to the nearest integer and stores the result.


bigfloat_compare


Compares two big floats.

int bigfloat_compare(
    const REAL *a,
    const REAL *b);  
Parameters
a

The first big float.

b

The second big float.

Return Value

The comparison result.

Discussion

Returns -1 if a < b, 0 if a == b, 1 if a > b.


bigfloat_copy


Copies one big float to another.

bignum_err_t bigfloat_copy(
    const REAL *src,
    REAL *dest);  
Parameters
src

The source big float.

dest

The destination big float.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Copies all data from the source big float to the destination.


bigfloat_cos


Computes the cosine of a big float.

bignum_err_t bigfloat_cos(
    const REAL *a,
    REAL *result);  
Parameters
a

The angle in radians.

result

Pointer to store the result.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Calculates cos(a) and stores the result.


bigfloat_cosh


Computes the hyperbolic cosine of a big float.

bignum_err_t bigfloat_cosh(
    const REAL *a,
    REAL *result);  
Parameters
a

The input.

result

Pointer to store the result.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Calculates cosh(a) and stores the result.


bigfloat_divide


Divides two big floats.

bignum_err_t bigfloat_divide(
    const REAL *a,
    const REAL *b,
    REAL *result);  
Parameters
a

The dividend.

b

The divisor.

result

Pointer to store the quotient.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Computes a / b and stores the result in the provided result buffer.


bigfloat_equals


Checks if two big floats are equal.

bool bigfloat_equals(
    const REAL *a,
    const REAL *b);  
Parameters
a

The first big float.

b

The second big float.

Return Value

True if equal, false otherwise.

Discussion

Returns true if a == b, false otherwise.


bigfloat_exp


Computes the exponential function of a big float.

bignum_err_t bigfloat_exp(
    const REAL *a,
    REAL *result);  
Parameters
a

The exponent.

result

Pointer to store the result.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Calculates e^a and stores the result.


bigfloat_floor


Computes the floor of a big float.

bignum_err_t bigfloat_floor(
    const REAL *a,
    REAL *result);  
Parameters
a

The input big float.

result

Pointer to store the floor value.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Rounds down to the nearest integer and stores the result.


bigfloat_free


Frees the memory associated with a big float.

void bigfloat_free(
    REAL *bigfloat);  
Parameters
bigfloat

The big float to free.

Discussion

Releases all resources allocated for the big float. The pointer becomes invalid after this call.


bigfloat_greater_than


Checks if the first big float is greater than the second.

bool bigfloat_greater_than(
    const REAL *a,
    const REAL *b);  
Parameters
a

The first big float.

b

The second big float.

Return Value

True if a > b, false otherwise.

Discussion

Returns true if a > b, false otherwise.


bigfloat_is_negative


Checks if a big float is negative.

bool bigfloat_is_negative(
    const REAL *a);  
Parameters
a

The big float to check.

Return Value

True if negative, false otherwise.

Discussion

Returns true if the big float is negative, false otherwise.


bigfloat_is_zero


Checks if a big float is zero.

bool bigfloat_is_zero(
    const REAL *a);  
Parameters
a

The big float to check.

Return Value

True if zero, false otherwise.

Discussion

Returns true if the big float represents 0.0, false otherwise.


bigfloat_less_than


Checks if the first big float is less than the second.

bool bigfloat_less_than(
    const REAL *a,
    const REAL *b);  
Parameters
a

The first big float.

b

The second big float.

Return Value

True if a < b, false otherwise.

Discussion

Returns true if a < b, false otherwise.


bigfloat_log


Computes the natural logarithm of a big float.

bignum_err_t bigfloat_log(
    const REAL *a,
    REAL *result);  
Parameters
a

The input (must be positive).

result

Pointer to store the result.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Calculates ln(a) and stores the result.


bigfloat_log10


Computes the base-10 logarithm of a big float.

bignum_err_t bigfloat_log10(
    const REAL *a,
    REAL *result);  
Parameters
a

The input (must be positive).

result

Pointer to store the result.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Calculates log10(a) and stores the result.


bigfloat_log2


Computes the base-2 logarithm of a big float.

bignum_err_t bigfloat_log2(
    const REAL *a,
    REAL *result);  
Parameters
a

The input (must be positive).

result

Pointer to store the result.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Calculates log2(a) and stores the result.


bigfloat_max


Returns the maximum of two big floats.

bignum_err_t bigfloat_max(
    const REAL *a,
    const REAL *b,
    REAL *result);  
Parameters
a

The first value.

b

The second value.

result

Pointer to store the maximum.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Compares a and b and stores the larger one in result.


bigfloat_min


Returns the minimum of two big floats.

bignum_err_t bigfloat_min(
    const REAL *a,
    const REAL *b,
    REAL *result);  
Parameters
a

The first value.

b

The second value.

result

Pointer to store the minimum.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Compares a and b and stores the smaller one in result.


bigfloat_multiply


Multiplies two big floats.

bignum_err_t bigfloat_multiply(
    const REAL *a,
    const REAL *b,
    REAL *result);  
Parameters
a

The first operand.

b

The second operand.

result

Pointer to store the product.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Computes a * b and stores the result in the provided result buffer.


bigfloat_negate


Negates a big float.

Parameters
a

The big float to negate.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Flips the sign of the big float. Zero remains zero.


bigfloat_new(size_t)


Creates a new big float initialized to zero.

REAL *bigfloat_new(
    size_t precision);  
Parameters
precision

The number of significant decimal digits.

Return Value

A pointer to the new big float, or NULL on allocation failure.

Discussion

Allocates memory for a new REAL structure and initializes it to represent 0.0 with the specified precision.


bigfloat_new(size_t)


Creates a new big float initialized to zero.

REAL *bigfloat_new(
    size_t precision);  
Parameters
precision

The number of significant decimal digits.

Return Value

A pointer to the new big float, or NULL on allocation failure.

Discussion

Allocates memory for a new REAL structure and initializes it to represent 0.0 with the specified precision.


bigfloat_new_copy


Creates a copy of an existing big float.

REAL *bigfloat_new_copy(
    const REAL *src);  
Parameters
src

The big float to copy.

Return Value

A pointer to the new copy, or NULL on failure.

Discussion

Allocates a new big float and copies all data from the source.


bigfloat_new_from_bigint


Creates a new big float from a big integer.

REAL *bigfloat_new_from_bigint(
    const INT *bigint,
    size_t precision);  
Parameters
bigint

The big integer to convert.

precision

The number of significant decimal digits.

Return Value

A pointer to the new big float, or NULL on failure.

Discussion

Converts the big integer to a big float with the specified precision.


bigfloat_new_from_double


Creates a new big float from a double value.

REAL *bigfloat_new_from_double(
    double value,
    size_t precision);  
Parameters
value

The double value to convert.

precision

The number of significant decimal digits.

Return Value

A pointer to the new big float, or NULL on failure.

Discussion

Converts the double to a big float with the specified precision.


bigfloat_new_from_string


Creates a new big float from a string representation.

REAL *bigfloat_new_from_string(
    const char *str,
    size_t precision);  
Parameters
str

The string representation of the number.

precision

The number of significant decimal digits.

Return Value

A pointer to the new big float, or NULL on failure.

Discussion

Parses the input string and creates a big float with the specified precision.


bigfloat_pow


Computes the power of a big float raised to another.

bignum_err_t bigfloat_pow(
    const REAL *base,
    const REAL *exp,
    REAL *result);  
Parameters
base

The base.

exp

The exponent.

result

Pointer to store the result.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Calculates base^exp and stores the result.


bigfloat_power


Raises a big float to a power.

bignum_err_t bigfloat_power(
    const REAL *base,
    const REAL *exponent,
    REAL *result);  
Parameters
base

The base.

exponent

The exponent.

result

Pointer to store the power.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Computes base^exponent and stores the result in the provided result buffer.


bigfloat_round


Rounds a big float to the nearest integer.

bignum_err_t bigfloat_round(
    const REAL *a,
    REAL *result);  
Parameters
a

The input big float.

result

Pointer to store the rounded value.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Rounds to the nearest integer and stores the result.


bigfloat_set_from_bigint


Sets a big float from a big integer.

bignum_err_t bigfloat_set_from_bigint(
    REAL *bigfloat,
    const INT *bigint);  
Parameters
bigfloat

The big float to set.

bigint

The big integer to convert.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Converts the big integer to a big float.


bigfloat_set_from_double


Sets a big float from a double value.

bignum_err_t bigfloat_set_from_double(
    REAL *bigfloat,
    double value);  
Parameters
bigfloat

The big float to set.

value

The double value.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Converts the double to a big float representation.


bigfloat_set_from_string


Sets a big float from a string representation.

bignum_err_t bigfloat_set_from_string(
    REAL *bigfloat,
    const char *str);  
Parameters
bigfloat

The big float to set.

str

The string representation of the number.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Parses the input string and sets the big float to the corresponding value.


bigfloat_set_precision


Sets the precision of a big float.

bignum_err_t bigfloat_set_precision(
    REAL *bigfloat,
    size_t precision);  
Parameters
bigfloat

The big float to update.

precision

The new precision.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Updates the precision (number of significant decimal digits) for the big float.


bigfloat_sign


Returns the sign of a big float.

bignum_err_t bigfloat_sign(
    const REAL *a,
    REAL *result);  
Parameters
a

The input.

result

Pointer to store the sign.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Returns -1 for negative, 0 for zero, 1 for positive.


bigfloat_sin


Computes the sine of a big float.

bignum_err_t bigfloat_sin(
    const REAL *a,
    REAL *result);  
Parameters
a

The angle in radians.

result

Pointer to store the result.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Calculates sin(a) and stores the result.


bigfloat_sinh


Computes the hyperbolic sine of a big float.

bignum_err_t bigfloat_sinh(
    const REAL *a,
    REAL *result);  
Parameters
a

The input.

result

Pointer to store the result.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Calculates sinh(a) and stores the result.


bigfloat_sqrt


Computes the square root of a big float.

bignum_err_t bigfloat_sqrt(
    const REAL *a,
    REAL *result);  
Parameters
a

The input big float (must be non-negative).

result

Pointer to store the square root.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Calculates sqrt(a) and stores the result in the provided buffer.


bigfloat_subtract


Subtracts two big floats.

bignum_err_t bigfloat_subtract(
    const REAL *a,
    const REAL *b,
    REAL *result);  
Parameters
a

The minuend.

b

The subtrahend.

result

Pointer to store the difference.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Computes a - b and stores the result in the provided result buffer.


bigfloat_tan


Computes the tangent of a big float.

bignum_err_t bigfloat_tan(
    const REAL *a,
    REAL *result);  
Parameters
a

The angle in radians.

result

Pointer to store the result.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Calculates tan(a) and stores the result.


bigfloat_tanh


Computes the hyperbolic tangent of a big float.

bignum_err_t bigfloat_tanh(
    const REAL *a,
    REAL *result);  
Parameters
a

The input.

result

Pointer to store the result.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Calculates tanh(a) and stores the result.


bigfloat_to_bigint


Converts a big float to a big integer.

INT *bigfloat_to_bigint(
    const REAL *bigfloat);  
Parameters
bigfloat

The big float to convert.

Return Value

A pointer to the new big integer, or NULL on failure.

Discussion

Truncates the big float to an integer value.


bigfloat_to_double


Converts a big float to a double.

double bigfloat_to_double(
    const REAL *bigfloat);  
Parameters
bigfloat

The big float to convert.

Return Value

The double value.

Discussion

Attempts to convert the big float to a double value.


bigfloat_to_string


Converts a big float to a string representation.

char *bigfloat_to_string(
    const REAL *bigfloat);  
Parameters
bigfloat

The big float to convert.

Return Value

A pointer to the string, or NULL on failure. The caller must free the string.

Discussion

Returns a dynamically allocated string representing the big float.


bigfloat_trunc


Truncates a big float to an integer.

bignum_err_t bigfloat_trunc(
    const REAL *a,
    REAL *result);  
Parameters
a

The input big float.

result

Pointer to store the truncated value.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Removes the fractional part and stores the integer part.


bigint_absolute


Computes the absolute value of a big integer.

bignum_err_t bigint_absolute(
    const INT *a,
    INT *result);  
Parameters
a

The input big integer.

result

Pointer to store the absolute value.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Takes the absolute value of the input big integer and stores it in the result buffer.


bigint_add


Adds two big integers.

bignum_err_t bigint_add(
    const INT *a,
    const INT *b,
    INT *result);  
Parameters
a

The first operand.

b

The second operand.

result

Pointer to store the sum.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Computes a + b and stores the result in the provided result buffer.


bigint_and


Performs bitwise AND on two big integers.

bignum_err_t bigint_and(
    const INT *a,
    const INT *b,
    INT *result);  
Parameters
a

The first operand.

b

The second operand.

result

Pointer to store the result.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Computes a & b and stores the result in the provided result buffer. Operands must be non-negative.


bigint_compare(const INT *, const INT *)


Compares two big integers.

int bigint_compare(
    const INT *a,
    const INT *b);  
Parameters
a

The first big integer.

b

The second big integer.

Return Value

The comparison result.

Discussion

Returns -1 if a < b, 0 if a == b, 1 if a > b.


bigint_compare(const INT *, const INT *)


Compares two big integers.

int bigint_compare(
    const INT *a,
    const INT *b);  
Parameters
a

The first big integer.

b

The second big integer.

Return Value

The comparison result.

Discussion

Returns -1 if a < b, 0 if a == b, 1 if a > b.


bigint_create_power


Creates a big integer representing base raised to an exponent.

INT *bigint_create_power(
    int64_t base,
    size_t exponent);  
Parameters
base

The base (int64_t).

exponent

The exponent (size_t).

Return Value

A pointer to the new big integer, or NULL on failure.

Discussion

Computes base^exponent and returns a new big integer with that value.


bigint_divide


Divides two big integers.

bignum_err_t bigint_divide(
    const INT *a,
    const INT *b,
    INT *quotient,
    INT *remainder);  
Parameters
a

The dividend.

b

The divisor.

quotient

Pointer to store the quotient.

remainder

Pointer to store the remainder (can be NULL).

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Computes quotient and remainder of a / b. Stores quotient in quotient and remainder in remainder if provided.


bigint_ensure_capacity


Ensures a big integer has sufficient capacity.

bignum_err_t bigint_ensure_capacity(
    INT *bigint,
    size_t required_capacity);  
Parameters
bigint

The big integer to resize.

required_capacity

The minimum required capacity.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Resizes the digit array if necessary to accommodate at least the required number of digits.


bigint_equals


Checks if two big integers are equal.

bool bigint_equals(
    const INT *a,
    const INT *b);  
Parameters
a

The first big integer.

b

The second big integer.

Return Value

True if equal, false otherwise.

Discussion

Returns true if a == b, false otherwise.


bigint_factorial


Computes the factorial of a big integer.

bignum_err_t bigint_factorial(
    const INT *a,
    INT *result);  
Parameters
a

The input (must be non-negative).

result

Pointer to store the factorial.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Calculates a! for non-negative a.


bigint_free


Frees the memory associated with a big integer.

void bigint_free(
    INT *bigint);  
Parameters
bigint

The big integer to free.

Discussion

Releases all resources allocated for the big integer. The pointer becomes invalid after this call.


bigint_gcd


Computes the greatest common divisor of two big integers.

bignum_err_t bigint_gcd(
    const INT *a,
    const INT *b,
    INT *result);  
Parameters
a

The first big integer.

b

The second big integer.

result

Pointer to store the GCD.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Uses the Euclidean algorithm to find GCD(a, b).


bigint_greater_than


Checks if the first big integer is greater than the second.

bool bigint_greater_than(
    const INT *a,
    const INT *b);  
Parameters
a

The first big integer.

b

The second big integer.

Return Value

True if a > b, false otherwise.

Discussion

Returns true if a > b, false otherwise.


bigint_is_negative


Checks if a big integer is negative.

bool bigint_is_negative(
    const INT *a);  
Parameters
a

The big integer to check.

Return Value

True if negative, false otherwise.

Discussion

Returns true if the big integer is negative, false otherwise.


bigint_is_zero


Checks if a big integer is zero.

bool bigint_is_zero(
    const INT *a);  
Parameters
a

The big integer to check.

Return Value

True if zero, false otherwise.

Discussion

Returns true if the big integer represents 0, false otherwise.


bigint_isprime


Checks if a big integer is prime.

bool bigint_isprime(
    const INT *a);  
Parameters
a

The big integer to check (must be positive).

Return Value

True if prime, false otherwise.

Discussion

Returns true if a is a prime number, false otherwise.


bigint_lcm


Computes the least common multiple of two big integers.

bignum_err_t bigint_lcm(
    const INT *a,
    const INT *b,
    INT *result);  
Parameters
a

The first big integer.

b

The second big integer.

result

Pointer to store the LCM.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Calculates LCM(a, b) = |a*b| / GCD(a, b).


bigint_less_than


Checks if the first big integer is less than the second.

bool bigint_less_than(
    const INT *a,
    const INT *b);  
Parameters
a

The first big integer.

b

The second big integer.

Return Value

True if a < b, false otherwise.

Discussion

Returns true if a < b, false otherwise.


bigint_modulo


Computes the modulo of two big integers.

bignum_err_t bigint_modulo(
    const INT *a,
    const INT *b,
    INT *result);  
Parameters
a

The dividend.

b

The divisor.

result

Pointer to store the remainder.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Computes a % b and stores the result in the provided result buffer.


bigint_multiply


Multiplies two big integers.

bignum_err_t bigint_multiply(
    const INT *a,
    const INT *b,
    INT *result);  
Parameters
a

The first operand.

b

The second operand.

result

Pointer to store the product.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Computes a * b and stores the result in the provided result buffer.


bigint_negate


Negates a big integer.

Parameters
a

The big integer to negate.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Flips the sign of the big integer. Zero remains zero.


bigint_new


Creates a new big integer initialized to zero.

INT *bigint_new(
    void);  
Return Value

A pointer to the new big integer, or NULL on allocation failure.

Discussion

Allocates memory for a new INT structure and initializes it to represent the value 0.


bigint_new_copy


Creates a copy of an existing big integer.

INT *bigint_new_copy(
    const INT *src);  
Parameters
src

The big integer to copy.

Return Value

A pointer to the new copy, or NULL on failure.

Discussion

Allocates a new big integer and copies all data from the source.


bigint_new_from_int64


Creates a new big integer from a 64-bit integer value.

INT *bigint_new_from_int64(
    int64_t value);  
Parameters
value

The 64-bit integer value to convert.

Return Value

A pointer to the new big integer, or NULL on failure.

Discussion

Allocates and initializes a big integer with the given int64_t value.


bigint_new_from_string


Creates a new big integer from a string representation.

INT *bigint_new_from_string(
    const char *str);  
Parameters
str

The string representation of the number.

Return Value

A pointer to the new big integer, or NULL on failure.

Discussion

Parses the input string to create a big integer. Supports decimal notation with optional leading sign.


bigint_not


Performs bitwise NOT on a big integer.

bignum_err_t bigint_not(
    const INT *a,
    INT *result);  
Parameters
a

The operand.

result

Pointer to store the result.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Computes ~a and stores the result in the provided result buffer. The operand must be non-negative.


bigint_or


Performs bitwise OR on two big integers.

bignum_err_t bigint_or(
    const INT *a,
    const INT *b,
    INT *result);  
Parameters
a

The first operand.

b

The second operand.

result

Pointer to store the result.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Computes a | b and stores the result in the provided result buffer. Operands must be non-negative.


bigint_power


Raises a big integer to a power.

bignum_err_t bigint_power(
    const INT *base,
    const INT *exponent,
    INT *result);  
Parameters
base

The base.

exponent

The exponent (must be non-negative).

result

Pointer to store the power.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Computes base^exponent and stores the result in the provided result buffer. Only non-negative exponents are supported.


bigint_set_from_int64


Sets a big integer from a 64-bit integer.

bignum_err_t bigint_set_from_int64(
    INT *bigint,
    int64_t value);  
Parameters
bigint

The big integer to set.

value

The 64-bit integer value.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Converts the int64_t value to a big integer representation.


bigint_set_from_string


Sets a big integer from a string representation.

bignum_err_t bigint_set_from_string(
    INT *bigint,
    const char *str);  
Parameters
bigint

The big integer to set.

str

The string representation of the number.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Parses the input string and sets the big integer to the corresponding value.


bigint_shift_left


Shifts a big integer left by a specified number of bits.

bignum_err_t bigint_shift_left(
    const INT *a,
    size_t bits,
    INT *result);  
Parameters
a

The operand.

bits

The number of bits to shift.

result

Pointer to store the result.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Computes a << bits and stores the result in the provided result buffer.


bigint_shift_right


Shifts a big integer right by a specified number of bits.

bignum_err_t bigint_shift_right(
    const INT *a,
    size_t bits,
    INT *result);  
Parameters
a

The operand.

bits

The number of bits to shift.

result

Pointer to store the result.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Computes a >> bits and stores the result in the provided result buffer.


bigint_subtract


Subtracts two big integers.

bignum_err_t bigint_subtract(
    const INT *a,
    const INT *b,
    INT *result);  
Parameters
a

The minuend.

b

The subtrahend.

result

Pointer to store the difference.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Computes a - b and stores the result in the provided result buffer.


bigint_to_bigfloat


Converts a big integer to a big float.

REAL *bigint_to_bigfloat(
    const INT *bigint);  
Parameters
bigint

The big integer to convert.

Return Value

A pointer to the new big float, or NULL on failure.

Discussion

Creates a big float from a big integer value.


bigint_to_int64


Converts a big integer to a 64-bit integer.

int64_t bigint_to_int64(
    const INT *bigint);  
Parameters
bigint

The big integer to convert.

Return Value

The int64_t value, or INT64_MIN/INT64_MAX on overflow.

Discussion

Attempts to convert the big integer to int64_t, may overflow.


bigint_to_string


Converts a big integer to a string representation.

char *bigint_to_string(
    const INT *bigint);  
Parameters
bigint

The big integer to convert.

Return Value

A pointer to the string, or NULL on failure. The caller must free the string.

Discussion

Returns a dynamically allocated string representing the big integer in decimal.


bigint_to_string_base


Converts a big integer to a string in a specified base.

char *bigint_to_string_base(
    const INT *bigint,
    int base);  
Parameters
bigint

The big integer to convert.

base

The base for the conversion (2-36).

Return Value

A pointer to the string, or NULL on failure. The caller must free the string.

Discussion

Returns a string representation of the big integer in the given base (2-36).


bigint_xor


Performs bitwise XOR on two big integers.

bignum_err_t bigint_xor(
    const INT *a,
    const INT *b,
    INT *result);  
Parameters
a

The first operand.

b

The second operand.

result

Pointer to store the result.

Return Value

BIGNUM_OK on success, or an error code.

Discussion

Computes a ^ b and stores the result in the provided result buffer. Operands must be non-negative.


bignum_error_string


Returns a string description of a bignum error code.

const char *bignum_error_string(
    bignum_err_t error);  
Parameters
error

The error code.

Return Value

A string describing the error.

Discussion

Provides a human-readable string for the given error code.


Typedefs

bignum_err_t

Enumeration of error codes for bignum operations.

COMPLEX

Represents an arbitrary precision complex number.

INT

Represents an arbitrary precision integer.

REAL

Represents an arbitrary precision floating-point number.


bignum_err_t


Enumeration of error codes for bignum operations.

typedef enum bignum_err { 
    BIGNUM_OK = 0, 
    BIGNUM_ERROR_MEMORY, 
    BIGNUM_ERROR_DIVIDE_BY_ZERO, 
    BIGNUM_ERROR_INVALID_ARGS, 
    BIGNUM_ERROR_OVERFLOW, 
    BIGNUM_ERROR_UNDERFLOW 
} bignum_err_t;  
Fields
BIGNUM_OK

Operation completed successfully.

BIGNUM_ERROR_MEMORY

Memory allocation failed.

BIGNUM_ERROR_DIVIDE_BY_ZERO

Division by zero attempted.

BIGNUM_ERROR_INVALID_ARGS

Invalid arguments provided to function.

BIGNUM_ERROR_OVERFLOW

Arithmetic overflow occurred.

BIGNUM_ERROR_UNDERFLOW

Arithmetic underflow occurred.

Discussion

Defines the possible error conditions that can occur during arbitrary precision arithmetic operations.


COMPLEX


Represents an arbitrary precision complex number.

typedef struct COMPLEX { 
    REAL real; // Real part 
    REAL imag; // Imaginary part 
} COMPLEX;  
Fields
real

Real part of the complex number.

imag

Imaginary part of the complex number.

Discussion

Consists of real and imaginary parts, each stored as a REAL.


INT


Represents an arbitrary precision integer.

typedef struct INT { 
    uint32_t *digits; // Array of base-10^9 digits (least significant first) 
    size_t length; // Number of used digits 
    size_t capacity; // Allocated capacity 
    bool negative; // Sign flag 
} INT;  
Fields
digits

Array of base-10^9 digits (least significant first).

length

Number of used digits in the array.

capacity

Allocated capacity of the digits array.

negative

Sign flag (true for negative numbers).

Discussion

Uses a base-10^9 representation for efficient operations. The digits array stores the number in little-endian order (least significant digit first).


REAL


Represents an arbitrary precision floating-point number.

typedef struct REAL { 
    INT mantissa; // The significand as a big integer 
    int32_t exponent; // Power of 10 exponent 
    size_t precision; // Number of significant decimal digits 
    bool negative; // Sign flag 
} REAL;  
Fields
mantissa

The significand as a big integer.

exponent

Power of 10 exponent.

precision

Number of significant decimal digits.

negative

Sign flag (true for negative numbers).

Discussion

Uses a significand (mantissa) and exponent representation. The mantissa is stored as a big integer, and the exponent is a power of 10.


Macro Definitions

BIGNUM_BASE

The base used for big integer digit representation.

BIGNUM_DEFAULT_EPSILON

The default epsilon for float comparisons.

BIGNUM_DEFAULT_PRECISION

The default decimal precision for big floats.

BIGNUM_DIGIT_BITS

The number of bits in each digit.


BIGNUM_BASE


The base used for big integer digit representation.

#ifndef BIGNUM_BASE 
#define BIGNUM_BASE 1000000000UL // 10^9, fits in uint32_t  
#endif  
Discussion

Defines the base (10^9) for storing big integer digits, which fits in a uint32_t.


BIGNUM_DEFAULT_EPSILON


The default epsilon for float comparisons.

#ifndef BIGNUM_DEFAULT_EPSILON 
#define BIGNUM_DEFAULT_EPSILON 1e-30 // Default epsilon for float comparisons  
#endif  
Discussion

Defines the default epsilon value used for comparing big floats.


BIGNUM_DEFAULT_PRECISION


The default decimal precision for big floats.

#ifndef BIGNUM_DEFAULT_PRECISION 
#define BIGNUM_DEFAULT_PRECISION 64 // Default decimal precision for big floats  
#endif  
Discussion

Defines the default number of significant decimal digits for big float operations.


BIGNUM_DIGIT_BITS


The number of bits in each digit.

#ifndef BIGNUM_DIGIT_BITS 
#define BIGNUM_DIGIT_BITS 30 // log2(BIGNUM_BASE), for efficient operations  
#endif  
Discussion

Defines the number of bits (log2(BIGNUM_BASE)) for efficient operations.