-
Notifications
You must be signed in to change notification settings - Fork 23
Add rdiv_with_overflow and fld_with_overflow #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2e71f3e
2297c6f
48cc31a
c7e0826
112d5e0
68e2935
b43473b
48f1d8d
d5ef6e6
5a0dfe5
404161e
1399e4f
564d841
05381f9
3ba8bde
a59ebdf
d0cdb98
e45f98e
6976457
ecece4e
b2f0a82
933367b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -777,6 +777,8 @@ end | |
end | ||
|
||
@testset "limits: with_overflow math" begin | ||
using FixedPointDecimals: rdiv_with_overflow, fld_with_overflow | ||
|
||
# Easy to reason about cases of overflow: | ||
@test Base.Checked.add_with_overflow(FD{Int8,2}(1), FD{Int8,2}(1)) == (FD{Int8,2}(-0.56), true) | ||
@test Base.Checked.add_with_overflow(FD{Int8,2}(1), FD{Int8,2}(1)) == (FD{Int8,2}(-0.56), true) | ||
|
@@ -789,6 +791,19 @@ end | |
@test div_with_overflow(typemin(FD{Int32,0}), FD{Int32,0}(-1)) == (typemin(FD{Int32,0}), true) | ||
@test div_with_overflow(FD{Int16,1}(1639), FD{Int16,1}(0.5)) == (FD{Int16,1}(-3275.6), true) | ||
|
||
@test rdiv_with_overflow(Int8(1), FD{Int8,2}(0.7)) == (FD{Int8,2}(-1.13), true) | ||
@test rdiv_with_overflow(FD{Int16,2}(165), FD{Int16,2}(0.5)) == (FD{Int16,2}(-325.36), true) | ||
@test rdiv_with_overflow(FD{Int16,2}(-165), FD{Int16,2}(0.5)) == (FD{Int16,2}(325.36), true) | ||
@test rdiv_with_overflow(typemin(FD{Int64,8}), Int32(-1)) == (typemin(FD{Int64,8}), true) | ||
@test rdiv_with_overflow(typemin(FD{Int64,0}), FD{Int64,0}(-1)) == (typemin(FD{Int64,0}), true) | ||
@test rdiv_with_overflow(typemin(FD{Int8,2}), FD{Int8,2}(-1)) == (typemin(FD{Int8,2}), true) | ||
@test rdiv_with_overflow(typemin(FD{Int8,2}), FD{Int8,2}(-0.01)) == (FD{Int8,2}(0), true) | ||
|
||
@test fld_with_overflow(FD{Int8,2}(-1), FD{Int8,2}(0.9)) == (FD{Int8,2}(0.56), true) | ||
@test fld_with_overflow(typemin(FD{Int64,0}), FD{Int64,0}(-1)) == (typemin(FD{Int64,0}), true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. per above, please add a case of non-0
or somethign like that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added this for rdiv_with_overflow an equivalent for fld (it doesn't overflow). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops! Right, i meant I think it should be this: julia> FixedPointDecimals.fld_with_overflow(typemin(FD{Int8,2}), FD{Int8,2}(-0.01))
(FixedDecimal{Int8,2}(-1.28), true) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added the two test cases in PR review comment suggestions :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whoops :l thanks |
||
@test fld_with_overflow(FD{Int8,1}(7), FD{Int8,1}(0.5)) == (FD{Int8,1}(-11.6), true) | ||
NHDaly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@test FixedPointDecimals.fld_with_overflow(typemin(FD{Int8,2}), FD{Int8,2}(-0.01)) == (typemin(FD{Int8,2}), true) | ||
|
||
@testset "with_overflow math corner cases" begin | ||
@testset for I in (Int128, UInt128, Int8, UInt8), f in (0,2) | ||
T = FD{I, f} | ||
|
@@ -823,7 +838,21 @@ end | |
issigned(I) && @test_throws DivideError div_with_overflow(typemax(T), T(0)) | ||
issigned(I) && @test_throws DivideError div_with_overflow(typemin(T), T(0)) | ||
issigned(I) && @test div_with_overflow(typemin(T), -eps(T))[2] | ||
|
||
@test fld_with_overflow(typemax(T), eps(T))[2] | ||
issigned(I) && @test fld_with_overflow(typemin(T), eps(T))[2] | ||
issigned(I) && @test fld_with_overflow(typemax(T), -eps(T))[2] | ||
end | ||
|
||
@test_throws DivideError rdiv_with_overflow(typemax(T), T(0)) | ||
@test_throws DivideError rdiv_with_overflow(typemin(T), T(0)) | ||
@test_throws DivideError rdiv_with_overflow(eps(T), T(0)) | ||
@test_throws DivideError rdiv_with_overflow(-eps(T), T(0)) | ||
|
||
@test_throws DivideError fld_with_overflow(typemax(T), T(0)) | ||
@test_throws DivideError fld_with_overflow(typemin(T), T(0)) | ||
@test_throws DivideError fld_with_overflow(eps(T), T(0)) | ||
@test_throws DivideError fld_with_overflow(-eps(T), T(0)) | ||
end | ||
end | ||
|
||
|
@@ -848,6 +877,17 @@ end | |
@test div_with_overflow(FD{Int128,14}(10), FD{Int128,14}(20.1)) == (FD{Int128,14}(0), false) | ||
@test div_with_overflow(FD{Int128,30}(10.1), FD{Int128,30}(1)) == (FD{Int128,30}(10), false) | ||
@test div_with_overflow(typemin(FD{Int32,8}(1)), FD{Int32,8}(-1)) == (21, false) | ||
|
||
@test rdiv_with_overflow(Int8(1), FD{Int8,2}(0.8)) == (FD{Int8,2}(1.25), false) | ||
@test rdiv_with_overflow(FD{Int64,8}(5), FD{Int64,8}(2)) == (FD{Int64,8}(2.5), false) | ||
@test rdiv_with_overflow(FD{Int64,8}(5), FD{Int64,8}(0.5)) == (FD{Int64,8}(10), false) | ||
@test rdiv_with_overflow(FD{Int128,0}(20000), Int32(5000)) == (FD{Int128,0}(4), false) | ||
|
||
@test fld_with_overflow(typemax(FD{Int128,38}), FD{Int128,38}(1)) == (FD{Int128,38}(1), false) | ||
@test fld_with_overflow(FD{Int64,8}(20.5), FD{Int64,8}(2.1)) == (FD{Int64,8}(9), false) | ||
@test fld_with_overflow(FD{Int8,0}(-5), FD{Int8,0}(-1)) == (FD{Int8,0}(5), false) | ||
@test fld_with_overflow(FD{Int8,2}(0.99), FD{Int8,2}(0.5)) == (FD{Int8,2}(1), false) | ||
@test fld_with_overflow(typemin(FD{Int8,2}), FD{Int8,2}(-1)) == (FD{Int8,2}(1), false) | ||
end | ||
end | ||
NHDaly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.