在 Julia 中使用 Angle 时出错

Error using Angle in Julia

我只是想求一个复数的角度。为了测试 Julia 中提供的角度函数,我首先尝试了:

 angle(1+im)

但我收到以下错误消息:

MethodError: objects of type Float64 are not callable

Stacktrace: [1] include_string(::String, ::String) at ./loading.jl:515

当我尝试询问 typeof(1+im) Julia 回答时

Complex{Int64}

我不明白角度函数是怎么回事。我尝试使用 1+1*im 但我得到了相同的答案。

有人可以帮忙吗? 我也可以根据坐标计算角度,但我也想了解 Julia 语言。

谢谢

有时可能会不小心定义一个与您不知道的内置函数同名的变量。例如,有一个名为 angle() 的函数,但您可以这样做:

julia> angle=1.5
1.5

julia> angle(1+1im)
ERROR: MethodError: objects of type Float64 are not callable

julia> typeof(angle)
Float64

现在你不能 'see' 函数定义,因为你的变量隐藏了它。

要解决此问题,您可以键入:

julia> angle=Base.angle
angle (generic function with 3 methods)

julia> angle(1+1im)
0.7853981633974483

一个很好的问题是,为什么 Julia 不告诉我我刚刚覆盖了一个基本函数?

附录

答案(感谢@Matt 的评论)是,如果您使用它,然后然后重新定义它,您将收到警告:

              _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.0 (2017-06-19 13:05 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-apple-darwin13.4.0

julia> angle(1+2im)
1.1071487177940904

julia> angle=1.5
WARNING: imported binding for angle overwritten in module Main
1.5