Skip to content
CShift0.32

Math

Math functions and constants: Math.Sqrt(2.0), Math.Max(a, b), Math.PI, …

double area = Math.PI * r * r;
int clamped = Math.Clamp(value, 0, 255);

The trigonometric functions use radians (Math.DegreesToRadians converts). For integer trigonometry without floating point (e.g. on a 68000) there is FastTrig.

Name Description
PI The ratio of a circle’s circumference to its diameter, π (3.14159…).
E The base of the natural logarithm, e (2.71828…).
Tau The ratio of a circle’s circumference to its radius, τ = 2π (6.28318…).
const double PI = 3.141592653589793

The ratio of a circle’s circumference to its diameter, π (3.14159…).

const double E = 2.718281828459045

The base of the natural logarithm, e (2.71828…).

const double Tau = 6.283185307179586

The ratio of a circle’s circumference to its radius, τ = 2π (6.28318…).

Name Description
Abs The absolute value of x.
Min The smaller of a and b.
Max The larger of a and b.
Clamp value limited to the range from min to max.
Sign The sign of x.
Floor The largest integer that is not larger than x (rounds towards negative infinity): Floor(-1.5) is -2.
Ceiling The smallest integer that is not smaller than x (rounds towards positive infinity): Ceiling(1.2) is 2.
Truncate x without its fractional part (rounds towards zero): Truncate(-1.7) is -1.
Round x rounded to the nearest integer; halves go to the even neighbour (2.5 becomes 2, 3.5 becomes 4), like C#.
Sqrt The square root of x.
Cbrt The cube root of x (also for a negative x).
Pow x to the power of y.
Exp e to the power of x.
Log The natural logarithm (base e) of x.
Log2 The logarithm of x to base 2.
Log10 The logarithm of x to base 10.
Hypot The length of the hypotenuse, Sqrt(x * x + y * y), without overflowing in between.
Sin The sine of the angle x (in radians).
Cos The cosine of the angle x (in radians).
Tan The tangent of the angle x (in radians).
Asin The angle (in radians, -π/2 to π/2) whose sine is x.
Acos The angle (in radians, 0 to π) whose cosine is x.
Atan The angle (in radians, -π/2 to π/2) whose tangent is x.
Atan2 The angle (in radians, -π to π) of the point (x, y): unlike Atan(y / x) it knows the quadrant and handles x == 0.
Sinh The hyperbolic sine of x.
Cosh The hyperbolic cosine of x.
Tanh The hyperbolic tangent of x.
DegreesToRadians An angle in degrees converted to radians.
RadiansToDegrees An angle in radians converted to degrees.
Lerp Linear interpolation between a and b: a + (b - a) * t, so a for t = 0 and b for t = 1.
IsNaN Whether x is NaN (“not a number”, e.g. the result of 0.0 / 0.0).
IsInfinity Whether x is positive or negative infinity.
int Abs(int x)

The absolute value of x.

int64 Abs(int64 x)

The absolute value of x.

float Abs(float x)

The absolute value of x.

double Abs(double x)

The absolute value of x.

int Min(int a, int b)

The smaller of a and b.

int64 Min(int64 a, int64 b)

The smaller of a and b.

float Min(float a, float b)

The smaller of a and b.

double Min(double a, double b)

The smaller of a and b.

int Max(int a, int b)

The larger of a and b.

int64 Max(int64 a, int64 b)

The larger of a and b.

float Max(float a, float b)

The larger of a and b.

double Max(double a, double b)

The larger of a and b.

int Clamp(int value, int min, int max)

value limited to the range from min to max.

Returns min if value is smaller, max if it is larger, otherwise value.

int64 Clamp(int64 value, int64 min, int64 max)

value limited to the range from min to max.

Returns min if value is smaller, max if it is larger, otherwise value.

float Clamp(float value, float min, float max)

value limited to the range from min to max.

Returns min if value is smaller, max if it is larger, otherwise value.

double Clamp(double value, double min, double max)

value limited to the range from min to max.

Returns min if value is smaller, max if it is larger, otherwise value.

int Sign(int x)

The sign of x.

Returns -1 for a negative x, 0 for 0, 1 for a positive x.

int Sign(double x)

The sign of x.

Returns -1 for a negative x, 0 for 0 (and NaN), 1 for a positive x.

double Floor(double x)

The largest integer that is not larger than x (rounds towards negative infinity): Floor(-1.5) is -2.

double Ceiling(double x)

The smallest integer that is not smaller than x (rounds towards positive infinity): Ceiling(1.2) is 2.

double Truncate(double x)

x without its fractional part (rounds towards zero): Truncate(-1.7) is -1.

double Round(double x)

x rounded to the nearest integer; halves go to the even neighbour (2.5 becomes 2, 3.5 becomes 4), like C#.

double Sqrt(double x)

The square root of x.

Returns NaN for a negative x.

double Cbrt(double x)

The cube root of x (also for a negative x).

double Pow(double x, double y)

x to the power of y.

double Exp(double x)

e to the power of x.

double Log(double x)

The natural logarithm (base e) of x.

Returns negative infinity for 0, NaN for a negative x.

double Log2(double x)

The logarithm of x to base 2.

Returns negative infinity for 0, NaN for a negative x.

double Log10(double x)

The logarithm of x to base 10.

Returns negative infinity for 0, NaN for a negative x.

double Hypot(double x, double y)

The length of the hypotenuse, Sqrt(x * x + y * y), without overflowing in between.

double Sin(double x)

The sine of the angle x (in radians).

double Cos(double x)

The cosine of the angle x (in radians).

double Tan(double x)

The tangent of the angle x (in radians).

double Asin(double x)

The angle (in radians, -π/2 to π/2) whose sine is x.

Returns NaN if x is outside of -1 to 1.

double Acos(double x)

The angle (in radians, 0 to π) whose cosine is x.

Returns NaN if x is outside of -1 to 1.

double Atan(double x)

The angle (in radians, -π/2 to π/2) whose tangent is x.

double Atan2(double y, double x)

The angle (in radians, -π to π) of the point (x, y): unlike Atan(y / x) it knows the quadrant and handles x == 0.

double Sinh(double x)

The hyperbolic sine of x.

double Cosh(double x)

The hyperbolic cosine of x.

double Tanh(double x)

The hyperbolic tangent of x.

double DegreesToRadians(double degrees)

An angle in degrees converted to radians.

double RadiansToDegrees(double radians)

An angle in radians converted to degrees.

double Lerp(double a, double b, double t)

Linear interpolation between a and b: a + (b - a) * t, so a for t = 0 and b for t = 1.

bool IsNaN(double x)

Whether x is NaN (“not a number”, e.g. the result of 0.0 / 0.0).

bool IsInfinity(double x)

Whether x is positive or negative infinity.