Skip to content

Commit 027ab12

Browse files
authored
Fix order of promotion and float in pow (#52)
* Fix order of `promote` and `float` in `pow` * Add tests * Update Project.toml
1 parent 6436c4f commit 027ab12

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name = "NaNMath"
22
uuid = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
33
repo = "https://github.com/mlubin/NaNMath.jl.git"
44
authors = ["Miles Lubin"]
5-
version = "0.3.6"
5+
version = "0.3.7"
66

77
[deps]
88

src/NaNMath.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ 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-
# 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))...)
25+
# We `promote` first before converting to floating pointing numbers to ensure that
26+
# e.g. `pow(::Float32, ::Int)` ends up calling `pow(::Float32, ::Float32)`
27+
pow(x::Number, y::Number) = pow(promote(x, y)...)
28+
pow(x::T, y::T) where {T<:Number} = pow(float(x), float(y))
2829

2930
"""
3031
NaNMath.sum(A)

test/runtests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ using Test
77
@test isnan(NaNMath.pow(-1.5f0,2.3f0))
88
@test isnan(NaNMath.pow(-1.5,2.3f0))
99
@test isnan(NaNMath.pow(-1.5f0,2.3))
10+
@test NaNMath.pow(-1,2) isa Float64
11+
@test NaNMath.pow(-1.5f0,2) isa Float32
12+
@test NaNMath.pow(-1.5f0,2//1) isa Float32
1013
@test NaNMath.pow(-1.5f0,2.3f0) isa Float32
1114
@test NaNMath.pow(-1.5f0,2.3) isa Float64
15+
@test NaNMath.pow(-1.5,2) isa Float64
16+
@test NaNMath.pow(-1.5,2//1) isa Float64
1217
@test NaNMath.pow(-1.5,2.3f0) isa Float64
1318
@test NaNMath.pow(-1.5,2.3) isa Float64
1419
@test isnan(NaNMath.sqrt(-5))

0 commit comments

Comments
 (0)