LoadError在函数内调用@printf时发生

huangapple go评论77阅读模式
英文:

LoadError when calling @printf within a function

问题

ERROR: LoadError: UndefVarError: @printf not defined

英文:

I try to print a table of results within a function by calling the macro @printf. However, when I execute the code in Vscode (julia: execute the code in REPL), the interpreter throws a LoadError to me.
The code is following

  1. function mysqrt_step(a,x)
  2. 0.5*(a + a/x)
  3. end
  4. function mysqrt(a,x; tol = 1e-8)
  5. iters = 0
  6. x_prime = 0.0
  7. Δ = 1000.0
  8. while dist > tol
  9. x_prime = mysqrt_step(a,x)
  10. Δ = abs(x - x_prime)
  11. x = x_prime
  12. iters += 1
  13. end
  14. return(x,iters)
  15. end
  16. using Printf # use the Printf package (built-in)
  17. function sqrt_table()
  18. @printf("%2s %10s %10s %10s \n", "a", "mysqrt", "sqrt", "Iterations")
  19. for a in 2:10
  20. tmp = mysqrt(a,a^2)
  21. @printf("%2d %10f %10f %10d \n", a, tmp[1], sqrt(a), tmp[2])
  22. end
  23. end

The error message is

> ERROR: LoadError: UndefVarError: @printf not defined

To be noticed, I have using Printf before defining the function

version information

  1. Julia Version 1.7.1
  2. Commit ac5cc99908 (2021-12-22 19:35 UTC)
  3. Platform Info:
  4. OS: Windows (x86_64-w64-mingw32)
  5. CPU: Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
  6. WORD_SIZE: 64
  7. LIBM: libopenlibm
  8. LLVM: libLLVM-12.0.1 (ORCJIT, skylake)
  9. Environment:
  10. JULIA_EDITOR = code
  11. JULIA_NUM_THREADS =

答案1

得分: 1

以下是翻译好的内容:

"Thanks for providing a minimum working example, but do make sure to re-run it in a fresh Julia session to make sure it actually reproduces your problem.

When I copy paste your code into a fresh Julia 1.7 session and run the sqrt_table() function, I get:

  1. julia> sqrt_table()
  2. a mysqrt sqrt Iterations
  3. ERROR: UndefVarError: dist not defined
  4. Stacktrace:
  5. [1] mysqrt(a::Int64, x::Int64; tol::Float64)
  6. @ Main .\REPL[2]:6

So as Bill says in his comment, the error here is that the reference to dist in the while loop is undefined, and you probably meant to write while Δ > tol.

Also note that before the function errors, it prints the header line that you put into sqrt_table(), so there's actually no issue with Printf in your code.

With the distance variable name fixed, your code returns:

  1. julia> sqrt_table()
  2. a mysqrt sqrt Iterations
  3. 2 1.618034 1.414214 21
  4. 3 2.186141 1.732051 18
  5. 4 2.732051 2.000000 16
  6. 5 3.265564 2.236068 15
  7. 6 3.791288 2.449490 14
  8. 7 4.311738 2.645751 14
  9. 8 4.828427 2.828427 13
  10. 9 5.342329 3.000000 12
  11. 10 5.854102 3.162278 12
英文:

Thanks for providing a minimum working example, but do make sure to re-run it in a fresh Julia session to make sure it actually reproduces your problem.

When I copy paste your code into a fresh Julia 1.7 session and run the sqrt_table() function, I get:

  1. julia> sqrt_table()
  2. a mysqrt sqrt Iterations
  3. ERROR: UndefVarError: dist not defined
  4. Stacktrace:
  5. [1] mysqrt(a::Int64, x::Int64; tol::Float64)
  6. @ Main .\REPL[2]:6

So as Bill says in his comment, the error here is that the reference to dist in the while loop is undefined, and you probably meant to write while Δ > tol.

Also note that before the function errors, it prints the header line that you put into sqrt_table(), so there's actually no issue with Printf in your code.

With the distance variable name fixed, your code returns:

  1. julia> sqrt_table()
  2. a mysqrt sqrt Iterations
  3. 2 1.618034 1.414214 21
  4. 3 2.186141 1.732051 18
  5. 4 2.732051 2.000000 16
  6. 5 3.265564 2.236068 15
  7. 6 3.791288 2.449490 14
  8. 7 4.311738 2.645751 14
  9. 8 4.828427 2.828427 13
  10. 9 5.342329 3.000000 12
  11. 10 5.854102 3.162278 12

huangapple
  • 本文由 发表于 2023年2月24日 10:13:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75552052.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定