Skip to content

Commit ca7f8ce

Browse files
mbarbarNHDaly
andauthored
Remove promoting with_overflow overloads (#105)
* Remove promoting with_overflow functions. * Bump to 0.6 --------- Co-authored-by: Nathan Daly <NHDaly@gmail.com>
1 parent cdd6966 commit ca7f8ce

File tree

3 files changed

+19
-49
lines changed

3 files changed

+19
-49
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.5.6"
4+
version = "0.6.0"
55

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

src/FixedPointDecimals.jl

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -419,27 +419,18 @@ function Base.add_with_overflow(x::T, y::T) where {T<:FD}
419419
z, b = Base.add_with_overflow(x.i, y.i)
420420
return (reinterpret(T, z), b)
421421
end
422-
Base.Checked.add_with_overflow(x::FD, y::FD) = Base.Checked.add_with_overflow(promote(x, y)...)
423-
Base.Checked.add_with_overflow(x::FD, y) = Base.Checked.add_with_overflow(promote(x, y)...)
424-
Base.Checked.add_with_overflow(x, y::FD) = Base.Checked.add_with_overflow(promote(x, y)...)
425422

426423
function Base.sub_with_overflow(x::T, y::T) where {T<:FD}
427424
z, b = Base.sub_with_overflow(x.i, y.i)
428425
return (reinterpret(T, z), b)
429426
end
430-
Base.Checked.sub_with_overflow(x::FD, y::FD) = Base.Checked.sub_with_overflow(promote(x, y)...)
431-
Base.Checked.sub_with_overflow(x::FD, y) = Base.Checked.sub_with_overflow(promote(x, y)...)
432-
Base.Checked.sub_with_overflow(x, y::FD) = Base.Checked.sub_with_overflow(promote(x, y)...)
433427

434428
function Base.Checked.mul_with_overflow(x::FD{T,f}, y::FD{T,f}) where {T<:Integer,f}
435429
powt = coefficient(FD{T, f})
436430
quotient, remainder = fldmodinline(_widemul(x.i, y.i), powt)
437431
v = _round_to_nearest(quotient, remainder, powt)
438432
return (reinterpret(FD{T,f}, Base.trunc_int(T, v)), v < typemin(T) || v > typemax(T))
439433
end
440-
Base.Checked.mul_with_overflow(x::FD, y::FD) = Base.Checked.mul_with_overflow(promote(x, y)...)
441-
Base.Checked.mul_with_overflow(x::FD, y) = Base.Checked.mul_with_overflow(promote(x, y)...)
442-
Base.Checked.mul_with_overflow(x, y::FD) = Base.Checked.mul_with_overflow(promote(x, y)...)
443434

444435
# This does not exist in Base so is just part of this package.
445436
# Throws on divide by zero.

test/FixedDecimal.jl

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -779,17 +779,11 @@ end
779779
@testset "limits: with_overflow math" begin
780780
# Easy to reason about cases of overflow:
781781
@test Base.Checked.add_with_overflow(FD{Int8,2}(1), FD{Int8,2}(1)) == (FD{Int8,2}(-0.56), true)
782-
@test Base.Checked.add_with_overflow(FD{Int8,2}(1), 1) == (FD{Int8,2}(-0.56), true)
782+
@test Base.Checked.add_with_overflow(FD{Int8,2}(1), FD{Int8,2}(1)) == (FD{Int8,2}(-0.56), true)
783783
@test Base.Checked.add_with_overflow(FD{Int8,2}(1), FD{Int8,2}(0.4)) == (FD{Int8,2}(-1.16), true)
784-
785784
@test Base.Checked.sub_with_overflow(FD{Int8,2}(1), FD{Int8,2}(-1)) == (FD{Int8,2}(-0.56), true)
786-
@test Base.Checked.sub_with_overflow(1, FD{Int8,2}(-1)) == (FD{Int8,2}(-0.56), true)
787785
@test Base.Checked.sub_with_overflow(FD{Int8,2}(-1), FD{Int8,2}(0.4)) == (FD{Int8,2}(1.16), true)
788-
789786
@test Base.Checked.mul_with_overflow(FD{Int8,2}(1.2), FD{Int8,2}(1.2)) == (FD{Int8,2}(-1.12), true)
790-
@test Base.Checked.mul_with_overflow(FD{Int8,1}(12), 2) == (FD{Int8,1}(-1.6), true)
791-
@test Base.Checked.mul_with_overflow(FD{Int8,0}(120), 2) == (FD{Int8,0}(-16), true)
792-
@test Base.Checked.mul_with_overflow(120, FD{Int8,0}(2)) == (FD{Int8,0}(-16), true)
793787

794788
@test div_with_overflow(FD{Int8,2}(1), FD{Int8,2}(0.5)) == (FD{Int8,2}(-0.56), true)
795789
@test div_with_overflow(typemin(FD{Int32,0}), FD{Int32,0}(-1)) == (typemin(FD{Int32,0}), true)
@@ -802,23 +796,23 @@ end
802796

803797
@test Base.Checked.add_with_overflow(typemax(T), eps(T)) == (typemax(T) + eps(T), true)
804798
issigned(I) && @test Base.Checked.add_with_overflow(typemin(T), -eps(T)) == (typemin(T) + -eps(T), true)
805-
@test Base.Checked.add_with_overflow(typemax(T), 1) == (typemax(T) + 1, true)
806-
@test Base.Checked.add_with_overflow(1, typemax(T)) == (typemax(T) + 1, true)
799+
@test Base.Checked.add_with_overflow(typemax(T), T(1)) == (typemax(T) + 1, true)
800+
@test Base.Checked.add_with_overflow(T(1), typemax(T)) == (typemax(T) + 1, true)
807801

808802
@test Base.Checked.sub_with_overflow(typemin(T), eps(T)) == (typemin(T) - eps(T), true)
809803
issigned(I) && @test Base.Checked.sub_with_overflow(typemax(T), -eps(T)) == (typemax(T) - -eps(T), true)
810-
@test Base.Checked.sub_with_overflow(typemin(T), 1) == (typemin(T) - 1, true)
804+
@test Base.Checked.sub_with_overflow(typemin(T), T(1)) == (typemin(T) - 1, true)
811805
if issigned(I) && 2.0 <= typemax(T)
812-
@test Base.Checked.sub_with_overflow(-2, typemax(T)) == (-2 -typemax(T), true)
806+
@test Base.Checked.sub_with_overflow(T(-2), typemax(T)) == (-2 -typemax(T), true)
813807
end
814808

815809
@test Base.Checked.mul_with_overflow(typemax(T), typemax(T)) == (typemax(T) * typemax(T), true)
816810
issigned(I) && @test Base.Checked.mul_with_overflow(typemin(T), typemax(T)) == (typemin(T) * typemax(T), true)
817811
if 2.0 <= typemax(T)
818-
@test Base.Checked.mul_with_overflow(typemax(T), 2) == (typemax(T) * 2, true)
819-
@test Base.Checked.mul_with_overflow(2, typemax(T)) == (2 * typemax(T), true)
820-
issigned(I) && @test Base.Checked.mul_with_overflow(typemin(T), 2) == (typemin(T) * 2, true)
821-
issigned(I) && @test Base.Checked.mul_with_overflow(2, typemin(T)) == (2 * typemin(T), true)
812+
@test Base.Checked.mul_with_overflow(typemax(T), T(2)) == (typemax(T) * 2, true)
813+
@test Base.Checked.mul_with_overflow(T(2), typemax(T)) == (2 * typemax(T), true)
814+
issigned(I) && @test Base.Checked.mul_with_overflow(typemin(T), T(2)) == (typemin(T) * 2, true)
815+
issigned(I) && @test Base.Checked.mul_with_overflow(T(2), typemin(T)) == (2 * typemin(T), true)
822816
end
823817

824818
if f > 0
@@ -833,36 +827,21 @@ end
833827
end
834828
end
835829

836-
@testset "with_overflow math promotions" begin
837-
x = FD{Int8,1}(1)
838-
y = FD{Int64,1}(2)
839-
@testset for op in (
840-
Base.Checked.add_with_overflow, Base.Checked.sub_with_overflow,
841-
Base.Checked.mul_with_overflow,
842-
)
843-
@test op(x, y) === op(FD{Int64,1}(1), y)
844-
@test op(y, x) === op(y, FD{Int64,1}(1))
845-
846-
@test op(x, 2) === op(x, FD{Int8,1}(2))
847-
@test op(2, x) === op(FD{Int8,1}(2), x)
848-
end
849-
end
850-
851830
@testset "non-overflowing with_overflow math" begin
852-
@test Base.Checked.add_with_overflow(1, FD{Int8,1}(1.1)) == (FD{Int8,1}(2.1), false)
853-
@test Base.Checked.add_with_overflow(FD{Int8,1}(1.1), 1) == (FD{Int8,1}(2.1), false)
831+
@test Base.Checked.add_with_overflow(FD{Int8,1}(1), FD{Int8,1}(1.1)) == (FD{Int8,1}(2.1), false)
832+
@test Base.Checked.add_with_overflow(FD{Int8,1}(1.1), FD{Int8,1}(1)) == (FD{Int8,1}(2.1), false)
854833
@test Base.Checked.add_with_overflow(FD{Int64,8}(30.123), FD{Int64,8}(30)) == (FD{Int64,8}(60.123), false)
855-
@test Base.Checked.add_with_overflow(FD{Int64,8}(30.123), FD{Int64,5}(30)) == (FD{Int64,8}(60.123), false)
834+
@test Base.Checked.add_with_overflow(FD{Int64,8}(30.123), FD{Int64,8}(-50)) == (FD{Int64,8}(-19.877), false)
856835

857-
@test Base.Checked.sub_with_overflow(3, FD{Int16,2}(2.5)) == (FD{Int16,1}(0.5), false)
858-
@test Base.Checked.sub_with_overflow(FD{Int16,2}(2.5), 3) == (FD{Int16,1}(-0.5), false)
836+
@test Base.Checked.sub_with_overflow(FD{Int16,2}(3), FD{Int16,2}(2.5)) == (FD{Int16,1}(0.5), false)
837+
@test Base.Checked.sub_with_overflow(FD{Int16,2}(2.5), FD{Int16,2}(3)) == (FD{Int16,1}(-0.5), false)
859838
@test Base.Checked.sub_with_overflow(FD{Int32,4}(10.11), FD{Int32,4}(2)) == (FD{Int32,4}(8.11), false)
860-
@test Base.Checked.sub_with_overflow(FD{Int32,4}(10.11), FD{Int32,3}(2)) == (FD{Int32,4}(8.11), false)
839+
@test Base.Checked.sub_with_overflow(FD{Int32,4}(10.11), FD{Int32,4}(-2)) == (FD{Int32,4}(12.11), false)
861840

862-
@test Base.Checked.mul_with_overflow(4, FD{Int64,6}(2.22)) == (FD{Int64,6}(8.88), false)
863-
@test Base.Checked.mul_with_overflow(FD{Int64,6}(2.22), 4) == (FD{Int64,6}(8.88), false)
841+
@test Base.Checked.mul_with_overflow(FD{Int64,6}(4), FD{Int64,6}(2.22)) == (FD{Int64,6}(8.88), false)
842+
@test Base.Checked.mul_with_overflow(FD{Int64,6}(2.22), FD{Int64,6}(4)) == (FD{Int64,6}(8.88), false)
864843
@test Base.Checked.mul_with_overflow(FD{Int128,14}(10), FD{Int128,14}(20.1)) == (FD{Int128,14}(201), false)
865-
@test Base.Checked.mul_with_overflow(FD{Int128,30}(10.11), FD{Int128,0}(1)) == (FD{Int128,30}(10.11), false)
844+
@test Base.Checked.mul_with_overflow(FD{Int128,30}(10.1), FD{Int128,30}(1)) == (FD{Int128,30}(10.1), false)
866845

867846
@test div_with_overflow(FD{Int64,6}(4), FD{Int64,6}(2)) == (FD{Int64,6}(2), false)
868847
@test div_with_overflow(FD{Int32,6}(4), FD{Int32,6}(2.1)) == (FD{Int32,6}(1), false)

0 commit comments

Comments
 (0)