Dynamic Fraction Library 1.0.0
Reference-counted arbitrary precision rational number library (MIT OR Unlicense)
Loading...
Searching...
No Matches
Functions
Arithmetic Operations

Basic arithmetic operations for arbitrary precision integers. More...

Functions

di_int di_add (di_int a, di_int b)
 Add two integers.
 
di_int di_add_i32 (di_int a, int32_t b)
 Add an integer and a 32-bit signed integer.
 
di_int di_sub (di_int a, di_int b)
 Subtract two integers.
 
di_int di_sub_i32 (di_int a, int32_t b)
 Subtract a 32-bit signed integer from an integer.
 
di_int di_mul (di_int a, di_int b)
 Multiply two integers.
 
di_int di_mul_i32 (di_int a, int32_t b)
 Multiply an integer by a 32-bit signed integer.
 
di_int di_div (di_int a, di_int b)
 Divide two integers using floor division.
 
di_int di_mod (di_int a, di_int b)
 Get remainder of integer division using floor modulo.
 
di_int di_negate (di_int a)
 Negate an integer (change sign)
 
di_int di_abs (di_int a)
 Get absolute value of an integer.
 
di_int di_pow (di_int base, uint32_t exp)
 Raise integer to a power.
 

Detailed Description

Basic arithmetic operations for arbitrary precision integers.

Function Documentation

◆ di_abs()

di_int di_abs ( di_int  a)

Get absolute value of an integer.

Parameters
aInteger to get absolute value of (may be NULL)
Returns
New di_int with |a|, or NULL on failure
Since
1.0.0
See also
di_negate() for negation
di_is_negative() for checking sign

Definition at line 2047 of file dynamic_int.h.

◆ di_add()

di_int di_add ( di_int  a,
di_int  b 
)

Add two integers.

Parameters
aFirst integer (may be NULL)
bSecond integer (may be NULL)
Returns
New di_int with result of a + b, or NULL on failure
Since
1.0.0
di_int sum = di_add(a, b); // sum = 142
di_release(&sum);
struct di_int_internal * di_int
Integer handle for arbitrary precision integers.
di_int di_add(di_int a, di_int b)
Add two integers.
di_int di_from_int32(int32_t value)
Create a new integer from a 32-bit signed integer.
void di_release(di_int *big)
Decrement reference count and free if zero.
See also
di_add_i32() for mixed-type addition with int32_t
di_sub() for subtraction

Definition at line 1523 of file dynamic_int.h.

◆ di_add_i32()

di_int di_add_i32 ( di_int  a,
int32_t  b 
)

Add an integer and a 32-bit signed integer.

Parameters
aInteger operand (may be NULL)
b32-bit signed integer operand
Returns
New di_int with result of a + b, or NULL on failure
Since
1.0.0
See also
di_add() for integer-integer addition

Definition at line 1602 of file dynamic_int.h.

◆ di_div()

di_int di_div ( di_int  a,
di_int  b 
)

Divide two integers using floor division.

Parameters
aDividend integer (must not be NULL)
bDivisor integer (must not be NULL)
Returns
New di_int with floor quotient of a / b
Since
1.0.0

Uses floor division semantics where the result is rounded towards negative infinity, ensuring consistent behavior with signed operands.

di_int quotient = di_div(a, b); // Result: -3 (floor division)
// Compare with C-style: -7/3 would be -2 (truncation towards zero)
di_release(&quotient);
di_int di_div(di_int a, di_int b)
Divide two integers using floor division.
Note
Asserts if a or b is NULL, or if b is zero
See also
di_mod() for remainder/modulo operation using floor division

Definition at line 1887 of file dynamic_int.h.

◆ di_mod()

di_int di_mod ( di_int  a,
di_int  b 
)

Get remainder of integer division using floor modulo.

Parameters
aDividend integer (must not be NULL)
bDivisor integer (must not be NULL)
Returns
New di_int with remainder of a % b where remainder has same sign as divisor
Since
1.0.0

Uses floor modulo semantics where the remainder always has the same sign as the divisor, consistent with floor division behavior.

di_int remainder = di_mod(a, b); // Result: 2 (positive, same sign as divisor)
// Compare with C-style: -7%3 would be -1 (negative, same sign as dividend)
di_release(&remainder);
di_int di_mod(di_int a, di_int b)
Get remainder of integer division using floor modulo.
Note
Asserts if a or b is NULL, or if b is zero
See also
di_div() for quotient using floor division

Definition at line 2025 of file dynamic_int.h.

◆ di_mul()

di_int di_mul ( di_int  a,
di_int  b 
)

Multiply two integers.

Parameters
aFirst integer (may be NULL)
bSecond integer (may be NULL)
Returns
New di_int with result of a * b, or NULL on failure
Since
1.0.0
Note
Supports arbitrary precision multiplication
See also
di_mul_i32() for mixed-type multiplication with int32_t

Definition at line 1803 of file dynamic_int.h.

◆ di_mul_i32()

di_int di_mul_i32 ( di_int  a,
int32_t  b 
)

Multiply an integer by a 32-bit signed integer.

Parameters
aInteger operand (may be NULL)
b32-bit signed integer operand
Returns
New di_int with result of a * b, or NULL on failure
Since
1.0.0
See also
di_mul() for integer-integer multiplication

Definition at line 1877 of file dynamic_int.h.

◆ di_negate()

di_int di_negate ( di_int  a)

Negate an integer (change sign)

Parameters
aInteger to negate (may be NULL)
Returns
New di_int with result of -a, or NULL on failure
Since
1.0.0
di_int neg = di_negate(pos); // neg = -42
di_int pos_again = di_negate(neg); // pos_again = 42
di_release(&pos);
di_release(&neg);
di_release(&pos_again);
di_int di_negate(di_int a)
Negate an integer (change sign)
See also
di_abs() for absolute value

Definition at line 1639 of file dynamic_int.h.

◆ di_pow()

di_int di_pow ( di_int  base,
uint32_t  exp 
)

Raise integer to a power.

Parameters
baseBase integer (may be NULL)
expExponent (32-bit unsigned integer)
Returns
New di_int with result of base^exp, or NULL on failure
Since
1.0.0
Note
This is not yet implemented
See also
di_mod_pow() for modular exponentiation

◆ di_sub()

di_int di_sub ( di_int  a,
di_int  b 
)

Subtract two integers.

Parameters
aFirst integer (minuend, may be NULL)
bSecond integer (subtrahend, may be NULL)
Returns
New di_int with result of a - b, or NULL on failure
Since
1.0.0
See also
di_sub_i32() for mixed-type subtraction with int32_t
di_add() for addition

Definition at line 1614 of file dynamic_int.h.

◆ di_sub_i32()

di_int di_sub_i32 ( di_int  a,
int32_t  b 
)

Subtract a 32-bit signed integer from an integer.

Parameters
aInteger operand (may be NULL)
b32-bit signed integer operand
Returns
New di_int with result of a - b, or NULL on failure
Since
1.0.0
See also
di_sub() for integer-integer subtraction

Definition at line 1627 of file dynamic_int.h.