Skip to content
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

Don't convert to Float64 in Printf #180

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 src/DecFP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,8 @@ promote_rule(::Type{Irrational{s}}, T::Type{Complex{F}}) where {s,F<:DecimalFloa
Base.widen(::Type{Dec32}) = Dec64
Base.widen(::Type{Dec64}) = Dec128

Printf.tofloat(x::DecimalFloatingPoint) = BigFloat(x)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to pass a precision? Otherwise the BigFloat constructor may use too low a precision.

Why do we need a conversion at all here? Wouldn't it be possible to have Printf.tofloat(x::DecimalFloatingPoint) = x and support printing natively on decimal floating point values? It seems like a shame to do a lossy conversion to binary floating point for printing — kinda defeats the point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also prefer to not go through BigFloat. In the original Printf PR, there is another interface mentioned where we could pass a buffer of digit chars and the location of the decimal point and have that formatted.
I would like to try that interface but I can't find it in the current code. JuliaLang/julia#32859 (comment) Is that interface implemented @quinnj ?


macro d_str(s, flags...) parse(Dec64, s) end
macro d32_str(s, flags...) parse(Dec32, s) end
macro d64_str(s, flags...) parse(Dec64, s) end
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ for T in (Dec32, Dec64, Dec128)

@test @sprintf("%.2f %.4f", T("12.34567"), 9.87) == "12.35 9.8700"

# issue #178
@test @sprintf("%.36f", T("0.1")) == "0.100000000000000000000000000000000000"

x,y,z = 1.5, -3.25, 0.0625 # exactly represented in binary
xd = T(x); yd = T(y); zd = T(z)

Expand Down
Loading