Math module
Cnx provides some extensions to the C99 type-generic math library and will in the future also provide some additional math-related functionality
Functions
- i64 cnx_gcd_i64(i64 left, i64 right)
- Calculate the gcd of the two inputs.
- u64 cnx_gcd_u64(u64 left, u64 right)
- Calculate the gcd of the two inputs.
- i64 cnx_lcm_i64(i64 left, i64 right)
- Calculate the lcm of the two inputs.
- u64 cnx_lcm_u64(u64 left, u64 right)
- Calculate the lcm of the two inputs.
- CnxOption(u8) cnx_checked_add_u8(u8 left
- Performs the checked addition of
leftandright - CnxOption(u16) cnx_checked_add_u16(u16 left
- Performs the checked addition of
leftandright - CnxOption(u32) cnx_checked_add_u32(u32 left
- Performs the checked addition of
leftandright - CnxOption(u64) cnx_checked_add_u64(u64 left
- Performs the checked addition of
leftandright - CnxOption(i8) cnx_checked_add_i8(i8 left
- Performs the checked addition of
leftandright - CnxOption(i16) cnx_checked_add_i16(i16 left
- Performs the checked addition of
leftandright - CnxOption(i32) cnx_checked_add_i32(i32 left
- Performs the checked addition of
leftandright - CnxOption(i64) cnx_checked_add_i64(i64 left
- Performs the checked addition of
leftandright - CnxOption(f32) cnx_checked_add_f32(f32 left
- Performs the checked addition of
leftandright - CnxOption(f64) cnx_checked_add_f64(f64 left
- Performs the checked addition of
leftandright
Defines
- #define cnx_min(x, y)
- Returns the minimum of the two arguments.
- #define min(x, y)
- Returns the minimum of the two arguments.
- #define cnx_max(x, y)
- Returns the maximum of the two arguments.
- #define max(x, y)
- Returns the maximum of the two arguments.
- #define cnx_abs(x)
- Returns the absolute value of the given one.
- #define abs(x)
- Returns the absolute value of the given one.
- #define cnx_static_abs(x)
- Calculates the absolute value of the given input at compile time.
- #define cnx_static_sign(x)
- Calculates the sign of the given input at compile time.
- #define cnx_sign(x)
- Calculates the sign of the given input.
- #define cnx_gcd(left, right)
- Calculate the gcd of the two inputs.
- #define cnx_static_gcd(left, right)
- Calculate the gcd of the two inputs at compile time.
- #define cnx_lcm(left, right)
- Calculate the lcm of the two inputs.
- #define cnx_static_lcm(left, right)
- Calculate the lcm of the two inputs at compile time.
- #define cnx_static_checked_add(left, right)
- Performs the checked addition of
leftandrightat compile-time. - #define cnx_checked_sub(left, right)
- Performs the checked subtraction of
rightfromleft - #define cnx_static_checked_sub(left, right)
- Performs the checked subtraction of
rightfromleftat compile-time. - #define cnx_checked_mul(left, right)
- Performs the checked multiplication of
leftandright - #define cnx_static_checked_mul(left, right)
- Performs the checked multiplication of
leftandrightat compile-time. - #define cnx_checked_div(left, right)
- Performs the checked division of
leftandright - #define cnx_static_checked_div(left, right)
- Performs the checked division of
leftandrightat compile-time.
Function documentation
i64 cnx_gcd_i64(i64 left,
i64 right)
#include <include/Cnx/Math.h>
Calculate the gcd of the two inputs.
| Parameters | |
|---|---|
| left | - One of the pair of numbers to get the gcd of |
| right | - One of the pair of numbers to get the gcd of |
| Returns | The gcd of the pair of numbers |
u64 cnx_gcd_u64(u64 left,
u64 right)
#include <include/Cnx/Math.h>
Calculate the gcd of the two inputs.
| Parameters | |
|---|---|
| left | - One of the pair of numbers to get the gcd of |
| right | - One of the pair of numbers to get the gcd of |
| Returns | The gcd of the pair of numbers |
i64 cnx_lcm_i64(i64 left,
i64 right)
#include <include/Cnx/Math.h>
Calculate the lcm of the two inputs.
| Parameters | |
|---|---|
| left | - One of the pair of numbers to get the lcm of |
| right | - One of the pair of numbers to get the lcm of |
| Returns | The lcm of the pair of numbers |
u64 cnx_lcm_u64(u64 left,
u64 right)
#include <include/Cnx/Math.h>
Calculate the lcm of the two inputs.
| Parameters | |
|---|---|
| left | - One of the pair of numbers to get the lcm of |
| right | - One of the pair of numbers to get the lcm of |
| Returns | The lcm of the pair of numbers |
CnxOption(u8) cnx_checked_add_u8(u8 left
#include <include/Cnx/Math.h>
Performs the checked addition of left and right
| Returns | Some(u8, result) if the addition was successful (no overflow occurred), None(u8) otherwise |
|---|
Performs the checked division of left and right
Performs the checked multiplication of left and right
Performs the checked subtraction of right from left
Performs the checked addition of left and right, returning Some(u8, result) if overflow did not occur or None(u8) if overflow occurred
Performs the checked subtraction of right from left, returning Some(u8, result) if overflow did not occur or None(u8) if overflow occurred
Performs the checked multiplication of left and right, returning Some(u8, result) if overflow did not occur or None(u8) if overflow occurred
CnxOption(u16) cnx_checked_add_u16(u16 left
#include <include/Cnx/Math.h>
Performs the checked addition of left and right
| Returns | Some(u16, result) if the addition was successful (no overflow occurred), None(u16) otherwise |
|---|
Performs the checked division of left and right
Performs the checked multiplication of left and right
Performs the checked subtraction of right from left
Performs the checked addition of left and right, returning Some(u16, result) if overflow did not occur or None(u16) if overflow occurred
Performs the checked subtraction of right from left, returning Some(u16, result) if overflow did not occur or None(u16) if overflow occurred
Performs the checked multiplication of left and right, returning Some(u16, result) if overflow did not occur or None(u16) if overflow occurred
Performs the checked division of left and right, returning Some(u16, result) if overflow did not occur or None(u16) if overflow occurred
CnxOption(u32) cnx_checked_add_u32(u32 left
#include <include/Cnx/Math.h>
Performs the checked addition of left and right
| Returns | Some(u32, result) if the addition was successful (no overflow occurred), None(u32) otherwise |
|---|
Performs the checked division of left and right
Performs the checked multiplication of left and right
Performs the checked subtraction of right from left
Performs the checked addition of left and right, returning Some(u32, result) if overflow did not occur or None(u32) if overflow occurred
Performs the checked subtraction of right from left, returning Some(u32, result) if overflow did not occur or None(u32) if overflow occurred
Performs the checked multiplication of left and right, returning Some(u32, result) if overflow did not occur or None(u32) if overflow occurred
Performs the checked division of left and right, returning Some(u32, result) if overflow did not occur or None(u32) if overflow occurred
CnxOption(u64) cnx_checked_add_u64(u64 left
#include <include/Cnx/Math.h>
Performs the checked addition of left and right
| Returns | Some(u64, result) if the addition was successful (no overflow occurred), None(u64) otherwise |
|---|
Performs the checked division of left and right
Performs the checked multiplication of left and right
Performs the checked subtraction of right from left
Performs the checked addition of left and right, returning Some(u64, result) if overflow did not occur or None(u64) if overflow occurred
Performs the checked subtraction of right from left, returning Some(u64, result) if overflow did not occur or None(u64) if overflow occurred
Performs the checked multiplication of left and right, returning Some(u64, result) if overflow did not occur or None(u64) if overflow occurred
Performs the checked division of left and right, returning Some(u64, result) if overflow did not occur or None(u64) if overflow occurred
CnxOption(i8) cnx_checked_add_i8(i8 left
#include <include/Cnx/Math.h>
Performs the checked addition of left and right
| Returns | Some(i8, result) if the addition was successful (no overflow occurred), None(i8) otherwise |
|---|
Performs the checked division of left and right
Performs the checked multiplication of left and right
Performs the checked subtraction of right from left
Performs the checked addition of left and right, returning Some(i8, result) if overflow did not occur or None(i8) if overflow occurred
Performs the checked subtraction of right from left, returning Some(i8, result) if overflow did not occur or None(i8) if overflow occurred
Performs the checked multiplication of left and right, returning Some(i8, result) if overflow did not occur or None(i8) if overflow occurred
Performs the checked division of left and right, returning Some(i8, result) if overflow did not occur or None(i8) if overflow occurred
CnxOption(i16) cnx_checked_add_i16(i16 left
#include <include/Cnx/Math.h>
Performs the checked addition of left and right
| Returns | Some(i16, result) if the addition was successful (no overflow occurred), None(i16) otherwise |
|---|
Performs the checked division of left and right
Performs the checked multiplication of left and right
Performs the checked subtraction of right from left
Performs the checked addition of left and right, returning Some(i16, result) if overflow did not occur or None(i16) if overflow occurred
Performs the checked subtraction of right from left, returning Some(i16, result) if overflow did not occur or None(i16) if overflow occurred
Performs the checked multiplication of left and right, returning Some(i16, result) if overflow did not occur or None(i16) if overflow occurred
Performs the checked division of left and right, returning Some(i16, result) if overflow did not occur or None(i16) if overflow occurred
CnxOption(i32) cnx_checked_add_i32(i32 left
#include <include/Cnx/Math.h>
Performs the checked addition of left and right
| Returns | Some(i32, result) if the addition was successful (no overflow occurred), None(i32) otherwise |
|---|
Performs the checked division of left and right
Performs the checked multiplication of left and right
Performs the checked subtraction of right from left
Performs the checked addition of left and right, returning Some(i32, result) if overflow did not occur or None(i32) if overflow occurred
Performs the checked subtraction of right from left, returning Some(i32, result) if overflow did not occur or None(i32) if overflow occurred
Performs the checked multiplication of left and right, returning Some(i32, result) if overflow did not occur or None(i32) if overflow occurred
Performs the checked division of left and right, returning Some(i32, result) if overflow did not occur or None(i32) if overflow occurred
CnxOption(i64) cnx_checked_add_i64(i64 left
#include <include/Cnx/Math.h>
Performs the checked addition of left and right
| Returns | Some(i64, result) if the addition was successful (no overflow occurred), None(i64) otherwise |
|---|
Returns the given CnxRatio as a scalar value.
Performs the checked division of left and right
Performs the checked multiplication of left and right
Performs the checked subtraction of right from left
Performs the checked addition of left and right, returning Some(i64, result) if overflow did not occur or None(i64) if overflow occurred
Performs the checked subtraction of right from left, returning Some(i64, result) if overflow did not occur or None(i64) if overflow occurred
Performs the checked multiplication of left and right, returning Some(i64, result) if overflow did not occur or None(i64) if overflow occurred
Performs the checked division of left and right, returning Some(i64, result) if overflow did not occur or None(i64) if overflow occurred
CnxOption(f32) cnx_checked_add_f32(f32 left
#include <include/Cnx/Math.h>
Performs the checked addition of left and right
| Returns | Some(f32, result) if the addition was successful (no overflow occurred), None(f32) otherwise |
|---|
Performs the checked division of left and right
Performs the checked multiplication of left and right
Performs the checked subtraction of right from left
Performs the checked addition of left and right, returning Some(f32, result) if overflow did not occur or None(f32) if overflow occurred
Performs the checked subtraction of right from left, returning Some(f32, result) if overflow did not occur or None(f32) if overflow occurred
Performs the checked multiplication of left and right, returning Some(f32, result) if overflow did not occur or None(f32) if overflow occurred
Performs the checked division of left and right, returning Some(f32, result) if overflow did not occur or None(f32) if overflow occurred
CnxOption(f64) cnx_checked_add_f64(f64 left
#include <include/Cnx/Math.h>
Performs the checked addition of left and right
| Returns | Some(f64, result) if the addition was successful (no overflow occurred), None(f64) otherwise |
|---|
Performs the checked division of left and right
Performs the checked multiplication of left and right
Performs the checked subtraction of right from left
Performs the checked addition of left and right, returning Some(f64, result) if overflow did not occur or None(f64) if overflow occurred
Performs the checked subtraction of right from left, returning Some(f64, result) if overflow did not occur or None(f64) if overflow occurred
Performs the checked multiplication of left and right, returning Some(f64, result) if overflow did not occur or None(f64) if overflow occurred
Performs the checked division of left and right, returning Some(f64, result) if overflow did not occur or None(f64) if overflow occurred
Define documentation
#define cnx_min(x,
y)
#include <include/Cnx/Math.h>
Returns the minimum of the two arguments.
| Parameters | |
|---|---|
| x | - the first argument |
| y | - the second argument |
| Returns | the argument with the minimum value |
Returns the argument with the minimum value of the two
#define min(x,
y)
#include <include/Cnx/Math.h>
Returns the minimum of the two arguments.
| Parameters | |
|---|---|
| x | - the first argument |
| y | - the second argument |
| Returns | the argument with the minimum value |
Returns the argument with the minimum value of the two
#define cnx_max(x,
y)
#include <include/Cnx/Math.h>
Returns the maximum of the two arguments.
| Parameters | |
|---|---|
| x | - the first argument |
| y | - the second argument |
| Returns | the argument with the maximum value |
Returns the argument with the maximum value of the two
#define max(x,
y)
#include <include/Cnx/Math.h>
Returns the maximum of the two arguments.
| Parameters | |
|---|---|
| x | - the first argument |
| y | - the second argument |
| Returns | the argument with the maximum value |
Returns the argument with the maximum value of the two
#define cnx_abs(x)
#include <include/Cnx/Math.h>
Returns the absolute value of the given one.
| Parameters | |
|---|---|
| x | - The value to get the absolute value of |
| Returns | the absolute value of x |
#define abs(x)
#include <include/Cnx/Math.h>
Returns the absolute value of the given one.
| Parameters | |
|---|---|
| x | - The value to get the absolute value of |
| Returns | the absolute value of x |
#define cnx_static_abs(x)
#include <include/Cnx/Math.h>
Calculates the absolute value of the given input at compile time.
| Parameters | |
|---|---|
| x | - The value to get the absolute value of |
| Returns | the absolute value of x, as a compile-time constant |
#define cnx_static_sign(x)
#include <include/Cnx/Math.h>
Calculates the sign of the given input at compile time.
| Parameters | |
|---|---|
| x | - The value to get the sign of |
| Returns | -1 if negative, 0 if 0, 1 if positive |
#define cnx_sign(x)
#include <include/Cnx/Math.h>
Calculates the sign of the given input.
| Parameters | |
|---|---|
| x | - The value to get the sign of |
| Returns | -1 if negative, 0 if 0, 1 if positive |
#define cnx_gcd(left,
right)
#include <include/Cnx/Math.h>
Calculate the gcd of the two inputs.
| Parameters | |
|---|---|
| left | - One of the pair of numbers to get the gcd of |
| right | - One of the pair of numbers to get the gcd of |
| Returns | The gcd of the pair of numbers |
#define cnx_static_gcd(left,
right)
#include <include/Cnx/Math.h>
Calculate the gcd of the two inputs at compile time.
| Parameters | |
|---|---|
| left | - One of the pair of numbers to get the gcd of |
| right | - One of the pair of numbers to get the gcd of |
| Returns | The gcd of the pair of numbers, as a compile-time constant |
#define cnx_lcm(left,
right)
#include <include/Cnx/Math.h>
Calculate the lcm of the two inputs.
| Parameters | |
|---|---|
| left | - One of the pair of numbers to get the lcm of |
| right | - One of the pair of numbers to get the lcm of |
| Returns | The lcm of the pair of numbers |
#define cnx_static_lcm(left,
right)
#include <include/Cnx/Math.h>
Calculate the lcm of the two inputs at compile time.
| Parameters | |
|---|---|
| left | - One of the pair of numbers to get the lcm of |
| right | - One of the pair of numbers to get the lcm of |
| Returns | The lcm of the pair of numbers, as a compile-time constant |
#define cnx_static_checked_add(left,
right)
#include <include/Cnx/Math.h>
Performs the checked addition of left and right at compile-time.
| Parameters | |
|---|---|
| left | - The left integer to add |
| right | - The right integer to add |
| Returns | sum of left and right |
Performs the addition of left and right at compile-time, checking for overflow and triggering a compiler error if overflow occurs
#define cnx_checked_sub(left,
right)
#include <include/Cnx/Math.h>
Performs the checked subtraction of right from left
| Parameters | |
|---|---|
| left | - The number to subtract from |
| right | - The number to subtract |
| Returns | Some(T, result) if the subtraction was successful (no overflow occurred), None(T) otherwise |
Performs the checked subtraction of right from left, returning Some(T, result) if overflow did not occur or None(T) if overflow occurred, where T is the type of the passed arguments
#define cnx_static_checked_sub(left,
right)
#include <include/Cnx/Math.h>
Performs the checked subtraction of right from left at compile-time.
| Parameters | |
|---|---|
| left | - The integer to subtract from |
| right | - The integer to subtract |
| Returns | subtraction of right from left |
Performs the subtraction of right from left at compile-time, checking for overflow and triggering a compiler error if overflow occurs
#define cnx_checked_mul(left,
right)
#include <include/Cnx/Math.h>
Performs the checked multiplication of left and right
| Parameters | |
|---|---|
| left | - The left-hand number of the multiplication |
| right | - The right-hand number of the multiplication |
| Returns | Some(T, result) if the multiplication was successful (no overflow occurred), None(T) otherwise |
Performs the checked multiplication of left and right, returning Some(T, result) if overflow did not occur or None(T) if overflow occurred, where T is the type of the passed arguments
#define cnx_static_checked_mul(left,
right)
#include <include/Cnx/Math.h>
Performs the checked multiplication of left and right at compile-time.
| Parameters | |
|---|---|
| left | - The left integer to multiply |
| right | - The right integer to multiply |
| Returns | product of left and right |
Performs the multiplication of left and right at compile-time, checking for overflow and triggering a compiler error if overflow occurs
#define cnx_checked_div(left,
right)
#include <include/Cnx/Math.h>
Performs the checked division of left and right
| Parameters | |
|---|---|
| left | - The dividend |
| right | - The divisor |
| Returns | Some(T, result) if the division was successful (no overflow occurred), None(T) otherwise |
Performs the checked division of left and right, returning Some(T, result) if overflow did not occur or None(T) if overflow occurred, where T is the type of the passed arguments
#define cnx_static_checked_div(left,
right)
#include <include/Cnx/Math.h>
Performs the checked division of left and right at compile-time.
| Parameters | |
|---|---|
| left | - The integer dividend |
| right | - The integer divisor |
| Returns | quotient of left / right |
Performs the division of left and right at compile-time, checking for overflow and triggering a compiler error if overflow occurs