JuliaLang / julia

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

`Int8(8) // Int8(100)im == -50im` due to overflow #53435

Open nhz2 opened 9 months ago

nhz2 commented 9 months ago

The current method for // on Integer and Complex{<:Integer} can overflow, silently giving an incorrect answer. For example:

julia> Int8(8) // Int8(100)im
0//1 - 50//1*im

The expected answer is 0//1 - 2//25*im

The issue is caused by abs2 and conj on Complex{Int8} silently overflowing in: https://github.com/JuliaLang/julia/blob/ccba6c91e24ebc0940acc2f8c390e6391ce0e292/base/rational.jl#L101

One solution could be to specialize this method on the many different combinations of Complex{<:Rational}, Complex{<:Integer}, Rational, Integer, and Number for the inputs, and check for overflow when doing any Integer math. Maybe there is a nicer way to fix this issue.

nsajko commented 1 month ago

The offending oneliner is buggy in other ways, too: #56245.