Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Irrationals do not format as expected #31

Open
anowacki opened this issue Mar 14, 2017 · 0 comments
Open

Irrationals do not format as expected #31

anowacki opened this issue Mar 14, 2017 · 0 comments

Comments

@anowacki
Copy link

String formatting with C (sprintf1()) and Python (fmt()) styles

Using string output of any kind, with any formatter, only outputs the result of calling show() on irrational numbers:

julia> # Expecting: "3.1415" with some other numbers at the end

julia> format("{:s}", π)
"π = 3.1415926535897..."

One would not expect there to be the definition (π = ) at the front, nor the ellipsis () at the end

Using format() with keyword arguments

The 'native' formatter in Formatting.jl (using keyword arguments with format() doesn't produce the expected values for Irrationals even when using other format, and errors when those irrationals have Unicode symbols as definitions.

Firstly, when using stripzeros = false, then no other formatting is done and the standard display() (or is it show()?) method seems to be called:

julia> # Expecting: "+3.1416"

julia> format(π, stripzeros=false, precision=4, signed=true)
"π = 3.1415926535897..."

Secondly, when implicitly (via, e.g., precision) or explicitly setting stripzeros = true, then we get an error if the standard show method would return a string containing Unicode:

julia> # Expecting "+3.1416"

julia> format(pi, precision=4, signed=true, stripzeros=true)
ERROR: UnicodeError: invalid character index
 in slow_utf8_next(::Array{UInt8,1}, ::UInt8, ::Int64) at ./strings/string.jl:67
 in next at ./strings/string.jl:96 [inlined]
 in getindex(::String, ::Int64) at ./strings/basic.jl:70
 in findnext(::String, ::Char, ::Int64) at ./array.jl:839
 in findfirst(::String, ::Char) at ./array.jl:864
 in #format#2(::Int64, ::Int64, ::Bool, ::Bool, ::Bool, ::Bool, ::Bool, ::Bool, ::Bool, ::Bool, ::Bool, ::String, ::String, ::Int64, ::Int64, ::String, ::Symbol, ::String, ::Formatting.#format, ::Irrational{:π}) at /Users/nowacki/.julia/v0.5/Formatting/src/cformat.jl:296
 in (::Formatting.#kw##format)(::Array{Any,1}, ::Formatting.#format, ::Irrational{:π}) at ./<missing>:0

Note this doesn't error when using an 'ASCII irrational:

julia> # Expecting "+0.9160"

julia> format(catalan, precision=4, signed=true, stripzeros=true)
"catalan = 0.9159655941772..."

Possible fix

I guess one option would be to convert to Float64 or BigFloat as appropriate, depending on the desired precision.

(See the source for all irrationals defined in Base.)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant