std/math

March 17, 2026 ยท View on GitHub

Mathematical functions including trigonometry, exponentials, and neural network activation functions.

Constants

  • std::math::PI - Pi constant (double precision)
  • std::math::PI_f - Pi constant (float precision)
  • std::math::PI_hf - Pi constant (half precision)
  • std::math::NAN - Not-a-Number (double)
  • std::math::NAN_f - Not-a-Number (float)
  • std::math::NAN_hf - Not-a-Number (half)

Basic Functions

abs

T abs<T>(T value)

Returns absolute value of a number.

sign

char sign<T>(T x)

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

getSign

char getSign<T>(T value)

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

copySign

T copySign<T>(T v1, T v2)

Returns v1 with the sign of v2.

min

T min<T>(T one, T two)

Returns the smaller of two values.

max

T max<T>(T one, T two)

Returns the larger of two values.

Rounding Functions

floor

double floor(double f)
float floor(float f)
half floor(half f)

Rounds down to nearest integer.

ceil

double ceil(double d)
float ceil(float f)
half ceil(half h)

Rounds up to nearest integer.

round

double round(double x, int n)
float round(float x, int n)

Rounds to n decimal places.

mod

double mod(double x)
float mod(float x)
half mod(half x)

Returns fractional part of a number.

Exponential and Logarithmic

exp

double exp(double x)
float exp(float x)

Returns e raised to the power x.

loge

double loge(double x)
float loge(float x)

Natural logarithm (base e).

log

double log(double x)
float log(float x)

Common logarithm (base 10).

pow

double pow(double f, double f2)
float pow(float f, float f2)

Returns f raised to the power f2.

sqrt

double sqrt(double d)
float sqrt(float f)
half sqrt(half hf)

Square root.

cbrt

double cbrt(double d)
float cbrt(float f)

Cube root.

Trigonometric Functions

sin

double sin(double x)
float sin(float x)

Sine function.

cos

double cos(double d)
float cos(float f)

Cosine function.

tan

double tan(double d)
float tan(float f)

Tangent function.

asin

double asin(double x)
float asin(float x)

Arcsine function.

acos

double acos(double d)
float acos(float f)

Arccosine function.

degToRad

double degToRad(double d)
float degToRad(float d)
half degToRad(half hf)

Converts degrees to radians.

radToDeg

double radToDeg(double r)
float radToDeg(float r)
half radToDeg(half r)

Converts radians to degrees.

Hyperbolic Functions

sinh

double sinh(double x)
float sinh(float x)

Hyperbolic sine.

cosh

double cosh(double x)
float cosh(float x)

Hyperbolic cosine.

tanh

double tanh(double x)
float tanh(float x)

Hyperbolic tangent.

asinh

double asinh(double x)
float asinh(float x)

Inverse hyperbolic sine.

acosh

double acosh(double x)
float acosh(float x)

Inverse hyperbolic cosine.

atanh

double atanh(double x)
float atanh(float x)

Inverse hyperbolic tangent.

Neural Network Functions

sigmoid

double sigmoid(double x)
float sigmoid(float x)

Sigmoid activation function: 1 / (1 + e^(-x))

dsigmoid

double dsigmoid(double x)
float dsigmoid(float x)
half dsigmoid(half x)

Derivative of sigmoid.

relu

double relu(double x)
float relu(float x)

ReLU activation: max(0, x)

drelu

double drelu(double x)
float drelu(float x)
half drelu(half x)

Derivative of ReLU.

gelu

double gelu(double x)
float gelu(float x)
half gelu(half x)

GELU activation function.

geglu

void geglu(double* input, double* output, int size)
void geglu(float* input, float* output, int size)

GeGLU activation for transformer models.

softmax

void softmax(double* x, int size)
void softmax(float* x, int size)

Softmax function (in-place).

softplus

double softplus(double x)
float softplus(float x)

Softplus activation: log(1 + e^x)

rmsnorm

void rmsnorm(double* out, double* x, double* weight, int size)
void rmsnorm(float* out, float* x, float* weight, int size)

RMS normalization for neural networks.

Utility Functions

fma

double fma(double a, double b, double c)
float fma(float a, float b, float c)
half fma(half a, half b, half c)

Fused multiply-add: (a * b) + c

isNAN

bool isNAN(double d)
bool isNAN(float f)
bool isNAN(half hf)

Checks if value is Not-a-Number.

factorial

long factorial(int n)

Returns n! (factorial).

isPrime

bool isPrime(int number)

Checks if number is prime.

erf

double erf(double x)
float erf(float x)

Error function.

Example

import <std/math>

double angle = std::math::degToRad(45.0);
double result = std::math::sin(angle);
double power = std::math::pow(2.0, 8.0);
double root = std::math::sqrt(16.0);