@Printf with DecFP / Dec128

I’m seeing two different outputs for Dec128 numbers println() vs @printf:
(Julia v1.10.0 with DecFP v1.3.2)

using Printf, Quadmath, DecFP
C = log2(big(ℯ)) # \euler <tab> to get symbol
d = convert(Dec128, C)
println("C = ", C)
println("d = ", d)
@printf("d = %.36f\n", d)
@printf("d = %.36f\n", convert(BigFloat,d))

Gives output

C = 1.442695040888963407359924681001892137426645954152985934135449406931109219181187
d = 1.442695040888963407359924681001892
d = 1.442695040888963387004650940070860088
d = 1.442695040888963407359924681001892000

Should I be using a different modifier not %f for DecFP formats ?

Dont use Printf
Write your own output string function using

"""
    sigexp(x::DecFP.DecimalFloatingPoint)

Return `(sign, significand, exponent)` such that `x` is equal to `sign * significand * 10^exponent`.
Throws `DomainError` for infinite or `NaN` arguments.

# Examples
```jldoctest
julia> sigexp(Dec64(1.25))
(1, 125, -2)

“”"

Printf is supposed to work with DecFP — @jmkuhn implemented some methods specifically to get this working a few years back: Fix printf formatting by jmkuhn · Pull Request #48 · JuliaMath/DecFP.jl · GitHub

Not sure if it has bitrotted recently or … ? Maybe file an issue. I’ve filed an issue: printf is converting to `Float64` · Issue #178 · JuliaMath/DecFP.jl · GitHub

It looks like there’s an implicit conversion to Float64 going on with @printf that didn’t used to be there:

julia> using DecFP, Printf

julia> x = rand(Dec128)
0.6498353664629435612857337361220566

julia> println(x)
0.6498353664629435612857337361220566

julia> @printf("%.36f", x)
0.649835366462943597731793943239608780
julia> 

julia> @printf("%.36f\n", Float64(x)) # matches @printf("%.36f", x) !!
0.649835366462943597731793943239608780
1 Like