Issue #15811 has been updated by wishdev (John Higgins).

mrkn (Kenta Murata) wrote:
> Julia provides `isapprox` function in `Base` module.  This returns `true` if `norm(x-y) <= max(atol, rtol*max(norm(x), norm(y)))`.
> The detail documentation is [here](https://docs.julialang.org/en/v1/base/math/#Base.isapprox).
>
> The definition is [here](https://github.com/JuliaLang/julia/blob/master/base/floatfuncs.jl#L273-L277):
> ```
> function isapprox(x::Number, y::Number; atol::Real=0, rtol::Real=rtoldefault(x,y,atol), nans::Bool=false)
>     x == y || (isfinite(x) && isfinite(y) && abs(x-y) <= max(atol, rtol*max(abs(x), abs(y)))) || (nans && isnan(x) && isnan(y))
> end
>
> const  = isapprox
> ```

From https://repl.it/languages/julia (online julia repl)

isapprox(0.01-0.02, 0, atol=1e-2)
true

isapprox(0.02-0.03, 0, atol=1e-2)
true

isapprox(0.03-0.04, 0, atol=1e-2)
false

This does not work here either. Which mirrors the examples above (they take the earlier code from this ticket and treat exactly 0.01 as true for this example).

----------------------------------------
Feature #15811: Propsing new method for comparing equality of 2 (float) numbers relatively
https://bugs.ruby-lang.org/issues/15811#change-80174

* Author: yennguyenh (yen nguyen)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
# Background

Equal comparison method between 2 float numbers returns unexpected results sometimes. Therefore, a relative comparison method is needed!

# Proposal

A relative equal comparison method has been written based on a Python project! This method gives the approximation for the equal comparison based on two values: realative tolerance and absolute tolerance. Near zero value will also be considered carefully!

# Implementation

The function for that would be called close?
`close?(a, b, rel_tol, abs_tol)`

`a` and `b`: are the two values to be tested to relative closeness

`rel_tol`: is the relative tolerance -- it is the amount of error allowed, relative to the larger absolute value of a or b. For example, to set a tolerance of 5%, pass tol=0.05. The default tolerance is 1E-9, which assures that the two values are the same within about 9 decimal digits. rel_tol must be greater than 0.0

`abs_tol`: is a minimum absolute tolerance level -- useful for comparisons near zero.

By default, relative tolerance is 1E-9 which is relatively precise enough to compare two float numbers. However it can also be adjusted in case higher accuracy is requested. The absolute tolerance is by default 0.0 and need to be set in case of near-zero numbers.

# Discussion

There are some test cases available for the method which has approved the accuracy of the method. BigNumbers and integers are also tested. However, more test cases are still needed to assure even better the accuracy of the method.

# Gist

Relative equal comparison