Skip to content

Commit

Permalink
Solves the inconsistency JuliaLang#10729 and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Benet committed Apr 3, 2015
1 parent 757672d commit e20f0ba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
14 changes: 9 additions & 5 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,19 @@ for f in (:atan2, :hypot)
end
end

max{T<:FloatingPoint}(x::T, y::T) = ifelse((y > x) | (x != x) | (signbit(y) < signbit(x)), y, x)
max{T<:FloatingPoint}(x::T, y::T) = ifelse((y > x) | (signbit(y) < signbit(x)),
ifelse(isnan(y), x, y), ifelse(isnan(x), y, x))

@vectorize_2arg Real max

min{T<:FloatingPoint}(x::T, y::T) = ifelse((y < x) | (x != x) | (signbit(y) > signbit(x)), y, x)
min{T<:FloatingPoint}(x::T, y::T) = ifelse((y < x) | (signbit(y) > signbit(x)),
ifelse(isnan(y), x, y), ifelse(isnan(x), y, x))
@vectorize_2arg Real min

minmax{T<:FloatingPoint}(x::T, y::T) = ifelse((y < x) | (signbit(y) > signbit(x)), (y, x),
ifelse((y > x) | (signbit(y) < signbit(x)), (x, y),
ifelse(x == x, (x, x), (y, y))))
minmax{T<:FloatingPoint}(x::T, y::T) = ifelse(isnan(x-y), ifelse(isnan(x), (y, y), (x, x)),
ifelse((y < x) | (signbit(y) > signbit(x)), (y, x),
ifelse((y > x) | (signbit(y) < signbit(x)), (x, y),
ifelse(x == x, (x, x), (y, y)))))

ldexp(x::Float64,e::Integer) = ccall((:scalbn,libm), Float64, (Float64,Int32), x, Int32(e))
ldexp(x::Float32,e::Integer) = ccall((:scalbnf,libm), Float32, (Float32,Int32), x, Int32(e))
Expand Down
17 changes: 17 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
@test 2.0 * 3.0 == 6.
@test min(1.0,1) == 1

# min, max and minmax
@test minmax(5, 3) == (3, 5)
@test minmax(3., 5.) == (3., 5.)
@test minmax(5., 3.) == (3., 5.)
Expand All @@ -61,6 +62,22 @@
@test min(-0.0,0.0) === min(0.0,-0.0)
@test max(-0.0,0.0) === max(0.0,-0.0)
@test minmax(-0.0,0.0) === minmax(0.0,-0.0)
@test max(-3.2, 5.1) == max(5.1, -3.2) == 5.1
@test min(-3.2, 5.1) == min(5.1, -3.2) == -3.2
@test max(-3.2, Inf) == max(Inf, -3.2) == Inf
@test max(-3.2, NaN) == max(NaN, -3.2) == -3.2
@test min(5.1, Inf) == min(Inf, 5.1) == 5.1
@test min(5.1, -Inf) == min(-Inf, 5.1) == -Inf
@test min(5.1, NaN) == min(NaN, 5.1) == 5.1
@test min(5.1, -NaN) == min(-NaN, 5.1) == 5.1
@test minmax(-3.2, 5.1) == (min(-3.2, 5.1), max(-3.2, 5.1))
@test minmax(-3.2, Inf) == (min(-3.2, Inf), max(-3.2, Inf))
@test minmax(-3.2, NaN) == (min(-3.2, NaN), max(-3.2, NaN))
@test (max(Inf,NaN), max(-Inf,NaN), max(Inf,-NaN), max(-Inf,-NaN)) == (Inf, -Inf, Inf, -Inf)
@test (max(NaN,Inf), max(NaN,-Inf), max(-NaN,Inf), max(-NaN,-Inf)) == (Inf, -Inf, Inf, -Inf)
@test (min(Inf,NaN), min(-Inf,NaN), min(Inf,-NaN), min(-Inf,-NaN)) == (Inf, -Inf, Inf, -Inf)
@test (min(NaN,Inf), min(NaN,-Inf), min(-NaN,Inf), min(-NaN,-Inf)) == (Inf, -Inf, Inf, -Inf)
@test minmax(-Inf,NaN) == (min(-Inf,NaN), max(-Inf,NaN))

# fma
let x = Int64(7)^7
Expand Down

0 comments on commit e20f0ba

Please sign in to comment.