Skip to content

fix Float32/Float16 power for giant integer exponents #57829

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

Merged
merged 4 commits into from
Mar 26, 2025
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
44 changes: 33 additions & 11 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,8 @@ end
return @inline Base.Math.exp_impl(hi, xylo-(hi-xyhi), Val(:ℯ))
end

# @constprop aggressive to help the compiler see the switch between the integer and float
# variants for callers with constant `y`
@constprop :aggressive function ^(x::T, y::T) where T <: Union{Float16, Float32}
x == 1 && return one(T)
# Exponents greater than this will always overflow or underflow.
Expand All @@ -1183,17 +1185,31 @@ end
y = sign(y)*max_exp
end
yint = unsafe_trunc(Int32, y) # This is actually safe since julia freezes the result
y == yint && return x^yint
x < 0 && throw_exp_domainerror(x)
!isfinite(x) && return x*(y>0 || isnan(x))
x==0 && return abs(y)*T(Inf)*(!(y>0))
return pow_body(x, y)
yisint = y == yint
if yisint
yint == 0 && return one(T)
use_power_by_squaring(yint) && return pow_body(x, yint)
end
s = 1
if x < 0
!yisint && throw_exp_domainerror(x) # y isn't an integer
s = ifelse(isodd(yint), -1, 1)
end
!isfinite(x) && return copysign(x,s)*(y>0 || isnan(x)) # x is inf or NaN
return copysign(pow_body(abs(x), y), s)
end

@inline function pow_body(x::T, y::T) where T <: Union{Float16, Float32}
@inline function pow_body(x::T, y) where T <: Union{Float16, Float32}
return T(exp2(log2(abs(widen(x))) * y))
end

@inline function pow_body(x::Union{Float16, Float32}, n::Int32)
n == -2 && return (i=inv(x); i*i)
n == 3 && return x*x*x #keep compatibility with literal_pow
n < 0 && return oftype(x, Base.power_by_squaring(inv(widen(x)), -n))
return oftype(x, Base.power_by_squaring(widen(x), n))
end

@constprop :aggressive @inline function ^(x::Float64, n::Integer)
n = clamp(n, Int64)
n == 0 && return one(x)
Expand Down Expand Up @@ -1242,11 +1258,17 @@ end
return ifelse(isfinite(x) & isfinite(err), muladd(x, y, err), x*y)
end

function ^(x::Union{Float16,Float32}, n::Integer)
n == -2 && return (i=inv(x); i*i)
n == 3 && return x*x*x #keep compatibility with literal_pow
n < 0 && return oftype(x, Base.power_by_squaring(inv(widen(x)),-n))
oftype(x, Base.power_by_squaring(widen(x),n))
# @constprop aggressive to help the compiler see the switch between the integer and float
# variants for callers with constant `y`
@constprop :aggressive @inline function ^(x::T, n::Integer) where T <: Union{Float16, Float32}
n = clamp(n, Int32)
# Exponents greater than this will always overflow or underflow.
# Note that NaN can pass through this, but that will end up fine.
n == 0 && return one(x)
use_power_by_squaring(n) && return pow_body(x, n)
s = ifelse(x < 0 && isodd(n), -one(T), one(T))
x = abs(x)
return pow_body(x, widen(T)(n))
end

## rem2pi-related calculations ##
Expand Down
28 changes: 28 additions & 0 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1443,6 +1443,7 @@ end
Float32=>[.51, .51, .51, 2.0, 1.5],
Float64=>[.55, 0.8, 1.5, 2.0, 1.5])
for T in (Float16, Float32, Float64)
@inferred T T(1.1)^T(1.1) #test that we always return the right type
for x in (0.0, -0.0, 1.0, 10.0, 2.0, Inf, NaN, -Inf, -NaN)
for y in (0.0, -0.0, 1.0, -3.0,-10.0 , Inf, NaN, -Inf, -NaN)
got, expected = T(x)^T(y), T(big(x)^T(y))
Expand Down Expand Up @@ -1547,6 +1548,33 @@ end
@test EvenInteger(16) === @inferred EvenInteger(2)^Int(4) # avoid `literal_pow`
@test EvenInteger(16) === @inferred EvenInteger(2)^EvenInteger(4)
end

# issue #57464
@test Float32(1.1)^typemin(Int) == Float32(0.0)
@test Float16(1.1)^typemin(Int) == Float16(0.0)
@test Float32(1.1)^unsigned(0) === Float32(1.0)
@test Float32(1.1)^big(0) === Float32(1.0)

# By using a limited-precision integer (3 bits) we can trigger issue 57464
# for a case where the answer isn't zero.
struct Int3 <: Integer
x::Int8
function Int3(x::Integer)
if x < -4 || x > 3
Core.throw_inexacterror(:Int3, Int3, x)
end
return new(x)
end
end
Base.typemin(::Type{Int3}) = Int3(-4)
Base.promote_rule(::Type{Int3}, ::Type{T}) where {T<:Integer} = T
Base.convert(::Type{T}, x::Int3) where {T<:Integer} = convert(T, x.x)
Base.:-(x::Int3) = x.x == -4 ? x : Int3(-x.x)
Base.trailing_zeros(x::Int3) = trailing_zeros(x.x)
Base.:>>(x::Int3, n::UInt64) = Int3(x.x>>n)

@test 1.001f0^-3 == 1.001f0^Int3(-3)
@test 1.001f0^-4 == 1.001f0^typemin(Int3)
end

@testset "special function `::Real` fallback shouldn't recur without bound, issue #57789" begin
Expand Down