Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #12375 (fix scaling of isapprox) #12393

Merged
merged 1 commit into from
Jul 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ Library improvements

* The `MathConst` type has been renamed `Irrational` ([#11922]).

* `isapprox` now has simpler and more sensible default tolerances ([#12393]).

* Random numbers

* Streamlined random number generation APIs [#8246].
Expand Down Expand Up @@ -1554,3 +1556,4 @@ Too numerous to mention.
[#12087]: https://github.com/JuliaLang/julia/issues/12087
[#12137]: https://github.com/JuliaLang/julia/issues/12137
[#12162]: https://github.com/JuliaLang/julia/issues/12162
[#12393]: https://github.com/JuliaLang/julia/issues/12393
34 changes: 6 additions & 28 deletions base/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,33 +165,11 @@ for f in (:round, :ceil, :floor, :trunc)
end
end

# isapprox: Tolerant comparison of floating point numbers
function isapprox(x::AbstractFloat, y::AbstractFloat; rtol::Real=rtoldefault(x,y), atol::Real=atoldefault(x,y))
(isinf(x) || isinf(y)) ? x == y : abs(x-y) <= atol + rtol.*max(abs(x), abs(y))
end

# promotion of non-floats
isapprox(x::Real, y::AbstractFloat; rtol::Real=rtoldefault(x, y), atol::Real=atoldefault(x, y)) = isapprox(promote(x, y)...; rtol=rtol, atol=atol)
isapprox(x::AbstractFloat, y::Real; rtol::Real=rtoldefault(x, y), atol::Real=atoldefault(x, y)) = isapprox(promote(x, y)...; rtol=rtol, atol=atol)

# other real numbers
isapprox(x::Real, y::Real; rtol::Real=0, atol::Real=0) = abs(x-y) <= atol

# complex numbers
isapprox(z::Complex, w::Complex; rtol::Real=rtoldefault(abs(z), abs(w)), atol::Real=atoldefault(abs(z), abs(w))) = abs(z-w) <= atol + rtol*max(abs(z), abs(w))

# real-complex combinations
isapprox(x::Real, z::Complex; rtol::Real=rtoldefault(x, abs(z)), atol::Real=atoldefault(x, abs(z))) = isapprox(complex(x), z; rtol=rtol, atol=atol)
isapprox(z::Complex, x::Real; rtol::Real=rtoldefault(x, abs(z)), atol::Real=atoldefault(x, abs(z))) = isapprox(complex(x), z; rtol=rtol, atol=atol)
# isapprox: approximate equality of numbers
isapprox(x::Number, y::Number; rtol::Real=rtoldefault(x,y), atol::Real=0) =
x == y || (isfinite(x) && isfinite(y) && abs(x-y) <= atol + rtol*max(abs(x), abs(y)))

# default tolerance arguments
rtoldefault(x::AbstractFloat, y::AbstractFloat) = cbrt(max(eps(x), eps(y)))
atoldefault(x::AbstractFloat, y::AbstractFloat) = sqrt(max(eps(x), eps(y)))

# promotion of non-floats
for fun in (:rtoldefault, :atoldefault)
@eval begin
($fun)(x::Real, y::AbstractFloat) = ($fun)(promote(x,y)...)
($fun)(x::AbstractFloat, y::Real) = ($fun)(promote(x,y)...)
end
end
rtoldefault{T<:AbstractFloat}(::Type{T}) = sqrt(eps(T))
rtoldefault{T<:Real}(::Type{T}) = 0
rtoldefault{T<:Number,S<:Number}(x::T, y::S) = rtoldefault(promote_type(real(T),real(S)))
12 changes: 3 additions & 9 deletions doc/stdlib/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -417,17 +417,11 @@ Mathematical Operators
Mathematical Functions
----------------------

.. function:: isapprox(x::Number, y::Number; rtol::Real=cbrt(maxeps), atol::Real=sqrt(maxeps))
.. function:: isapprox(x::Number, y::Number; rtol::Real=sqrt(eps), atol::Real=0)

Inexact equality comparison - behaves slightly different depending on types of input args:
Inexact equality comparison: ``true`` if ``abs(x-y) <= atol + rtol*max(abs(x), abs(y))``. The default ``atol`` is zero and the default ``rtol`` depends on the types of ``x`` and ``y``.

* For ``AbstractFloat`` numbers, ``isapprox`` returns ``true`` if ``abs(x-y) <= atol + rtol*max(abs(x), abs(y))``.

* For ``Integer`` and ``Rational`` numbers, ``isapprox`` returns ``true`` if ``abs(x-y) <= atol``. The `rtol` argument is ignored. If one of ``x`` and ``y`` is ``AbstractFloat``, the other is promoted, and the method above is called instead.

* For ``Complex`` numbers, the distance in the complex plane is compared, using the same criterion as above.

For default tolerance arguments, ``maxeps = max(eps(abs(x)), eps(abs(y)))``.
For real or complex floating-point values, ``rtol`` defaults to ``sqrt(eps(typeof(real(x-y))))``. This corresponds to requiring equality of about half of the significand digits. For other types, ``rtol`` defaults to zero.

.. function:: sin(x)

Expand Down
12 changes: 8 additions & 4 deletions test/fastmath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,22 @@ for T in (Complex64, Complex128, Complex{BigFloat})
half = (1+1im)/T(2)
third = (1-1im)/T(3)

# some of these functions promote their result to double
# precision, but we want to check equality at precision T
rtol = Base.rtoldefault(real(T))

for f in (:+, :-, :abs, :abs2, :conj, :inv, :sign,
:acos, :acosh, :asin, :asinh, :atan, :atanh, :cos,
:cosh, :exp10, :exp2, :exp, :expm1, :log10, :log1p,
:log2, :log, :sin, :sinh, :sqrt, :tan, :tanh)
@test isapprox((@eval @fastmath $f($half)), (@eval $f($half)))
@test isapprox((@eval @fastmath $f($third)), (@eval $f($third)))
@test isapprox((@eval @fastmath $f($half)), (@eval $f($half)), rtol=rtol)
@test isapprox((@eval @fastmath $f($third)), (@eval $f($third)), rtol=rtol)
end
for f in (:+, :-, :*, :/, :(==), :!=, :^)
@test isapprox((@eval @fastmath $f($half, $third)),
(@eval $f($half, $third)))
(@eval $f($half, $third)), rtol=rtol)
@test isapprox((@eval @fastmath $f($third, $half)),
(@eval $f($third, $half)))
(@eval $f($third, $half)), rtol=rtol)
end
end

Expand Down
3 changes: 3 additions & 0 deletions test/floatapprox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@
# Notably missing from this test suite at the moment
# * Tests for other magnitudes of numbers - very small, very big, and combinations of small and big
# * Tests for various odd combinations of types, e.g. isapprox(x::Integer, y::Rational)

# issue #12375:
@test !isapprox(1e17, 1)