Skip to content

Commit

Permalink
Add Printf support as suggested by Greg Plowman
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhang314 committed Oct 29, 2019
1 parent 5c378b6 commit d7e5bfe
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name = "MultiFloats"
uuid = "bdf0d083-296b-4888-a5b6-7498122e68a5"
authors = ["David K. Zhang <dzhang314@gmail.com>"]
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"
39 changes: 34 additions & 5 deletions src/MultiFloats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

2 comments on commit d7e5bfe

@dzhang314
Copy link
Owner Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/4834

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" d7e5bfeffa7ace13075db7707e3487443800aa93
git push origin v0.2.0

Please sign in to comment.