Libft
Custom implementation of core libc functions with additional utility helpers.
Loading...
Searching...
No Matches
Math Functions

Mathematical utilities. More...

Functions

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.

Detailed Description

Mathematical utilities.

Functions for common mathematical operations.

Function Documentation

◆ absolute()

size_t absolute ( long nbr)

Returns the absolute value of a long integer.

Parameters
nbrNumber 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
aFirst long integer.
bSecond long integer.
Returns
The larger value.

◆ min()

long min ( long a,
long b )

Returns the minimum of two long integers.

Parameters
aFirst long integer.
bSecond 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
aDividend (can be negative).
bDivisor (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
aBase value.
bExponent (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
nbNumber 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
nbNumber to find square root of.
Returns
Nearest integer square root, -1 if nb <= 0.