Mathematical utilities.
More...
|
| long | min (long a, long b) |
| | Returns the minimum of two long integers.
|
| |
| long | max (long a, long b) |
| | Returns the maximum of two long integers.
|
| |
| size_t | absolute (long nbr) |
| | Returns the absolute value of a long integer.
|
| |
| long | power (int a, int b) |
| | Computes a raised to the power of b.
|
| |
| size_t | modulo (long a, size_t b) |
| | Computes the modulo of a signed integer with an unsigned modulus.
|
| |
| int | square_root_exact (int nb) |
| | Calculates the exact integer square root.
|
| |
| int | square_root_rounded (int nb) |
| | Calculates the nearest integer square root.
|
| |
Mathematical utilities.
Functions for common mathematical operations.
◆ absolute()
| size_t absolute |
( |
long |
nbr | ) |
|
Returns the absolute value of a long integer.
- Parameters
-
| nbr | Number to get absolute value of. |
- Returns
- Absolute value as size_t (always positive).
◆ max()
| long max |
( |
long |
a, |
|
|
long |
b |
|
) |
| |
Returns the maximum of two long integers.
- Parameters
-
| a | First long integer. |
| b | Second long integer. |
- Returns
- The larger value.
◆ min()
| long min |
( |
long |
a, |
|
|
long |
b |
|
) |
| |
Returns the minimum of two long integers.
- Parameters
-
| a | First long integer. |
| b | Second long integer. |
- Returns
- The smaller value.
◆ modulo()
| size_t modulo |
( |
long |
a, |
|
|
size_t |
b |
|
) |
| |
Computes the modulo of a signed integer with an unsigned modulus.
Handles negative values of a by ensuring the result is always positive.
- Parameters
-
| a | Dividend (can be negative). |
| b | Divisor (must be > 0). |
- Returns
- Remainder in range [0, b-1].
◆ power()
| long power |
( |
int |
a, |
|
|
int |
b |
|
) |
| |
Computes a raised to the power of b.
Uses bit shifting optimization when a == 2.
- Warning
- UB if the result overflows a long.
-
Returns 0 for negative exponents (cannot represent fractions).
- Parameters
-
| a | Base value. |
| b | Exponent (must be >= 0 for meaningful result). |
- Returns
- a^b, or 0 if b < 0.
◆ square_root_exact()
| int square_root_exact |
( |
int |
nb | ) |
|
Calculates the exact integer square root.
- Parameters
-
| nb | Number to find square root of. |
- Returns
- Exact square root if nb is a perfect square, -1 otherwise.
◆ square_root_rounded()
| int square_root_rounded |
( |
int |
nb | ) |
|
Calculates the nearest integer square root.
- Parameters
-
| nb | Number to find square root of. |
- Returns
- Nearest integer square root, -1 if nb <= 0.