2. The Math library

Floating point math in general is not bit-precise. Compiler can optimize permutations, replace divisions with multiplications, and some of functions are not bit-exact. If you need precise math use double precision type. All functions and symbols are in “math” module, use require to get access to it.

require math

2.1. Common Symbols

2.1.1. all numerics (uint*, int*, float*, double)

min(x, y)

returns the minimum of x and y

max(x, y)

returns the minimum of x and y

2.1.2. float* and double

abs(x)

returns the absolute value of x as same time

acos(x)

returns the arccosine of x

asin(x)

returns the arcsine of x

atan(x)

returns the arctangent of x

atan2(x, y)

returns the arctangent of x/y

cos(x)

returns the cosine of x

sin(x)

returns the sine of x

tan(x)

returns the tangent of x

exp(x)

returns the exponential value of the float parameter x

log(x)

returns the natural logarithm of x

exp2(x)

returns the 2^x value of the float parameter x

log2(x)

returns the logarithm base-2 of x

pow(x, y)

returns x raised to the power of y

sqrt(x)

returns the square root of x

rcp(x)

returns the 1/x of x

PI

The numeric constant pi (3.141592) is the ratio of the circumference of a circle to its diameter

ceil(x)

returns a float value representing the smallest integer (type is still float) that is greater than or equal to x

floor(x)

returns a float value representing the largest integer that is less than or equal to x

abs(x)

returns a positive value representing with same module as x

saturate(x)

returns a clamped to [0..1] inclusive range x

2.1.3. float* only

atan2_est(x, y)

returns the faster approximation of arctangent of x/y - float only

rcp_est(x)

returns the fast approximation 1/x of x - float only

ceili(x)

returns a value representing the smallest integer (integer type!) that is greater than or equal to x

floori(x)

returns a integer value representing the largest integer that is less than or equal to x

roundi(x)

returns a integer value representing the integer that is closest to x

trunci(x)

returns a integer value representing the float without fraction part of x

2.2. Noise functions

uint32_hash(x:uint)

returns hashed value of x

uint_noise_1D(position: int; seed:uint)

returns noise value of position in seed sequence

uint_noise_2D(x, y: int; seed:uint)

returns noise value of x,y position in seed sequence

uint_noise_3D(x, y, z: int; seed:uint)

returns noise value of x,y,z position in seed sequence

2.3. Vector functions

2.3.1. float2, float3, float4:

length(x)

returns a non-negative value representing magnitude of x

length_sq(x)

returns a non-negative value representing squared magnitude of x

inv_length(x)

returns a non-negative value representing 1/magnitude of x

inv_length_sq(x)

returns a non-negative value representing 1/squared magnitude of x

distance(x, y)

returns a non-negative value representing distance between x and y

distance_sq(x)

returns a non-negative value representing squared distance between x and y

inv_distance(x, y)

returns a non-negative value representing 1/distance between x and y

inv_distance_sq(x)

returns a non-negative value representing 1/squared distance between x and y

dot(x, y)

returns scalar representating dot product between x and y

normalize(x)

returns normalized x, or nan if length(x) is 0

safe_normalize(x)

returns normalized x, or 0 if length(x) is 0

2.3.2. float3 only:

cross(x, y)

returns vector representating cross product between x and y

reflect(v, n)

returns vector representating reflection of x from n same as

def reflect(v, n: float3)
    return v - 2. * dot(v, n) * n
refract(v, n)

returns vector representating reflection of x from n same as

def refract(v, n: float3; nint: float; outRefracted: float3&)
    let dt = dot(v, n)
    let discr = 1. - nint * nint * (1. - dt * dt)
    if discr > 0.
        outRefracted = nint * (v - n * dt) - n * sqrt(discr)
        return true
    return false

2.4. lerp/madd/clamp

lerp(a, b, t)

returns vector or scalar representating a + (b - a) * t

madd(a, b, c)

returns vector or scalar representating a * b + c

clamp(t, a, b)

returns vector or scalar representating min(max(t, a), b)