JuliaDiff / ForwardDiff.jl

Forward Mode Automatic Differentiation for Julia
Other
897 stars 148 forks source link

printf formatting causes stack overflow #289

Open ikirill opened 6 years ago

ikirill commented 6 years ago

I know it's not really well-defined to try to print a dual number like this, but it would be better if it failed more gracefully, for example by printing the number with the right formatting and omitting the partials, or printing the partials with the same formatting, or something.

julia> Pkg.status("ForwardDiff")
 - ForwardDiff                   0.7.0              master

julia> ForwardDiff.derivative(x -> (@printf "%f\n" x; x), 0.0)
ERROR: StackOverflowError:
Stacktrace:
 [1] fix_dec(::ForwardDiff.Dual{ForwardDiff.Tag{##3#4,Float64},Float64,1}, ::Int64) at ./printf.jl:949 (repeats 80000 times)

julia> ForwardDiff.derivative(x -> (@printf "%s\n" x; x), 0.0)
Dual{ForwardDiff.Tag{##5#6,Float64}}(0.0,1.0)
1.0

julia> ForwardDiff.derivative(x -> (@printf "%e\n" x; x), 0.0)
ERROR: StackOverflowError:
Stacktrace:
 [1] ini_dec(::ForwardDiff.Dual{ForwardDiff.Tag{##7#8,Float64},Float64,1}, ::Int64) at ./printf.jl:970 (repeats 80000 times)

julia> ForwardDiff.derivative(x -> (@printf "%.2e\n" x; x), 0.0)
ERROR: StackOverflowError:
Stacktrace:
 [1] ini_dec(::ForwardDiff.Dual{ForwardDiff.Tag{##9#10,Float64},Float64,1}, ::Int64) at ./printf.jl:970 (repeats 80000 times)

julia> ForwardDiff.derivative(x -> (@printf "%g\n" x; x), 0.0)
ERROR: StackOverflowError:
Stacktrace:
 [1] ini_dec(::ForwardDiff.Dual{ForwardDiff.Tag{##11#12,Float64},Float64,1}, ::Int64) at ./printf.jl:970 (repeats 80000 times)
julia> versioninfo()
Julia Version 0.6.0
Commit 903644385b (2017-06-19 13:05 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin13.4.0)
  CPU: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, haswell)
jerlich commented 5 years ago

Yes, it would be nice if the print functions works for Duals