Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@sprintf throws a bound error - but only on some inputs. #22137

Closed
sbromberger opened this issue May 30, 2017 · 6 comments
Closed

@sprintf throws a bound error - but only on some inputs. #22137

sbromberger opened this issue May 30, 2017 · 6 comments
Labels
bug Indicates an unexpected problem or unintended behavior display and printing Aesthetics and correctness of printed representations of objects. strings "Strings!"

Comments

@sbromberger
Copy link
Contributor

Perhaps related to #22126 ?

julia> @sprintf("%.330f", 2)
"2.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"

julia> @sprintf("%.330f", 2.1)
ERROR: BoundsError: attempt to access 326-element Array{UInt8,1} at index [327]
Stacktrace:
 [1] bignumdtoa(::Float64, ::Int64, ::Int64, ::Array{UInt8,1}, ::Array{Base.Grisu.Bignums.Bignum,1}) at ./grisu/bignum.jl:68
 [2] grisu(::Float64, ::Int64, ::Int64, ::Array{UInt8,1}, ::Array{Base.Grisu.Bignums.Bignum,1}) at ./grisu/grisu.jl:51
 [3] fix_dec(::Float64, ::Int64) at ./printf.jl:953
 [4] fix_dec(::Base.AbstractIOBuffer{Array{UInt8,1}}, ::Float64, ::String, ::Int64, ::Int64, ::Char) at ./printf.jl:801

julia> versioninfo()
Julia Version 0.7.0-DEV.373
Commit 82e9638225 (2017-05-28 01:51 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin16.5.0)
  CPU: Intel(R) Core(TM) i5-6267U CPU @ 2.90GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, skylake)
@kshyatt kshyatt added the strings "Strings!" label May 30, 2017
@JeffBezanson JeffBezanson added bug Indicates an unexpected problem or unintended behavior display and printing Aesthetics and correctness of printed representations of objects. labels May 31, 2017
@omus
Copy link
Member

omus commented Jun 1, 2017

Issue also exists on 0.5 and 0.6.

julia> @sprintf("%.325f", nextfloat(0.0))
"0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000049"

julia> @sprintf("%.325f", 1.0)
ERROR: BoundsError: attempt to access 326-element Array{UInt8,1} at index [327]
Stacktrace:
 [1] bignumdtoa(::Float64, ::Int64, ::Int64, ::Array{UInt8,1}, ::Array{Base.Grisu.Bignums.Bignum,1}) at ./grisu/bignum.jl:68
 [2] grisu(::Float64, ::Int64, ::Int64, ::Array{UInt8,1}, ::Array{Base.Grisu.Bignums.Bignum,1}) at ./grisu/grisu.jl:51
 [3] fix_dec(::Float64, ::Int64) at ./printf.jl:953
 [4] fix_dec(::Base.AbstractIOBuffer{Array{UInt8,1}}, ::Float64, ::String, ::Int64, ::Int64, ::Char) at ./printf.jl:801

The exception can occur with printing less decimal places when the input is large:

julia> v = prevfloat(typemax(Float64))
1.7976931348623157e308

julia> @sprintf("%.16f", v)
"179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0000000000000000"

julia> @sprintf("%.17f", v)
ERROR: BoundsError: attempt to access 326-element Array{UInt8,1} at index [327]
Stacktrace:
 [1] bignumdtoa(::Float64, ::Int64, ::Int64, ::Array{UInt8,1}, ::Array{Base.Grisu.Bignums.Bignum,1}) at ./grisu/bignum.jl:68
 [2] grisu(::Float64, ::Int64, ::Int64, ::Array{UInt8,1}, ::Array{Base.Grisu.Bignums.Bignum,1}) at ./grisu/grisu.jl:51
 [3] fix_dec(::Float64, ::Int64) at ./printf.jl:953
 [4] fix_dec(::Base.AbstractIOBuffer{Array{UInt8,1}}, ::Float64, ::String, ::Int64, ::Int64, ::Char) at ./printf.jl:801

@vtjnash
Copy link
Member

vtjnash commented Jun 1, 2017

Off by one error? looks like

if n > length(DIGITS)-1; n = length(DIGITS)-1; end
is supposed to prevent this, but also seems wrong that grisu is trying to append a nul-byte

@sbromberger
Copy link
Contributor Author

This is still an issue in 1.1.0

@StefanKarpinski
Copy link
Member

@quinnj, I think you're the most recent person to have dived into the Printf code, would it be possible for you to take a look at this?

@quinnj
Copy link
Member

quinnj commented Aug 7, 2019

So, I somehow totally missed that I was pinged here; sorry about that. But, as fate would have it, I'm going over old numeric display issues in light of #32799 and it looks like this would be solved by that PR. Now, we still need to work out the exact details of @printf + new ryu algorithm details, but essentially calling Ryu.writefixed or Ryu.writeexp with all these examples works in that PR just fine.

@quinnj
Copy link
Member

quinnj commented Sep 8, 2020

Fixed in #32859

@quinnj quinnj closed this as completed Sep 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior display and printing Aesthetics and correctness of printed representations of objects. strings "Strings!"
Projects
None yet
Development

No branches or pull requests

7 participants