JuliaIO / Formatting.jl

Deprecated. See README. (A Julia package to provide Python-like formatting support)
Other
130 stars 23 forks source link

Formatting large float throws InexactError #102

Open oxdc opened 3 years ago

oxdc commented 3 years ago

https://github.com/JuliaIO/Formatting.jl/blob/e4c9f60020e965a72abae049ddac7e02bf986067/src/fmtcore.jl#L176

julia> using Formatting

julia> fmt(FormatSpec("15.6f"), 1e5)  # OK
"  100000.000000"

julia> fmt(FormatSpec("15.6f"), 1e10)  # OK, even the width is too small
"10000000000.000000"

julia> fmt(FormatSpec("15.6f"), 1e20)  # Error, the float is too large to be represented by Int64
ERROR: InexactError: trunc(Int64, 1.0e20)
Stacktrace:
 [1] trunc
   @ .\float.jl:716 [inlined]
 [2] trunc
   @ .\float.jl:291 [inlined]
 [3] _pfmt_f(out::IOBuffer, fs::FormatSpec, x::Float64)
   @ Formatting ~\.julia\packages\Formatting\BwWBf\src\fmtcore.jl:176
 [4] printfmt(io::IOBuffer, fs::FormatSpec, x::Float64)
   @ Formatting ~\.julia\packages\Formatting\BwWBf\src\fmtspec.jl:190
 [5] sprint(::Function, ::FormatSpec, ::Vararg{Any, N} where N; context::Nothing, sizehint::Int64)
   @ Base .\strings\io.jl:105
 [6] sprint
   @ .\strings\io.jl:101 [inlined]
 [7] fmt(fs::FormatSpec, x::Float64)
   @ Formatting ~\.julia\packages\Formatting\BwWBf\src\fmtspec.jl:205
 [8] top-level scope
   @ REPL[16]:1

This could be fixed by changing Integer to BigInt.

ScottPJones commented 9 months ago

Note: this has been fixed in Format.jl.