Skip to content

Commit 4c2aa17

Browse files
RalphASsimonbyrne
authored andcommitted
handle negative integer powers (#23)
1 parent 7f88c70 commit 4c2aa17

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/Quadmath.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,15 @@ round(x::Float128, r::RoundingMode{:ToZero}) = round(x)
324324
## two argument
325325
(^)(x::Float128, y::Float128) =
326326
Float128(@ccall(libquadmath.powq(x::Cfloat128, y::Cfloat128)::Cfloat128))
327+
328+
# circumvent a failure in Base
329+
function (^)(x::Float128, p::Integer)
330+
if p >= 0
331+
Base.power_by_squaring(x,p)
332+
else
333+
Base.power_by_squaring(inv(x),-p)
334+
end
335+
end
327336
copysign(x::Float128, y::Float128) =
328337
Float128(@ccall(libquadmath.copysignq(x::Cfloat128, y::Cfloat128)::Cfloat128))
329338
hypot(x::Float128, y::Float128) =

test/runtests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ end
9595
@test fma(x,x,Float128(-1.0)) Float128(1)
9696
end
9797

98+
@testset "misc. math" begin
99+
x = sqrt(Float128(2.0))
100+
@test abs(x^(-2) - Float128(0.5)) < 1.0e-32
101+
end
102+
98103
@testset "string conversion" begin
99104
s = string(Float128(3.0))
100105
p = r"3\.0+e\+0+"

0 commit comments

Comments
 (0)