Skip to content

Commit 8cce014

Browse files
authored
Merge pull request #94 from JuliaMath/os/fix-BigFloat-pow
fix BigFloat pow
2 parents 72357e3 + 01a0d0f commit 8cce014

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/NaNMath.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ Base.@assume_effects :total pow(x::Float32, y::Float32) = ccall((:powf,libm), Fl
6868
# e.g. `pow(::Float32, ::Int)` ends up calling `pow(::Float32, ::Float32)`
6969
pow(x::Real, y::Real) = pow(promote(x, y)...)
7070
pow(x::T, y::T) where {T<:Real} = pow(float(x), float(y))
71+
function pow(x::T, y::T) where {T<:AbstractFloat}
72+
if x < 0 && !isinteger(y)
73+
return T(NaN)
74+
end
75+
return x ^ y
76+
end
7177
pow(x, y) = ^(x, y)
7278

7379
# The following combinations are safe, so we can fall back to ^

test/runtests.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ using Test
33

44
@test isnan(NaNMath.log(-10))
55
@test isnan(NaNMath.log1p(-100))
6-
@test isnan(NaNMath.pow(-1.5,2.3))
7-
@test isnan(NaNMath.pow(-1.5f0,2.3f0))
6+
for T in (Float16, Float32, Float64, BigFloat)
7+
@test isnan(NaNMath.pow(T(-1.5),T(2.3)))
8+
@test NaNMath.pow(T(-1.5),T(5)) T(-1.5) ^ T(5)
9+
end
810
@test isnan(NaNMath.pow(-1.5,2.3f0))
911
@test isnan(NaNMath.pow(-1.5f0,2.3))
1012
@test NaNMath.pow(-1.5f0,2) isa Float32

0 commit comments

Comments
 (0)