Skip to content

Commit c64fad8

Browse files
nhz2mkitti
authored andcommitted
Throw OverflowError on copysign(typemin(Int)//1, 1) (JuliaLang#53395)
The default `copysign(x::Real, y::Real)` in `number.jl` works, so the incorrect method in `rational.jl` isn't needed. Here is a benchmark of the new version. ```julia using BenchmarkTools function foo!(c,a,b) c .= copysign.(a, b) nothing end N = 1000 @Btime foo!(c,a,b) setup=(c=zeros(Rational{Int},N); a=rand(Int,N).//rand(Int,N); b=fill(-1,N)) ``` On master: 406.215 ns (0 allocations: 0 bytes) On this PR: 869.327 ns (0 allocations: 0 bytes)
1 parent a76f5fe commit c64fad8

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

base/rational.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,6 @@ denominator(x::Rational) = x.den
315315

316316
sign(x::Rational) = oftype(x, sign(x.num))
317317
signbit(x::Rational) = signbit(x.num)
318-
copysign(x::Rational, y::Real) = unsafe_rational(copysign(x.num, y), x.den)
319-
copysign(x::Rational, y::Rational) = unsafe_rational(copysign(x.num, y.num), x.den)
320318

321319
abs(x::Rational) = unsafe_rational(checked_abs(x.num), x.den)
322320

test/numbers.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,9 @@ end
679679
@test copysign(big(-1), 0x02) == 1
680680
@test copysign(big(-1.0), 0x02) == 1.0
681681
@test copysign(-1//2, 0x01) == 1//2
682+
683+
# Verify overflow is checked with rational
684+
@test_throws OverflowError copysign(typemin(Int)//1, 1)
682685
end
683686

684687
@testset "isnan/isinf/isfinite" begin

0 commit comments

Comments
 (0)