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

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

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

function mysqrt_step(a,x)
    0.5*(a + a/x)
end

function mysqrt(a,x; tol = 1e-8)
    iters = 0
    x_prime = 0.0
    Δ = 1000.0
    
    while dist > tol
        x_prime = mysqrt_step(a,x)
        Δ = abs(x - x_prime)
        x = x_prime
        iters += 1
    end
    
    return(x,iters)
end    

using Printf  # use the Printf package (built-in)
function sqrt_table()
    @printf("%2s %10s %10s %10s \n", "a", "mysqrt", "sqrt", "Iterations")
    for a in 2:10
        tmp = mysqrt(a,a^2)
        @printf("%2d %10f %10f %10d \n", a, tmp[1], sqrt(a), tmp[2])
    end
end

The error message is

> ERROR: LoadError: UndefVarError: @printf not defined

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

version information

Julia Version 1.7.1
Commit ac5cc99908 (2021-12-22 19:35 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, skylake)
Environment:
  JULIA_EDITOR = code
  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:

julia> sqrt_table()
 a     mysqrt       sqrt Iterations
ERROR: UndefVarError: dist not defined
Stacktrace:
 [1] mysqrt(a::Int64, x::Int64; tol::Float64)
   @ 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:

julia> sqrt_table()
 a     mysqrt       sqrt Iterations
 2   1.618034   1.414214         21
 3   2.186141   1.732051         18
 4   2.732051   2.000000         16
 5   3.265564   2.236068         15
 6   3.791288   2.449490         14
 7   4.311738   2.645751         14
 8   4.828427   2.828427         13
 9   5.342329   3.000000         12
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:

julia> sqrt_table()
 a     mysqrt       sqrt Iterations
ERROR: UndefVarError: dist not defined
Stacktrace:
 [1] mysqrt(a::Int64, x::Int64; tol::Float64)
   @ 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:

julia> sqrt_table()
 a     mysqrt       sqrt Iterations
 2   1.618034   1.414214         21
 3   2.186141   1.732051         18
 4   2.732051   2.000000         16
 5   3.265564   2.236068         15
 6   3.791288   2.449490         14
 7   4.311738   2.645751         14
 8   4.828427   2.828427         13
 9   5.342329   3.000000         12
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:

确定