diff --git a/Manifest.toml b/Manifest.toml index 49e8d20..2614faa 100644 --- a/Manifest.toml +++ b/Manifest.toml @@ -6,3 +6,10 @@ uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" [[LinearAlgebra]] deps = ["Libdl"] uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" + +[[Printf]] +deps = ["Unicode"] +uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" + +[[Unicode]] +uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" diff --git a/Project.toml b/Project.toml index 9e8ac0c..b590ee3 100644 --- a/Project.toml +++ b/Project.toml @@ -1,10 +1,11 @@ name = "MultiFloats" uuid = "bdf0d083-296b-4888-a5b6-7498122e68a5" authors = ["David K. Zhang "] -version = "0.1.0" +version = "0.2.0" [deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" [compat] julia = "1.0" diff --git a/src/MultiFloats.jl b/src/MultiFloats.jl index 3e635e3..01dd1b4 100644 --- a/src/MultiFloats.jl +++ b/src/MultiFloats.jl @@ -185,24 +185,53 @@ Base.promote_rule(::Type{Float64x{N}}, ::Type{Float32}) where {N} = Float64x{N} end end -function Base.show(io::IO, x::MultiFloat{T,N}) where {T<:AF,N} +function call_normalized(callback, x::MultiFloat{T,N}) where {T<:AF,N} x = renormalize(x) if !isfinite(x.x[1]) - show(io, x.x[1]) + callback(x.x[1]) else i = N while (i > 0) && iszero(x.x[i]) i -= 1 end if iszero(i) - show(io, zero(T)) + callback(zero(T)) else - show(io, setprecision(() -> BigFloat(x), - precision(T) + exponent(x.x[1]) - exponent(x.x[i]))) + setprecision(() -> callback(BigFloat(x)), + precision(T) + exponent(x.x[1]) - exponent(x.x[i])) end end end +function Base.show(io::IO, x::MultiFloat{T,N}) where {T<:AF,N} + call_normalized(y -> show(io, y), x) +end + +################################################################################ + +# Thanks to Greg Plowman (https://github.com/GregPlowman) for suggesting +# implementations of Printf.fix_dec and Printf.ini_dec for @printf support. + +import Printf: fix_dec, ini_dec + +if VERSION < v"1.1" + + fix_dec(out, x::MultiFloat{T,N}, flags::String, width::Int, precision::Int, c::Char) where {T<:AF,N} = + call_normalized(d -> fix_dec(out, BigFloat(d), flags, width, precision, c), x) + + ini_dec(out, x::MultiFloat{T,N}, ndigits::Int, flags::String, width::Int, precision::Int, c::Char) where {T<:AF,N} = + call_normalized(d -> ini_dec(out, BigFloat(d), ndigits, flags, width, precision, c), x) + +else + + fix_dec(out, x::MultiFloat{T,N}, flags::String, width::Int, precision::Int, c::Char, digits) where {T<:AF,N} = + call_normalized(d -> fix_dec(out, BigFloat(d), flags, width, precision, c, digits), x) + + ini_dec(out, x::MultiFloat{T,N}, ndigits::Int, flags::String, width::Int, precision::Int, c::Char, digits) where {T<:AF,N} = + call_normalized(d -> ini_dec(out, BigFloat(d), ndigits, flags, width, precision, c, digits), x) + +end + ################################################################################ @inline Base.:(==)(x::MF{T,1}, y::MF{T,1}) where {T<:AF} = (x.x[1] == y.x[1])