Zig 0.17.0-dev (Split by item)

Shortcut keys
?
Show these tips
S
Focus search field
Esc
Clear focus & close tips
U
Jump to source code
↑
Move up in search results
↓
Move down in search results
↵
Go to active search result
This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Public Function

approxEqRel

Public Function
std.math.approxEqRel
pub fn approxEqRel(comptime T: type, x: T, y: T, tolerance: T) bool

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 max(|x|, |y|) * tolerance, where tolerance 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 usually sqrt(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.

Params

  • T: type
  • x: T
  • y: T
  • tolerance: T

File

math.zig:105
105
106
107
108
109
110
111
112
113
114
115
116
117
pub fn approxEqRel(comptime T: type, x: T, y: T, tolerance: T) bool {
    comptime 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) <= @max(@abs(x), @abs(y)) * tolerance;
}