Skip to content

Commit 8f9c709

Browse files
committed
Merge remote-tracking branch 'origin/master' into mbar-round
2 parents 5f0c0b6 + 933367b commit 8f9c709

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "FixedPointDecimals"
22
uuid = "fb4d412d-6eee-574d-9565-ede6634db7b0"
33
authors = ["Fengyang Wang <fengyang.wang.0@gmail.com>", "Curtis Vogt <curtis.vogt@gmail.com>"]
4-
version = "0.6.1"
4+
version = "0.6.2"
55

66
[deps]
77
BitIntegers = "c3b6d118-76ef-56ca-8cc7-ebb389d030a1"

src/FixedPointDecimals.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ function Base.Checked.mul_with_overflow(x::FD{T,f}, y::FD{T,f}) where {T<:Intege
430430
powt = coefficient(FD{T, f})
431431
quotient, remainder = fldmodinline(_widemul(x.i, y.i), powt)
432432
v = _round_to_nearest(quotient, remainder, powt)
433-
return (reinterpret(FD{T,f}, Base.trunc_int(T, v)), v < typemin(T) || v > typemax(T))
433+
return (reinterpret(FD{T,f}, rem(v, T)), v < typemin(T) || v > typemax(T))
434434
end
435435

436436
# This does not exist in Base so is just part of this package.
@@ -445,7 +445,7 @@ overflow/underflow did in fact happen. Throws a DivideError on divide-by-zero.
445445
function div_with_overflow(x::FD{T,f}, y::FD{T,f}) where {T<:Integer,f}
446446
C = coefficient(FD{T, f})
447447
# This case will break the div call below.
448-
if x.i == typemin(T) && y.i == -1
448+
if y.i == -1 && T <: Signed && hasmethod(typemin, (Type{T},)) && x.i == typemin(T)
449449
# To perform the div and overflow means reaching the max and adding 1, so typemin.
450450
return (x, true)
451451
end
@@ -469,9 +469,8 @@ See also:
469469
"""
470470
function fld_with_overflow(x::FD{T,f}, y::FD{T,f}) where {T<:Integer,f}
471471
C = coefficient(FD{T, f})
472-
# This case will break the fld call below. This can only happen when f is 0 as y.i
473-
# cannot be -1 otherwise.
474-
if T <: Signed && x.i == typemin(T) && y.i == -1
472+
# This case will break the fld call below.
473+
if y.i == -1 && T <: Signed && hasmethod(typemin, (Type{T},)) && x.i == typemin(T)
475474
# To fld and overflow means reaching the max and adding 1, so typemin (x).
476475
return (x, true)
477476
end
@@ -514,7 +513,7 @@ function rdiv_with_overflow(x::Integer, y::FD{T, f}) where {T<:Integer, f}
514513
return (reinterpret(FD{T,f}, rem(v, T)), v < typemin(T) || v > typemax(T))
515514
end
516515
function rdiv_with_overflow(x::FD{T, f}, y::Integer) where {T<:Integer, f}
517-
if T <: Signed && x.i == typemin(T) && y == -1
516+
if y == -1 && T <: Signed && hasmethod(typemin, (Type{T},)) && x.i == typemin(T)
518517
# typemin / -1 for signed integers wraps, giving typemin (x) again.
519518
return (x, true)
520519
end

test/FixedDecimal.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,10 +796,13 @@ end
796796
@test rdiv_with_overflow(FD{Int16,2}(-165), FD{Int16,2}(0.5)) == (FD{Int16,2}(325.36), true)
797797
@test rdiv_with_overflow(typemin(FD{Int64,8}), Int32(-1)) == (typemin(FD{Int64,8}), true)
798798
@test rdiv_with_overflow(typemin(FD{Int64,0}), FD{Int64,0}(-1)) == (typemin(FD{Int64,0}), true)
799+
@test rdiv_with_overflow(typemin(FD{Int8,2}), FD{Int8,2}(-1)) == (typemin(FD{Int8,2}), true)
800+
@test rdiv_with_overflow(typemin(FD{Int8,2}), FD{Int8,2}(-0.01)) == (FD{Int8,2}(0), true)
799801

800802
@test fld_with_overflow(FD{Int8,2}(-1), FD{Int8,2}(0.9)) == (FD{Int8,2}(0.56), true)
801803
@test fld_with_overflow(typemin(FD{Int64,0}), FD{Int64,0}(-1)) == (typemin(FD{Int64,0}), true)
802804
@test fld_with_overflow(FD{Int8,1}(7), FD{Int8,1}(0.5)) == (FD{Int8,1}(-11.6), true)
805+
@test FixedPointDecimals.fld_with_overflow(typemin(FD{Int8,2}), FD{Int8,2}(-0.01)) == (typemin(FD{Int8,2}), true)
803806

804807
@testset "with_overflow math corner cases" begin
805808
@testset for I in (Int128, UInt128, Int8, UInt8), f in (0,2)
@@ -884,6 +887,7 @@ end
884887
@test fld_with_overflow(FD{Int64,8}(20.5), FD{Int64,8}(2.1)) == (FD{Int64,8}(9), false)
885888
@test fld_with_overflow(FD{Int8,0}(-5), FD{Int8,0}(-1)) == (FD{Int8,0}(5), false)
886889
@test fld_with_overflow(FD{Int8,2}(0.99), FD{Int8,2}(0.5)) == (FD{Int8,2}(1), false)
890+
@test fld_with_overflow(typemin(FD{Int8,2}), FD{Int8,2}(-1)) == (FD{Int8,2}(1), false)
887891
end
888892
end
889893

0 commit comments

Comments
 (0)