Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix power_by_squaring: use promote instead of type inference #55634

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,11 @@ 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)
function to_power_type(x::Number)
T = promote_type(typeof(x), typeof(one(x)), typeof(x*x))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
T = promote_type(typeof(x), typeof(one(x)), typeof(x*x))
T = promote_type(typeof(x), typeof(x*x))

It just occured to me that a multiplicative identity doesn't necessarily exist:

https://en.wikipedia.org/wiki/Rng_(algebra)

Are such structures supported?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it'd be OK to require a type without a multiplicative identity to implement ^ themselves?

convert(T, x)
end
to_power_type(x) = oftype(x*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
22 changes: 22 additions & 0 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,28 @@ end
n = Int64(1024 / log2(E))
@test E^n == Inf
@test E^float(n) == Inf

# #55633
struct Issue55633_1 <: Number end
struct Issue55633_3 <: Number end
struct Issue55633_9 <: Number end
Base.one(::Issue55633_3) = Issue55633_1()
Base.:(*)(::Issue55633_3, ::Issue55633_3) = Issue55633_9()
Base.promote_rule(::Type{Issue55633_1}, ::Type{Issue55633_3}) = Int
Base.promote_rule(::Type{Issue55633_3}, ::Type{Issue55633_9}) = Int
Base.promote_rule(::Type{Issue55633_1}, ::Type{Issue55633_9}) = Int
Base.promote_rule(::Type{Issue55633_1}, ::Type{Int}) = Int
Base.promote_rule(::Type{Issue55633_3}, ::Type{Int}) = Int
Base.promote_rule(::Type{Issue55633_9}, ::Type{Int}) = Int
Base.convert(::Type{Int}, ::Issue55633_1) = 1
Base.convert(::Type{Int}, ::Issue55633_3) = 3
Base.convert(::Type{Int}, ::Issue55633_9) = 9
for x ∈ (im, pi, Issue55633_3())
p = promote(one(x), x, x*x)
for y ∈ 0:2
@test all((t -> ===(t...)), zip(x^y, p[y + 1]))
end
end
end
nsajko marked this conversation as resolved.
Show resolved Hide resolved

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