wrap
Odd sawtooth function
|
/ | / /
/ |/ /
--/----/----/--
/ /| /
/ / | /
|
Limit x to the half-open interval [-r, r).
Function parameters
Parameters
- x:anytype
- r:anytype
Type definitions in this namespace
Types
Functions in this namespace
Functions
- approxEqAbs
- Performs an approximate comparison of two floating point values `x` and `y`.
- approxEqRel
- Performs an approximate comparison of two floating point values `x` and `y`.
- sin
- Sine trigonometric function on a floating point number.
- cos
- Cosine trigonometric function on a floating point number.
- tan
- Tangent trigonometric function on a floating point number.
- radiansToDegrees
- Converts an angle in radians to degrees.
- degreesToRadians
- Converts an angle in degrees to radians.
- exp
- Base-e exponential function on a floating point number.
- exp2
- Base-2 exponential function on a floating point number.
- Min
- Given two types, returns the smallest one which is capable of holding the
- wrap
- Odd sawtooth function
- clamp
- Odd ramp function
- mul
- Returns the product of a and b.
- add
- Returns the sum of a and b.
- sub
- Returns a - b, or an error on overflow.
- shlExact
- Shifts a left by shift_amt.
- shl
- Shifts left.
- shr
- Shifts right.
- rotr
- Rotates right.
- rotl
- Rotates left.
- Log2Int
- Returns an unsigned int type that can hold the number of bits in T - 1.
- Log2IntCeil
- Returns an unsigned int type that can hold the number of bits in T.
- IntFittingRange
- Returns the smallest integer type that can hold both from and to.
- divTrunc
- Divide numerator by denominator, rounding toward zero.
- divFloor
- Divide numerator by denominator, rounding toward negative
- divCeil
- Divide numerator by denominator, rounding toward positive
- divExact
- Divide numerator by denominator.
- mod
- Returns numerator modulo denominator, or an error if denominator is
- rem
- Returns the remainder when numerator is divided by denominator, or
- negateCast
- Returns the negation of the integer parameter.
- cast
- Cast an integer to a different integer type.
- alignCast
- Align cast a pointer but return an error if it's the wrong alignment
- isPowerOfTwo
- Asserts `int > 0`.
- ByteAlignedInt
- Aligns the given integer type bit width to a width divisible by 8.
- round
- Rounds the given floating point number to the nearest integer.
- trunc
- Rounds the given floating point number to an integer, towards zero.
- floor
- Returns the largest integral value not greater than the given floating point number.
- floorPowerOfTwo
- Returns the nearest power of two less than or equal to value, or
- ceil
- Returns the smallest integral value not less than the given floating point number.
- ceilPowerOfTwoPromote
- Returns the next power of two (if the value is not already a power of two).
- ceilPowerOfTwo
- Returns the next power of two (if the value is not already a power of two).
- ceilPowerOfTwoAssert
- Returns the next power of two (if the value is not already a power
- log2_int
- Return the log base 2 of integer value x, rounding down to the
- log2_int_ceil
- Return the log base 2 of integer value x, rounding up to the
- lossyCast
- Cast a value to a different type.
- lerp
- Performs linear interpolation between *a* and *b* based on *t*.
- maxInt
- Returns the maximum value of integer type T.
- minInt
- Returns the minimum value of integer type T.
- mulWide
- Multiply a and b.
- order
- Given two numbers, this function returns the order they are with respect to each other.
- compare
- This function does the same thing as comparison operators, however the
- boolMask
- Returns a mask of all ones if value is true,
- comptimeMod
- Return the mod of `num` with the smallest integer type
- sign
- Returns -1, 0, or 1.
Error sets in this namespace
Error Sets
Euler's number (e)
Values
- e
- Euler's number (e)
- pi
- Archimedes' constant (π)
- phi
- Phi or Golden ratio constant (Φ) = (1 + sqrt(5))/2
- tau
- Circle constant (τ)
- log2e
- log2(e)
- log10e
- log10(e)
- ln2
- ln(2)
- ln10
- ln(10)
- two_sqrtpi
- 2/sqrt(π)
- sqrt2
- sqrt(2)
- sqrt1_2
- 1/sqrt(2)
- rad_per_deg
- pi/180.0
- deg_per_rad
- 180.0/pi
Source
Implementation
pub fn wrap(x: anytype, r: anytype) @TypeOf(x) {
const info_x = @typeInfo(@TypeOf(x));
const info_r = @typeInfo(@TypeOf(r));
if (info_x == .int and info_x.int.signedness != .signed) {
@compileError("x must be floating point, comptime integer, or signed integer.");
}
switch (info_r) {
.int => {
// in the rare usecase of r not being comptime_int or float,
// take the penalty of having an intermediary type conversion,
// otherwise the alternative is to unwind iteratively to avoid overflow
const R = comptime do: {
var info = info_r;
info.int.bits += 1;
info.int.signedness = .signed;
break :do @Type(info);
};
const radius: if (info_r.int.signedness == .signed) @TypeOf(r) else R = r;
return @intCast(@mod(x - radius, 2 * @as(R, r)) - r); // provably impossible to overflow
},
else => {
return @mod(x - r, 2 * r) - r;
},
}
}