Skip to content

Commit 24555b8

Browse files
authoredOct 8, 2024··
fix power_by_squaring: use promote instead of type inference (#55634)
Fixes #53504 Fixes #55633
1 parent 4cdd864 commit 24555b8

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed
 

‎base/intfuncs.jl

+5-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,11 @@ function invmod(n::T) where {T<:BitInteger}
298298
end
299299

300300
# ^ for any x supporting *
301-
to_power_type(x) = convert(Base._return_type(*, Tuple{typeof(x), typeof(x)}), x)
301+
function to_power_type(x::Number)
302+
T = promote_type(typeof(x), typeof(one(x)), typeof(x*x))
303+
convert(T, x)
304+
end
305+
to_power_type(x) = oftype(x*x, x)
302306
@noinline throw_domerr_powbysq(::Any, p) = throw(DomainError(p, LazyString(
303307
"Cannot raise an integer x to a negative power ", p, ".",
304308
"\nConvert input to float.")))

‎test/math.jl

+22
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,28 @@ end
14981498
n = Int64(1024 / log2(E))
14991499
@test E^n == Inf
15001500
@test E^float(n) == Inf
1501+
1502+
# #55633
1503+
struct Issue55633_1 <: Number end
1504+
struct Issue55633_3 <: Number end
1505+
struct Issue55633_9 <: Number end
1506+
Base.one(::Issue55633_3) = Issue55633_1()
1507+
Base.:(*)(::Issue55633_3, ::Issue55633_3) = Issue55633_9()
1508+
Base.promote_rule(::Type{Issue55633_1}, ::Type{Issue55633_3}) = Int
1509+
Base.promote_rule(::Type{Issue55633_3}, ::Type{Issue55633_9}) = Int
1510+
Base.promote_rule(::Type{Issue55633_1}, ::Type{Issue55633_9}) = Int
1511+
Base.promote_rule(::Type{Issue55633_1}, ::Type{Int}) = Int
1512+
Base.promote_rule(::Type{Issue55633_3}, ::Type{Int}) = Int
1513+
Base.promote_rule(::Type{Issue55633_9}, ::Type{Int}) = Int
1514+
Base.convert(::Type{Int}, ::Issue55633_1) = 1
1515+
Base.convert(::Type{Int}, ::Issue55633_3) = 3
1516+
Base.convert(::Type{Int}, ::Issue55633_9) = 9
1517+
for x (im, pi, Issue55633_3())
1518+
p = promote(one(x), x, x*x)
1519+
for y 0:2
1520+
@test all((t -> ===(t...)), zip(x^y, p[y + 1]))
1521+
end
1522+
end
15011523
end
15021524

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

0 commit comments

Comments
 (0)
Please sign in to comment.