Skip to content

Commit

Permalink
fix power_by_squaring: use promote instead of type inference
Browse files Browse the repository at this point in the history
Fixes #55633
  • Loading branch information
nsajko committed Aug 30, 2024
1 parent da3468c commit 3974869
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ function invmod(n::T) where {T<:BitInteger}
end

# ^ for any x supporting *
to_power_type(x) = convert(Base._return_type(*, Tuple{typeof(x), typeof(x)}), x)
to_power_type(x::Number) = first(promote(x, one(x), x*x))
to_power_type(x) = x
@noinline throw_domerr_powbysq(::Any, p) = throw(DomainError(p, LazyString(
"Cannot raise an integer x to a negative power ", p, ".",
"\nConvert input to float.")))
Expand Down
14 changes: 14 additions & 0 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,20 @@ end
n = Int64(1024 / log2(E))
@test E^n == Inf
@test E^float(n) == Inf

# #55633
struct Issue55633 <: Number end
struct Issue55633Squared <: Number end
Base.:(*)(::Issue55633, ::Issue55633) = Issue55633Squared()
Base.promote_rule(::Type{Issue55633}, ::Type{Issue55633Squared}) = Int
Base.convert(::Type{Int}, ::Issue55633) = 3
Base.convert(::Type{Int}, ::Issue55633Squared) = 9
for x (im, pi, Issue55633())
p = promote(one(x), x, x*x)
for y 0:2
@test all((t -> ===(t...)), zip(x^y, p[y + 1]))
end
end
end

# Test that sqrt behaves correctly and doesn't exhibit fp80 double rounding.
Expand Down

0 comments on commit 3974869

Please sign in to comment.