Functions
fn absCast(x: anytype) switch (@typeInfo(@TypeOf(x))) { .ComptimeInt => comptime_int, .Int => |int_info| std.meta.Int(.unsigned, int_info.bits), else => @compileError("absCast only accepts integers"), }
Returns the absolute value of the integer parameter. Converts result type to un…
Returns the absolute value of the integer parameter. Converts result type to unsigned if needed and returns a value of an unsigned integer type. Use
absInt
if you want to keep your integer type signed.fn absInt(x: anytype) !@TypeOf(x)
Returns the absolute value of x, where x is a value of a signed integer type. D…
Returns the absolute value of x, where x is a value of a signed integer type. Does not convert and returns a value of a signed integer type. Use
absCast
if you want to convert the result and get an unsigned type.fn add(comptime T: type, a: T, b: T) error{Overflow}!T
Returns the sum of a and b. Returns an error on overflow.
fn alignCast(comptime alignment: u29, ptr: anytype) AlignCastError!AlignCastResult(alignment, @TypeOf(ptr))
Align cast a pointer but return an error if it’s the wrong alignment
fn approxEqAbs(comptime T: type, x: T, y: T, tolerance: T) bool
Performs an approximate comparison of two floating point values
x
andy
. Re…Performs an approximate comparison of two floating point values
x
andy
. Returns true if the absolute difference between them is less or equal than the specified tolerance.The
tolerance
parameter is the absolute tolerance used when determining if the two numbers are close enough; a good value for this parameter is a small multiple offloatEps(T)
.Note that this function is recommended for comparing small numbers around zero; using
approxEqRel
is suggested otherwise.NaN values are never considered equal to any value.
fn approxEqRel(comptime T: type, x: T, y: T, tolerance: T) bool
Performs an approximate comparison of two floating point values
x
andy
. Re…Performs an approximate comparison of two floating point values
x
andy
. Returns true if the absolute difference between them is less or equal thanmax(|x|, |y|) * tolerance
, wheretolerance
is a positive number greater than zero.The
tolerance
parameter is the relative tolerance used when determining if the two numbers are close enough; a good value for this parameter is usuallysqrt(floatEps(T))
, meaning that the two numbers are considered equal if at least half of the digits are equal.Note that for comparisons of small numbers around zero this function won’t give meaningful results, use
approxEqAbs
instead.NaN values are never considered equal to any value.
inline fn boolMask(comptime MaskInt: type, value: bool) MaskInt
Returns a mask of all ones if value is true, and a mask of all zeroes if value …
Returns a mask of all ones if value is true, and a mask of all zeroes if value is false. Compiles to one instruction for register sized integers.
fn cast(comptime T: type, x: anytype) ?T
Cast an integer to a different integer type. If the value doesn’t fit, return n…
Cast an integer to a different integer type. If the value doesn’t fit, return null.
inline fn ceil(value: anytype) @TypeOf(value)
Returns the smallest integral value not less than the given floating point numbe…
Returns the smallest integral value not less than the given floating point number. Uses a dedicated hardware instruction when available. This is the same as calling the builtin @ceil
fn ceilPowerOfTwo(comptime T: type, value: T) error{Overflow}!T
Returns the next power of two (if the value is not already a power of two). Onl…
Returns the next power of two (if the value is not already a power of two). Only unsigned integers can be used. Zero is not an allowed input. If the value doesn’t fit, returns an error.
fn ceilPowerOfTwoAssert(comptime T: type, value: T) T
Returns the next power of two (if the value is not already a power of two). Onl…
Returns the next power of two (if the value is not already a power of two). Only unsigned integers can be used. Zero is not an allowed input. Asserts that the value fits.
fn ceilPowerOfTwoPromote(comptime T: type, value: T) field_call
Returns the next power of two (if the value is not already a power of two). Onl…
Returns the next power of two (if the value is not already a power of two). Only unsigned integers can be used. Zero is not an allowed input. Result is a type with 1 more bit than the input type.
fn clamp(val: anytype, lower: anytype, upper: anytype) @TypeOf(val, lower, upper)
Limit val to the inclusive range [lower, upper].
fn compare(a: anytype, op: CompareOperator, b: anytype) bool
This function does the same thing as comparison operators, however the operator…
This function does the same thing as comparison operators, however the operator is a runtime-known enum value. Works on any operands that support comparison operators.
fn comptimeMod(num: anytype, comptime denom: comptime_int) IntFittingRange(0, denom - 1)
Return the mod of
num
with the smallest integer typefn copysign(magnitude: anytype, sign: @TypeOf(magnitude)) @TypeOf(magnitude)
No documentation provided.
inline fn cos(value: anytype) @TypeOf(value)
Cosine trigonometric function on a floating point number. Uses a dedicated hard…
Cosine trigonometric function on a floating point number. Uses a dedicated hardware instruction when available. This is the same as calling the builtin @cos
fn degreesToRadians(comptime T: type, angle_in_degrees: T) T
Converts an angle in degrees to radians. T must be a float type.
fn divCeil(comptime T: type, numerator: T, denominator: T) !T
Divide numerator by denominator, rounding toward positive infinity. Returns an …
Divide numerator by denominator, rounding toward positive infinity. Returns an error on overflow or when denominator is zero.
fn divExact(comptime T: type, numerator: T, denominator: T) !T
Divide numerator by denominator. Return an error if quotient is not an integer,…
Divide numerator by denominator. Return an error if quotient is not an integer, denominator is zero, or on overflow.
fn divFloor(comptime T: type, numerator: T, denominator: T) !T
Divide numerator by denominator, rounding toward negative infinity. Returns an …
Divide numerator by denominator, rounding toward negative infinity. Returns an error on overflow or when denominator is zero.
fn divTrunc(comptime T: type, numerator: T, denominator: T) !T
Divide numerator by denominator, rounding toward zero. Returns an error on over…
Divide numerator by denominator, rounding toward zero. Returns an error on overflow or when denominator is zero.
inline fn exp(value: anytype) @TypeOf(value)
Base-e exponential function on a floating point number. Uses a dedicated hardwa…
Base-e exponential function on a floating point number. Uses a dedicated hardware instruction when available. This is the same as calling the builtin @exp
inline fn exp2(value: anytype) @TypeOf(value)
Base-2 exponential function on a floating point number. Uses a dedicated hardwa…
Base-2 exponential function on a floating point number. Uses a dedicated hardware instruction when available. This is the same as calling the builtin @exp2
inline fn fabs(value: anytype) @TypeOf(value)
Returns the absolute value of a floating point number. Uses a dedicated hardwar…
Returns the absolute value of a floating point number. Uses a dedicated hardware instruction when available. This is the same as calling the builtin @fabs
inline fn floor(value: anytype) @TypeOf(value)
Returns the largest integral value not greater than the given floating point num…
Returns the largest integral value not greater than the given floating point number. Uses a dedicated hardware instruction when available. This is the same as calling the builtin @floor
fn floorPowerOfTwo(comptime T: type, value: T) T
Returns the nearest power of two less than or equal to value, or zero if value …
Returns the nearest power of two less than or equal to value, or zero if value is less than or equal to zero.
fn lerp(a: anytype, b: anytype, t: anytype) @TypeOf(a, b, t)
Performs linear interpolation between a and b based on t. t must be in …
Performs linear interpolation between a and b based on t. t must be in range 0.0 to 1.0. Supports floats and vectors of floats.
This does not guarantee returning b if t is 1 due to floating-point errors. This is monotonic.
fn log2_int(comptime T: type, x: T) Log2Int(T)
Return the log base 2 of integer value x, rounding down to the nearest integer.
fn log2_int_ceil(comptime T: type, x: T) Log2IntCeil(T)
Return the log base 2 of integer value x, rounding up to the nearest integer.
fn lossyCast(comptime T: type, value: anytype) T
Cast a value to a different type. If the value doesn’t fit in, or can’t be perf…
Cast a value to a different type. If the value doesn’t fit in, or can’t be perfectly represented by, the new type, it will be converted to the closest possible representation.
fn mod(comptime T: type, numerator: T, denominator: T) !T
Returns numerator modulo denominator, or an error if denominator is zero or neg…
Returns numerator modulo denominator, or an error if denominator is zero or negative. Negative numerators never result in negative return values.
fn mul(comptime T: type, a: T, b: T) error{Overflow}!T
Returns the product of a and b. Returns an error on overflow.
fn mulWide(comptime T: type, a: T, b: T) field_call
Multiply a and b. Return type is wide enough to guarantee no overflow.
fn negateCast(x: anytype) !field_call
Returns the negation of the integer parameter. Result is a signed integer.
fn order(a: anytype, b: anytype) Order
Given two numbers, this function returns the order they are with respect to each…
Given two numbers, this function returns the order they are with respect to each other.
fn radiansToDegrees(comptime T: type, angle_in_radians: T) T
Converts an angle in radians to degrees. T must be a float type.
fn rem(comptime T: type, numerator: T, denominator: T) !T
Returns the remainder when numerator is divided by denominator, or an error if …
Returns the remainder when numerator is divided by denominator, or an error if denominator is zero or negative. Negative numerators can give negative results.
fn rotl(comptime T: type, x: T, r: anytype) T
Rotates left. Only unsigned values can be rotated. Negative shift values resul…
Rotates left. Only unsigned values can be rotated. Negative shift values result in shift modulo the bit count.
fn rotr(comptime T: type, x: T, r: anytype) T
Rotates right. Only unsigned values can be rotated. Negative shift values resu…
Rotates right. Only unsigned values can be rotated. Negative shift values result in shift modulo the bit count.
inline fn round(value: anytype) @TypeOf(value)
Rounds the given floating point number to an integer, away from zero. Uses a de…
Rounds the given floating point number to an integer, away from zero. Uses a dedicated hardware instruction when available. This is the same as calling the builtin @round
fn shl(comptime T: type, a: T, shift_amt: anytype) T
Shifts left. Overflowed bits are truncated. A negative shift amount results in …
Shifts left. Overflowed bits are truncated. A negative shift amount results in a right shift.
fn shlExact(comptime T: type, a: T, shift_amt: Log2Int(T)) !T
Shifts a left by shift_amt. Returns an error on overflow. shift_amt is unsigned…
Shifts a left by shift_amt. Returns an error on overflow. shift_amt is unsigned.
fn shr(comptime T: type, a: T, shift_amt: anytype) T
Shifts right. Overflowed bits are truncated. A negative shift amount results in…
Shifts right. Overflowed bits are truncated. A negative shift amount results in a left shift.
inline fn sign(i: anytype) @TypeOf(i)
Returns -1, 0, or 1. Supports integer and float types and vectors of integer an…
Returns -1, 0, or 1. Supports integer and float types and vectors of integer and float types. Unsigned integer types will always return 0 or 1. Branchless.
inline fn sin(value: anytype) @TypeOf(value)
Sine trigonometric function on a floating point number. Uses a dedicated hardwa…
Sine trigonometric function on a floating point number. Uses a dedicated hardware instruction when available. This is the same as calling the builtin @sin
inline fn tan(value: anytype) @TypeOf(value)
Tangent trigonometric function on a floating point number. Uses a dedicated har…
Tangent trigonometric function on a floating point number. Uses a dedicated hardware instruction when available. This is the same as calling the builtin @tan
inline fn trunc(value: anytype) @TypeOf(value)
Rounds the given floating point number to an integer, towards zero. Uses a dedi…
Rounds the given floating point number to an integer, towards zero. Uses a dedicated hardware instruction when available. This is the same as calling the builtin @trunc
Values
e | comptime_float | Euler’s number (e) |
epsilon | undefined | |
f128_epsilon | undefined | |
f128_max | undefined | |
f128_min | undefined | |
f128_toint | undefined | |
f128_true_min | undefined | |
f16_epsilon | undefined | |
f16_max | undefined | |
f16_min | undefined | |
f16_toint | undefined | |
f16_true_min | undefined | |
f32_epsilon | undefined | |
f32_max | undefined | |
f32_min | undefined | |
f32_toint | undefined | |
f32_true_min | undefined | |
f64_epsilon | undefined | |
f64_max | undefined | |
f64_min | undefined | |
f64_toint | undefined | |
f64_true_min | undefined | |
f80_epsilon | undefined | |
f80_max | undefined | |
f80_min | undefined | |
f80_toint | undefined | |
f80_true_min | undefined | |
inf_f128 | undefined | |
inf_f16 | undefined | |
inf_f32 | undefined | |
inf_f64 | undefined | |
inf_f80 | undefined | |
inf_u128 | undefined | |
inf_u16 | undefined | |
inf_u32 | undefined | |
inf_u64 | undefined | |
ln | undefined | |
ln10 | comptime_float | ln(10) |
ln2 | comptime_float | ln(2) |
log10e | comptime_float | log10(e) |
log2e | comptime_float | log2(e) |
max | undefined | |
max3 | undefined | |
min | undefined | |
min3 | undefined | |
modf32_result | undefined | |
modf64_result | undefined | |
nan_f128 | f128 | |
nan_f16 | f16 | |
nan_f32 | f32 | |
nan_f64 | f64 | |
nan_f80 | undefined | |
nan_u128 | u128 | |
nan_u16 | u16 | |
nan_u32 | u32 | |
nan_u64 | type | |
phi | comptime_float | Phi or Golden ratio constant (Φ) = (1 + sqrt(5))/2 |
pi | comptime_float | Archimedes’ constant (π) |
qnan_f128 | f128 | |
qnan_f16 | f16 | |
qnan_f32 | f32 | |
qnan_f64 | f64 | |
qnan_f80 | undefined | |
qnan_u128 | u128 | |
qnan_u16 | u16 | |
qnan_u32 | u32 | |
qnan_u64 | u64 | |
sqrt1_2 | comptime_float | 1/sqrt(2) |
sqrt2 | comptime_float | sqrt(2) |
tau | type | Circle constant (τ) |
two_sqrtpi | comptime_float | 2/sqrt(π) |