Skip to content

Commit a24500f

Browse files
Merge pull request #83 from JuliaMath/dw/pow_int_complex
Add safe fallbacks of `pow` for integer exponents and complex numbers
2 parents 20bd888 + f513cbf commit a24500f

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
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/JuliaMath/NaNMath.jl.git"
44
authors = ["Miles Lubin"]
5-
version = "1.0.2"
5+
version = "1.0.3"
66

77
[deps]
88
OpenLibm_jll = "05823500-19ac-5b8b-9628-191a04bc5112"

src/NaNMath.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ pow(x::Float32, y::Float32) = ccall((:powf,libm), Float32, (Float32,Float32), x,
2525
pow(x::Number, y::Number) = pow(promote(x, y)...)
2626
pow(x::T, y::T) where {T<:Number} = pow(float(x), float(y))
2727

28+
# The following combinations are safe, so we can fall back to ^
29+
pow(x::Number, y::Integer) = x^y
30+
pow(x::Complex, y::Complex) = x^y
31+
2832
"""
2933
NaNMath.sum(A)
3034

test/runtests.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ 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
1110
@test NaNMath.pow(-1.5f0,2) isa Float32
1211
@test NaNMath.pow(-1.5f0,2//1) isa Float32
1312
@test NaNMath.pow(-1.5f0,2.3f0) isa Float32
@@ -16,6 +15,11 @@ using Test
1615
@test NaNMath.pow(-1.5,2//1) isa Float64
1716
@test NaNMath.pow(-1.5,2.3f0) isa Float64
1817
@test NaNMath.pow(-1.5,2.3) isa Float64
18+
@test NaNMath.pow(-1,2) === 1
19+
@test NaNMath.pow(2,2) === 4
20+
@test NaNMath.pow(1.0, 1.0+im) === 1.0 + 0.0im
21+
@test NaNMath.pow(1.0+im, 1) === 1.0 + 1.0im
22+
@test NaNMath.pow(1.0+im, 1.0) === 1.0 + 1.0im
1923
@test isnan(NaNMath.sqrt(-5))
2024
@test NaNMath.sqrt(5) == Base.sqrt(5)
2125
@test isnan(NaNMath.sqrt(-3.2f0)) && NaNMath.sqrt(-3.2f0) isa Float32

0 commit comments

Comments
 (0)