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

printf MethodError with "append_nine_digits" #52749

Closed
stevengj opened this issue Jan 4, 2024 · 3 comments · Fixed by #52781
Closed

printf MethodError with "append_nine_digits" #52749

stevengj opened this issue Jan 4, 2024 · 3 comments · Fixed by #52781
Labels
bug Indicates an unexpected problem or unintended behavior display and printing Aesthetics and correctness of printed representations of objects.
Milestone

Comments

@stevengj
Copy link
Member

stevengj commented Jan 4, 2024

This seems like a bug:

julia> using Printf

julia> @printf("%.60g", 1.38e-23)
1.3800000000000000600105824657340787992976609667826426243954e-23
julia> @printf("%.160g", 1.38e-23)
ERROR: MethodError: no method matching append_nine_digits(::Int64, ::Vector{UInt8}, ::Int64)

Closest candidates are:
  append_nine_digits(::Unsigned, ::Any, ::Int64)
   @ Base intfuncs.jl:815

Stacktrace:
 [1] writeexp(buf::Vector{…}, pos::Int64, v::Float64, precision::Int64, plus::Bool, space::Bool, hash::Bool, expchar::UInt8, decchar::UInt8, trimtrailingzeros::Bool)
   @ Base.Ryu ./ryu/exp.jl:113
 [2] writeexp(buf::Vector{…}, pos::Int64, v::Float64, precision::Int64, plus::Bool, space::Bool, hash::Bool, expchar::UInt8, decchar::UInt8, trimtrailingzeros::Bool)
   @ Base.Ryu ./ryu/exp.jl:4 [inlined]
 [3] fmt
   @ ~/Documents/Code/julia/usr/share/julia/stdlib/v1.11/Printf/src/Printf.jl:531 [inlined]
 [4] fmt
   @ ~/Documents/Code/julia/usr/share/julia/stdlib/v1.11/Printf/src/Printf.jl:313 [inlined]
 [5] format
   @ ~/Documents/Code/julia/usr/share/julia/stdlib/v1.11/Printf/src/Printf.jl:834 [inlined]
 [6] format(io::Base.TTY, f::Printf.Format{Base.CodeUnits{UInt8, String}, Tuple{Printf.Spec{Val{'g'}}}}, args::Float64)
   @ Printf ~/Documents/Code/julia/usr/share/julia/stdlib/v1.11/Printf/src/Printf.jl:935

It looks like the Unsigned type qualifier was added by @vtjnash to append_nine_digits in #51273, so it's a pretty recent problem.

@stevengj stevengj added bug Indicates an unexpected problem or unintended behavior display and printing Aesthetics and correctness of printed representations of objects. labels Jan 4, 2024
@stevengj
Copy link
Member Author

stevengj commented Jan 4, 2024

Looks like the culprit is the digits = 0 initializations in Ryu.writeexp, which make the digits variable type-unstable (it should always be unsigned).

In fact, @code_warntype indicates a large number of type instabilities in this function that seem undesirable (cc @quinnj):

julia> @code_warntype Base.Ryu.writeexp(UInt8[], 1, 1.0, -1, false, false, false, UInt8('e'), UInt8('.'), false)
MethodInstance for Base.Ryu.writeexp(::Vector{UInt8}, ::Int64, ::Float64, ::Int64, ::Bool, ::Bool, ::Bool, ::UInt8, ::UInt8, ::Bool)
  from writeexp(buf, pos, v::T, precision, plus, space, hash, expchar, decchar, trimtrailingzeros) where T<:Union{Float16, Float32, Float64} @ Base.Ryu ryu/exp.jl:1
Static Parameters
  T = Float64
Arguments
  #self#::Core.Const(Base.Ryu.writeexp)
  buf::Vector{UInt8}
  pos@_3::Int64
  v::Float64
  precision@_5::Int64
  plus::Bool
  space::Bool
  hash::Bool
  expchar::UInt8
  decchar::UInt8
  trimtrailingzeros::Bool
Locals
  val@_12::UInt8
  val@_13::UInt8
  val@_14::UInt16
  val@_15::Int64
  val@_16::UInt8
  val@_17::UInt8
  val@_18::UInt16
  val@_19::UInt8
  val@_20::UInt8
  val@_21::Bool
  val@_22::Bool
  val@_23::UInt8
  val@_24::Union{Int64, UInt32, UInt64}
  @_25::Union{Nothing, Tuple{Int64, Int64}}
  @_26::Union{Nothing, Tuple{Int64, Int64}}
  val@_27::UInt8
  val@_28::UInt8
  val@_29::UInt8
  val@_30::UInt8
  val@_31::UInt8
  val@_32::UInt8
  val@_33::UInt8
  val@_34::UInt8
  val@_35::UInt8
  val@_36::UInt8
  val@_37::UInt8
  @_38::Union{Nothing, Tuple{Int64, Int64}}
  val@_39::UInt8
  val@_40::UInt8
  d100::UInt16
  c::Union{Int64, UInt8}
  e::Int64
  roundPos::Int64
  roundUp::Int64
  trailingZeros::Bool
  requiredFives::Int64
  requiredTwos::Int64
  rexp::Int64
  lastDigit::Union{Int64, UInt64}
  digits::Union{Int64, UInt32, UInt64}
  maximum::Int64
  i::Union{Int64, UInt8}
  idx::Int64
  len::Int64
  p10bits::Int64
  availableDigits::Int64
  printedDigits::Int64
  nonzero::Bool
  m2::UInt64
  e2::Int64
  exp::Int64
  mant::UInt64
  bits::UInt64
  x::Float64
  startpos::Int64
  val@_67::UInt8
  val@_68::UInt8
  val@_69::UInt32
  @_70::Int64
  mulc@_71::UInt64
  mulb@_72::UInt64
  mula@_73::UInt64
  j@_74::Int64
  val@_75::UInt8
  val@_76::Union{Int64, UInt32}
  @_77::Int64
  p::Union{Int64, UInt16}
  mulc@_79::UInt64
  mulb@_80::UInt64
  mula@_81::UInt64
  j@_82::Int64
  k::Int64
  val@_84::UInt8
  val@_85::Int64
  val@_86::UInt8
  val@_87::UInt8
  val@_88::UInt8
  val@_89::UInt8
  val@_90::UInt8
  val@_91::UInt8
  pos@_92::Int64
  precision@_93::Int64
  @_94::Bool
  @_95::Int64
  @_96::Int64
  @_97::Bool
  @_98::Bool
  @_99::Int64
  @_100::Bool
  @_101::Bool
  @_102::UInt8
  @_103::Bool
Body::Int64
...

@KristofferC KristofferC added this to the 1.11 milestone Jan 5, 2024
@akshaypainjane
Copy link

Hi @stevengj I modified the Ryu.writeexp function to explicitly remove the Union types and replace those variable initialization (for digits, lastDigit, c, i) with appropriate Unsigned integer datatype.

I also tried by removing all instances of the @inbounds macro from Ryu.writeexp function as I thought skipping bound checking could be the reason for the error, as @inbounds was as added in #51273

But in both the cases, the issue still remains the same.

@stevengj
Copy link
Member Author

stevengj commented Jan 6, 2024

If the error remains the same, then you are passing a signed value for the digits argument, which means that you still have a type instability.

(Indeed, fixing the digits variable solved the problem. See #52781.)

KristofferC pushed a commit that referenced this issue Jan 8, 2024
KristofferC pushed a commit that referenced this issue Jan 24, 2024
Drvi pushed a commit to RelationalAI/julia that referenced this issue Jun 7, 2024
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants