Skip to content

Commit

Permalink
Merge pull request JuliaMath#118 from jmkuhn/directed
Browse files Browse the repository at this point in the history
Constructors with RoundingMode
  • Loading branch information
jmkuhn committed May 11, 2020
2 parents 29adf8d + 0c90288 commit 5c102bc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/DecFP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ for w in (32,64,128)
$BID(x::Number) = convert($BID, x)
Base.reinterpret(::Type{$BID}, x::$Ti) = new(x)
end

@eval function $BID(x::Real, mode::RoundingMode)
setrounding($BID, mode) do
convert($BID, x)
end
end

# fix method ambiguities:
@eval $BID(x::Rational{T}) where {T} = convert($BID, x)
end
Expand Down Expand Up @@ -209,6 +216,12 @@ for w in (32,64,128)

$BID(x::AbstractString) = parse($BID, x)

function $BID(x::AbstractString, mode::RoundingMode)
setrounding($BID, mode) do
parse($BID, x)
end
end

function tostring(x::$BID)
# fills global _buffer
ccall(($(bidsym(w,"to_string")), libbid), Cvoid, (Ptr{UInt8},$BID,Ref{Cuint}), _buffer[Threads.threadid()], x, RefArray(flags, Threads.threadid()))
Expand Down
17 changes: 17 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ for T in (Dec32, Dec64, Dec128)
@test parse(T, "0.1")::T == T(1//10)
@test T("0.1")::T == T(1//10)

if T != Dec128
@test T(0.1) == T("0.1")
@test T(0.1, RoundNearest) == T("0.1")
@test T(0.1, RoundDown) == T("0.1")
@test T(0.1, RoundUp) == nextfloat(T("0.1"))
end
@test T("1", RoundDown) == T(1)
@test T("1", RoundUp) == T(1)
@test T("1.0000000000000000000000000000000000000001") == T(1)
@test T("1.0000000000000000000000000000000000000001", RoundNearest) == T(1)
@test T("1.0000000000000000000000000000000000000001", RoundDown) == T(1)
@test T("1.0000000000000000000000000000000000000001", RoundUp) == nextfloat(T(1))
@test T("0.9999999999999999999999999999999999999999") == T(1)
@test T("0.9999999999999999999999999999999999999999", RoundNearest) == T(1)
@test T("0.9999999999999999999999999999999999999999", RoundUp) == T(1)
@test T("0.9999999999999999999999999999999999999999", RoundDown) == prevfloat(T(1))

io = IOBuffer()
show(io, T("NaN")); @test String(take!(io)) == "NaN"
show(io, T("Inf")); @test String(take!(io)) == "Inf"
Expand Down

0 comments on commit 5c102bc

Please sign in to comment.