Skip to content

Commit 0e5aaa6

Browse files
authored
Fix for #47 (#50)
* fix for #47 * added tests * added promote to pow
1 parent 0fc4df8 commit 0e5aaa6

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/NaNMath.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ sqrt(x::Real) = x < 0.0 ? NaN : Base.sqrt(x)
2222
# Don't override built-in ^ operator
2323
pow(x::Float64, y::Float64) = ccall((:pow,libm), Float64, (Float64,Float64), x, y)
2424
pow(x::Float32, y::Float32) = ccall((:powf,libm), Float32, (Float32,Float32), x, y)
25-
pow(x::Number,y::Number) = pow(float(x),float(y))
25+
# Need `promote` here, otherwise we'll end up with an infinite recursion
26+
# in the case of `x` and `y` being different floating types.
27+
pow(x::Number, y::Number) = pow(promote(float(x), float(y))...)
2628

2729
"""
2830
NaNMath.sum(A)

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ using Test
44
@test isnan(NaNMath.log(-10))
55
@test isnan(NaNMath.log1p(-100))
66
@test isnan(NaNMath.pow(-1.5,2.3))
7+
@test isnan(NaNMath.pow(-1.5f0,2.3f0))
8+
@test isnan(NaNMath.pow(-1.5,2.3f0))
9+
@test isnan(NaNMath.pow(-1.5f0,2.3))
10+
@test NaNMath.pow(-1.5f0,2.3f0) isa Float32
11+
@test NaNMath.pow(-1.5f0,2.3) isa Float64
12+
@test NaNMath.pow(-1.5,2.3f0) isa Float64
13+
@test NaNMath.pow(-1.5,2.3) isa Float64
714
@test isnan(NaNMath.sqrt(-5))
815
@test NaNMath.sqrt(5) == Base.sqrt(5)
916
@test NaNMath.sum([1., 2., NaN]) == 3.0

0 commit comments

Comments
 (0)