Skip to content

Commit 3e293bc

Browse files
MoelfDilumAluthgeLilithHafneroscardssmithgiordano
authored
Change mod(x, Inf) semantics to align with C when x is finite (#47102)
fix #46694 --------- Co-authored-by: Dilum Aluthge <dilum@aluthge.com> Co-authored-by: Lilith Orion Hafner <lilithhafner@gmail.com> Co-authored-by: Oscar Smith <oscardssmith@gmail.com> Co-authored-by: Mosè Giordano <765740+giordano@users.noreply.github.com>
1 parent dc8bc73 commit 3e293bc

File tree

4 files changed

+45
-32
lines changed

4 files changed

+45
-32
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ New language features
88

99
Language changes
1010
----------------
11+
* `mod(x::AbstractFloat, -Inf)` now returns `x` (as long as `x` is finite), this aligns with C standard and
12+
is considered a bug fix ([#47102])
1113

1214
Compiler/Runtime improvements
1315
-----------------------------

base/float.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,10 @@ function rem(x::T, y::T) where {T<:IEEEFloat}
603603
end
604604
end
605605

606-
function mod(x::T, y::T) where {T<:AbstractFloat}
606+
function mod(x::T, y::T) where T<:AbstractFloat
607+
if isinf(y) && isfinite(x)
608+
return x
609+
end
607610
r = rem(x,y)
608611
if r == 0
609612
copysign(r,y)

base/int.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ end
250250
The reduction of `x` modulo `y`, or equivalently, the remainder of `x` after floored
251251
division by `y`, i.e. `x - y*fld(x,y)` if computed without intermediate rounding.
252252
253-
The result will have the same sign as `y`, and magnitude less than `abs(y)` (with some
253+
The result will have the same sign as `y` if `isfinite(y)`, and magnitude less than `abs(y)` (with some
254254
exceptions, see note below).
255255
256256
!!! note

test/numbers.jl

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,36 +1595,44 @@ end
15951595
end
15961596
end
15971597

1598-
for x=0:5, y=1:5
1599-
@test div(UInt(x),UInt(y)) == div(x,y)
1600-
@test div(UInt(x),y) == div(x,y)
1601-
@test div(x,UInt(y)) == div(x,y)
1602-
@test div(UInt(x),-y) == reinterpret(UInt,div(x,-y))
1603-
@test div(-x,UInt(y)) == div(-x,y)
1604-
1605-
@test fld(UInt(x),UInt(y)) == fld(x,y)
1606-
@test fld(UInt(x),y) == fld(x,y)
1607-
@test fld(x,UInt(y)) == fld(x,y)
1608-
@test fld(UInt(x),-y) == reinterpret(UInt,fld(x,-y))
1609-
@test fld(-x,UInt(y)) == fld(-x,y)
1610-
1611-
@test cld(UInt(x),UInt(y)) == cld(x,y)
1612-
@test cld(UInt(x),y) == cld(x,y)
1613-
@test cld(x,UInt(y)) == cld(x,y)
1614-
@test cld(UInt(x),-y) == reinterpret(UInt,cld(x,-y))
1615-
@test cld(-x,UInt(y)) == cld(-x,y)
1616-
1617-
@test rem(UInt(x),UInt(y)) == rem(x,y)
1618-
@test rem(UInt(x),y) == rem(x,y)
1619-
@test rem(x,UInt(y)) == rem(x,y)
1620-
@test rem(UInt(x),-y) == rem(x,-y)
1621-
@test rem(-x,UInt(y)) == rem(-x,y)
1622-
1623-
@test mod(UInt(x),UInt(y)) == mod(x,y)
1624-
@test mod(UInt(x),y) == mod(x,y)
1625-
@test mod(x,UInt(y)) == mod(x,y)
1626-
@test mod(UInt(x),-y) == mod(x,-y)
1627-
@test mod(-x,UInt(y)) == mod(-x,y)
1598+
@test isnan(mod(NaN, Inf))
1599+
@test isnan(mod(NaN, -Inf))
1600+
for x=0:5
1601+
@test mod(x, Inf) == x
1602+
@test mod(x, -Inf) == x
1603+
@test mod(-x, Inf) == -x
1604+
@test mod(-x, -Inf) == -x
1605+
for y=1:5
1606+
@test div(UInt(x),UInt(y)) == div(x,y)
1607+
@test div(UInt(x),y) == div(x,y)
1608+
@test div(x,UInt(y)) == div(x,y)
1609+
@test div(UInt(x),-y) == reinterpret(UInt,div(x,-y))
1610+
@test div(-x,UInt(y)) == div(-x,y)
1611+
1612+
@test fld(UInt(x),UInt(y)) == fld(x,y)
1613+
@test fld(UInt(x),y) == fld(x,y)
1614+
@test fld(x,UInt(y)) == fld(x,y)
1615+
@test fld(UInt(x),-y) == reinterpret(UInt,fld(x,-y))
1616+
@test fld(-x,UInt(y)) == fld(-x,y)
1617+
1618+
@test cld(UInt(x),UInt(y)) == cld(x,y)
1619+
@test cld(UInt(x),y) == cld(x,y)
1620+
@test cld(x,UInt(y)) == cld(x,y)
1621+
@test cld(UInt(x),-y) == reinterpret(UInt,cld(x,-y))
1622+
@test cld(-x,UInt(y)) == cld(-x,y)
1623+
1624+
@test rem(UInt(x),UInt(y)) == rem(x,y)
1625+
@test rem(UInt(x),y) == rem(x,y)
1626+
@test rem(x,UInt(y)) == rem(x,y)
1627+
@test rem(UInt(x),-y) == rem(x,-y)
1628+
@test rem(-x,UInt(y)) == rem(-x,y)
1629+
1630+
@test mod(UInt(x),UInt(y)) == mod(x,y)
1631+
@test mod(UInt(x),y) == mod(x,y)
1632+
@test mod(x,UInt(y)) == mod(x,y)
1633+
@test mod(UInt(x),-y) == mod(x,-y)
1634+
@test mod(-x,UInt(y)) == mod(-x,y)
1635+
end
16281636
end
16291637

16301638
@test div(typemax(UInt64) , 1) == typemax(UInt64)

0 commit comments

Comments
 (0)