JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.89k stars 5.49k forks source link

the error message for `^` calls all unknown types "integer" #55960

Open nsajko opened 1 month ago

nsajko commented 1 month ago
julia> π^Int(-1)
ERROR: DomainError with -1:
Cannot raise an integer x to a negative power -1.
Convert input to float.
Stacktrace:
 [1] throw_domerr_powbysq(::Float64, p::Int64)
   @ Base ./intfuncs.jl:302
 [2] power_by_squaring(x_::Irrational{:π}, p::Int64; mul::typeof(*))
   @ Base ./intfuncs.jl:327
 [3] power_by_squaring
   @ ./intfuncs.jl:316 [inlined]
 [4] ^(x::Irrational{:π}, p::Int64)
   @ Base ./intfuncs.jl:352
 [5] top-level scope
   @ REPL[1]:1

julia> isinteger(π)
false

NB: there's nothing special about π here, all unknown (not-floating-point?) types seem to be regarded as integer by the error message.

oscardssmith commented 1 month ago

The bigger issue is that this should probably just work.

nsajko commented 1 month ago

Made a separate issue about that: #55961

nsajko commented 1 month ago

Here's a standalone example for this issue, independent of Irrational:

julia> struct S <: Real end

julia> Base.:(*)(::S, ::S) = S()

julia> Base.isone(::S) = false

julia> Base.:(-)(::S) = S()

julia> e = -1; S() ^ e
ERROR: DomainError with -1:
Cannot raise an integer x to a negative power -1.
Convert input to float.
Stacktrace:
 [1] throw_domerr_powbysq(::S, p::Int64)
   @ Base ./intfuncs.jl:302
 [2] power_by_squaring(x_::S, p::Int64; mul::typeof(*))
   @ Base ./intfuncs.jl:327
 [3] power_by_squaring
   @ ./intfuncs.jl:316 [inlined]
 [4] ^(x::S, p::Int64)
   @ Base ./intfuncs.jl:352
 [5] top-level scope
   @ REPL[5]:1

julia> versioninfo()
Julia Version 1.12.0-DEV.1282
Commit dd310849adb (2024-10-01 18:56 UTC)
Build Info:
  Official https://julialang.org release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × AMD Ryzen 3 5300U with Radeon Graphics
  WORD_SIZE: 64
  LLVM: libLLVM-18.1.7 (ORCJIT, znver2)