Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.jl.cov
*.jl.*.cov
*.jl.mem
*.jl.*.mem
Manifest.toml
5 changes: 4 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ SpecialFunctions = "0.8, 0.9, 0.10, 0.11, 1, 2"
julia = "1.7"
Printf = "<0.0.1, 1"
Random = "<0.0.1, 1"
Test = "<0.0.1, 1"
Aqua = "0.8"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "Printf"]
test = ["Test", "Printf", "Aqua"]
9 changes: 8 additions & 1 deletion src/DecFP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ for w in (32,64,128)

@eval $BID(x::AbstractIrrational, r::RoundingMode) = $BID(string(BigFloat(x, precision=256)), r)

@eval $BID(x::Union{Int8,UInt8,Int16,UInt16}) = $BID(Int32(x))
@eval $BID(x::Union{Bool,Int8,UInt8,Int16,UInt16}) = $BID(Int32(x))
@eval $BID(x::Float16) = $BID(Float32(x))

@eval $BID(x::Rational{T}) where {T} = $BID(x.num) / $BID(x.den)
Expand Down Expand Up @@ -584,6 +584,13 @@ end
Base.Signed(x::DecimalFloatingPoint) = Int(x)
Base.Unsigned(x::DecimalFloatingPoint) = UInt(x)
Base.Integer(x::DecimalFloatingPoint) = Int(x)
Base.Bool(x::DecimalFloatingPoint) = x == 0 ? false : x == 1 ? true : throw(InexactError(:Bool, Bool, x))

# The generic ::Type{I<:Integer} methods above construct I(10), which fails for Bool, and on
# Julia < 1.11 they are also ambiguous with Base's (::Type{Bool}, ::AbstractFloat) methods.
for f in (:trunc, :floor, :ceil, :round)
@eval Base.$f(::Type{Bool}, x::DecimalFloatingPoint) = Bool($f(Int, x))
end

function (::Type{I})(x::DecimalFloatingPoint) where {I<:Integer}
x != trunc(x) && throw(InexactError(:convert, I, x))
Expand Down
13 changes: 13 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
using DecFP, Test, Printf, Random, Base.MathConstants, SpecialFunctions
using Aqua

@testset "Aqua" begin
Aqua.test_all(DecFP)
end

include("printf.jl")

Expand Down Expand Up @@ -361,6 +366,14 @@ for T in (Dec32, Dec64, Dec128)
@test round(T(2.5), RoundNearestTiesAway) === round(T(3.3), RoundNearestTiesAway) === T(3)
@test round(T(2.5), RoundNearestTiesUp) === round(T(3.3), RoundNearestTiesUp) === T(3)

@test T(true) === T(1) && T(false) === T(0)
@test Bool(T(1)) === true && Bool(T(0)) === false && Bool(-T(0)) === false
@test_throws InexactError Bool(T(2))
@test_throws InexactError Bool(T(NaN))
@test trunc(Bool, T(1.5)) === floor(Bool, T(1.5)) === round(Bool, T(1.2)) === ceil(Bool, T(0.5)) === true
@test trunc(Bool, T(-0.5)) === floor(Bool, T(0.5)) === round(Bool, T(0.2)) === ceil(Bool, T(-0.5)) === false
@test_throws InexactError trunc(Bool, T(2))
@test_throws InexactError floor(Bool, T(-1))
for Ti in (Integer,Int8,UInt8,Int16,UInt16,Int32,UInt32,Int64,UInt64,Int128,UInt128)
if Ti != Integer
@test parse(T, "17") == T(Ti(17)) == Ti(17) == Ti(T(17))
Expand Down
Loading