approxEqAbs
Performs an approximate comparison of two floating point values x and y.
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 of floatEps(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.
Function parameters
Parameters
- T:type
- x:T
- y:T
- tolerance:T
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 approxEqAbs(comptime T: type, x: T, y: T, tolerance: T) bool {
assert(@typeInfo(T) == .float or @typeInfo(T) == .comptime_float);
assert(tolerance >= 0);
// Fast path for equal values (and signed zeros and infinites).
if (x == y)
return true;
if (isNan(x) or isNan(y))
return false;
return @abs(x - y) <= tolerance;
}