Skip to content

Commit

Permalink
Add remove_trailing_zeros option to string methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel-Dahne committed Mar 17, 2024
1 parent 9bbf843 commit 35d0e15
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 35 deletions.
67 changes: 61 additions & 6 deletions src/show.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
digits_prec(prec::Integer) = floor(Int, prec * (log(2) / log(10)))

function _remove_trailing_zeros(str::AbstractString)
if occursin('.', str)
if occursin('e', str)
# Numbers on the form xxx.yyy0ezzz
mantissa, exponent = split(str, 'e', limit = 2)
mantissa = rstrip(mantissa, '0')
if endswith(mantissa, '.')
mantissa *= '0'
end
str = mantissa * 'e' * exponent
else
# Numbers on the form xxx.yyy0
str = rstrip(str, '0')
if endswith(str, '.')
str *= '0'
end
end
end

return str
end

function _string(x::MagOrRef)
Libc.flush_cstdio()
Base.flush(stdout)
Expand All @@ -16,20 +38,28 @@ function _string(x::MagOrRef)
return read(out_rd, String)
end

function Base.string(x::MagOrRef; digits::Integer = digits_prec(30))
function Base.string(
x::MagOrRef;
digits::Integer = digits_prec(30),
remove_trailing_zeros::Bool = true,
)
cstr = ccall(@libflint(arf_get_str), Ptr{UInt8}, (Ref{arf_struct}, Int), Arf(x), digits)
str = unsafe_string(cstr)
ccall(@libflint(flint_free), Nothing, (Ptr{UInt8},), cstr)

return str
return remove_trailing_zeros ? _remove_trailing_zeros(str) : str
end

function Base.string(x::ArfOrRef; digits::Integer = digits_prec(precision(x)))
function Base.string(
x::ArfOrRef;
digits::Integer = digits_prec(precision(x)),
remove_trailing_zeros::Bool = true,
)
cstr = ccall(@libflint(arf_get_str), Ptr{UInt8}, (Ref{arf_struct}, Int), x, digits)
str = unsafe_string(cstr)
ccall(@libflint(flint_free), Nothing, (Ptr{UInt8},), cstr)

return str
return remove_trailing_zeros ? _remove_trailing_zeros(str) : str
end

function Base.string(
Expand All @@ -39,6 +69,7 @@ function Base.string(
no_radius::Bool = false,
condense::Integer = 0,
unicode::Bool = false,
remove_trailing_zeros::Bool = !no_radius,
)
flag = convert(UInt, more + 2no_radius + 16condense)

Expand All @@ -57,6 +88,10 @@ function Base.string(
str = replace(str, "+/-" => "±", "..." => "")
end

if remove_trailing_zeros && !startswith(str, '[')
str = _remove_trailing_zeros(str)
end

return str
end

Expand All @@ -67,11 +102,31 @@ function Base.string(
no_radius::Bool = false,
condense::Integer = 0,
unicode::Bool = false,
remove_trailing_zeros::Bool = true,
)
str = string(realref(z); digits, more, no_radius, condense, unicode)
str = string(
realref(z);
digits,
more,
no_radius,
condense,
unicode,
remove_trailing_zeros,
)

if !iszero(imagref(z))
str *= " + " * string(imagref(z); digits, more, no_radius, condense, unicode) * "im"
str *=
" + " *
string(
imagref(z);
digits,
more,
no_radius,
condense,
unicode,
remove_trailing_zeros,
) *
"im"
end

return str
Expand Down
68 changes: 39 additions & 29 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,35 @@
@test Arblib._string(Mag(1)) == "(536870912 * 2^-29)"

@test string(Mag()) == "0"
@test string(Mag(1)) == "1.00000000"
@test string(Mag(1), digits = 2) == "1.0"
@test string(Mag(1), digits = 12) == "1.00000000000"
@test string(Mag(1)) == "1.0"
@test string(Mag(1), digits = 3, remove_trailing_zeros = false) == "1.00"
@test string(Mag(1), digits = 12, remove_trailing_zeros = false) == "1.00000000000"
@test string(Mag(10^15)) == "1.0e+15"
@test string(Mag(10^15), remove_trailing_zeros = false) == "1.00000000e+15"
@test string(Mag(π) * 10^10) == "3.14159267e+10"

@test string(Arf()) == "0"
@test string(Arf(1)) ==
"1.0000000000000000000000000000000000000000000000000000000000000000000000000000"
@test string(Arf(1, prec = 64)) == "1.000000000000000000"
@test string(Arf(1), digits = 2) == "1.0"
@test string(Arf(1), digits = 12) == "1.00000000000"
@test string(Arf(1)) == "1.0"
@test string(Arf(1), digits = 3, remove_trailing_zeros = false) == "1.00"
@test string(Arf(1), digits = 12, remove_trailing_zeros = false) == "1.00000000000"
@test string(Arf(1) / 3) ==
"0.33333333333333333333333333333333333333333333333333333333333333333333333333333"
@test string(Arf(1) / 3, digits = 12) == "0.333333333333"
@test string(Arf(10)^100) == "1.0e+100"
@test string(Arf(10)^100, remove_trailing_zeros = false) ==
"1.0000000000000000000000000000000000000000000000000000000000000000000000000000e+100"
@test string(Arf(1 // 3) * Arf(10)^81) ==
"3.3333333333333333333333333333333333333333333333333333333333333333333333333334e+80"

@test string(Arb()) == "0"
@test string(Arb(1)) ==
"1.0000000000000000000000000000000000000000000000000000000000000000000000000000"
@test string(Arb(1, prec = 64)) == "1.000000000000000000"
@test string(Arb(1), digits = 2) == "1.0"
@test string(Arb(1), digits = 12) == "1.00000000000"
@test string(Arb(1)) == "1.0"
@test string(Arb(1, prec = 64), remove_trailing_zeros = false) ==
"1.000000000000000000"
@test string(Arb(1), digits = 2, remove_trailing_zeros = false) == "1.0"
@test string(Arb(1), digits = 12, remove_trailing_zeros = false) == "1.00000000000"
@test string(Arb(10)^100) == "1.0e+100"
@test string(Arb(10)^100, remove_trailing_zeros = false) ==
"1.0000000000000000000000000000000000000000000000000000000000000000000000000000e+100"
@test string(Arb(π)) ==
"[3.1415926535897932384626433832795028841971693993751058209749445923078164062862 +/- 1.93e-77]"
@test string(Arb(π, prec = 64)) == "[3.141592653589793239 +/- 5.96e-19]"
Expand All @@ -39,11 +51,10 @@
"[3.14159{…66 digits…}62862 ± 1.93e-77]"

@test string(Acb()) == "0"
@test string(Acb(1)) ==
"1.0000000000000000000000000000000000000000000000000000000000000000000000000000"
@test string(Acb(0, 1)) ==
"0 + 1.0000000000000000000000000000000000000000000000000000000000000000000000000000im"
@test string(Acb(1, 1)) ==
@test string(Acb(1)) == "1.0"
@test string(Acb(0, 1)) == "0 + 1.0im"
@test string(Acb(1, 1)) == "1.0 + 1.0im"
@test string(Acb(1, 1), remove_trailing_zeros = false) ==
"1.0000000000000000000000000000000000000000000000000000000000000000000000000000 + 1.0000000000000000000000000000000000000000000000000000000000000000000000000000im"
@test string(Acb(π, ℯ)) ==
"[3.1415926535897932384626433832795028841971693993751058209749445923078164062862 +/- 1.93e-77] + [2.7182818284590452353602874713526624977572470936999595749669676277240766303535 +/- 5.46e-77]im"
Expand Down Expand Up @@ -77,25 +88,24 @@
end

@testset "show" begin
@test repr(Mag(1), context = IOContext(stdout, :compact => true)) == "1.00000"
@test repr(Arf(1), context = IOContext(stdout, :compact => true)) == "1.00000"
@test repr(Mag(1), context = IOContext(stdout, :compact => true)) == "1.0"
@test repr(Arf(1), context = IOContext(stdout, :compact => true)) == "1.0"
@test repr(Arb(π), context = IOContext(stdout, :compact => true)) ==
"[3.14{…72 digits…}62 ± 1.93e-77]"
@test repr(Acb(π, ℯ), context = IOContext(stdout, :compact => true)) ==
"[3.14{…72 digits…}62 ± 1.93e-77] + [2.71{…72 digits…}35 ± 5.46e-77]im"

prec = 32

P = ArbPoly(Arb[1, 2, 0, π], prec = prec)
@test "$P" == "1.00000000 + 2.00000000⋅x + [3.14159265 +/- 3.59e-9]⋅x^3"
P = AcbPoly([Acb[1, 2, 0, π]; Acb(1, 1)], prec = prec)
P = ArbPoly(Arb[1, 2, 0, π]; prec)
@test "$P" == "1.0 + 2.0⋅x + [3.14159265 +/- 3.59e-9]⋅x^3"
P = AcbPoly([Acb[1, 2, 0, π]; Acb(1, 1)]; prec)
@test "$P" == "1.0 + 2.0⋅x + [3.14159265 +/- 3.59e-9]⋅x^3 + (1.0 + 1.0im)⋅x^4"
P = ArbSeries(Arb[1, 2, 0, π], degree = 4; prec)
@test "$P" == "1.0 + 2.0⋅x + [3.14159265 +/- 3.59e-9]⋅x^3 + 𝒪(x^5)"
P = AcbSeries([Acb[1, 2, 0, π]; Acb(1, 1)], degree = 5; prec)
@test "$P" ==
"1.00000000 + 2.00000000⋅x + [3.14159265 +/- 3.59e-9]⋅x^3 + (1.00000000 + 1.00000000im)⋅x^4"
P = ArbSeries(Arb[1, 2, 0, π], degree = 4, prec = prec)
@test "$P" == "1.00000000 + 2.00000000⋅x + [3.14159265 +/- 3.59e-9]⋅x^3 + 𝒪(x^5)"
P = AcbSeries([Acb[1, 2, 0, π]; Acb(1, 1)], degree = 5, prec = prec)
@test "$P" ==
"1.00000000 + 2.00000000⋅x + [3.14159265 +/- 3.59e-9]⋅x^3 + (1.00000000 + 1.00000000im)⋅x^4 + 𝒪(x^6)"
"1.0 + 2.0⋅x + [3.14159265 +/- 3.59e-9]⋅x^3 + (1.0 + 1.0im)⋅x^4 + 𝒪(x^6)"

@test "$(ArbPoly())" == "$(AcbPoly())" == "0"
@test "$(ArbSeries())" == "$(AcbSeries())" == "𝒪(x)"
Expand Down

0 comments on commit 35d0e15

Please sign in to comment.