Julia 尝试添加不同类型的编号时发生MethodError

Julia 尝试添加不同类型的编号时发生MethodError,julia,Julia,运行此代码会让我: function random_step(x, dx) num=randn(1) return x + num*dx end random_step(1, 1) 所以我假设当我输入1时,数据类型不是Float64,randn是。那么,我怎样才能在julia控件中快速生成变量的数据类型呢?我经常需要这样做来调试代码。这对我和茱莉亚的关系来说也是个问题吗?我认为这就像python一样,我经常需要处理函数之间使用和传递的数据类型吗?问题是randn1生成数组{F

运行此代码会让我:

function random_step(x, dx)
    num=randn(1)
    return x + num*dx
end

random_step(1, 1)

所以我假设当我输入1时,数据类型不是Float64,randn是。那么,我怎样才能在julia控件中快速生成变量的数据类型呢?我经常需要这样做来调试代码。这对我和茱莉亚的关系来说也是个问题吗?我认为这就像python一样,我经常需要处理函数之间使用和传递的数据类型吗?

问题是randn1生成数组{Float64,1},而不是Float64

如果添加两个标量,即使它们不是同一类型的标量,其中一个或两个标量也将按预期升级,例如:

julia> random_step(1, 1)
ERROR: MethodError: no method matching +(::Int32, ::Array{Float64,1})       
For element-wise addition, use broadcasting with dot syntax: scalar .+ array
Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...) at operators.jl:538
  +(::T, ::T) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} at int.jl:86
  +(::Union{Int16, Int32, Int8}, ::BigInt) at gmp.jl:531
  ...
Stacktrace:
 [1] random_step(::Int32, ::Int32) at .\REPL[1]:3
 [2] top-level scope at REPL[2]:1
然而,并没有明确的规则来添加一个数组和一个标量,这正是您试图做的

出了什么问题? 函数randn1使用Float64类型的元素创建数组,而不仅仅是一个元素

您可以查看randn的文档并了解原因:

 julia> 1 + 1.0
 2.0
因此,通过使用1调用,可以指定您想要一个一维数组,其中唯一的维度有一个元素

如何修复它? 您应该将函数定义为

help?> randn
search: randn rand transcode macroexpand @macroexpand1 @macroexpand CartesianIndex CartesianIndices

  randn([rng=GLOBAL_RNG], [T=Float64], [dims...])

  Generate a normally-distributed random number of type T with mean 0 and standard deviation 1. Optionally
  generate an array of normally-distributed random numbers.
注意,我调用的是rand,它返回一个Float64,而不是rand1,它返回一个包含Float64类型元素的数组

这样,您可以看到您的示例是有效的:

function random_step(x, dx)
    num=randn()
    return x + num*dx
end

问题是randn1生成数组{Float64,1},而不是Float64

如果添加两个标量,即使它们不是同一类型的标量,其中一个或两个标量也将按预期升级,例如:

julia> random_step(1, 1)
ERROR: MethodError: no method matching +(::Int32, ::Array{Float64,1})       
For element-wise addition, use broadcasting with dot syntax: scalar .+ array
Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...) at operators.jl:538
  +(::T, ::T) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} at int.jl:86
  +(::Union{Int16, Int32, Int8}, ::BigInt) at gmp.jl:531
  ...
Stacktrace:
 [1] random_step(::Int32, ::Int32) at .\REPL[1]:3
 [2] top-level scope at REPL[2]:1
然而,并没有明确的规则来添加一个数组和一个标量,这正是您试图做的

出了什么问题? 函数randn1使用Float64类型的元素创建数组,而不仅仅是一个元素

您可以查看randn的文档并了解原因:

 julia> 1 + 1.0
 2.0
因此,通过使用1调用,可以指定您想要一个一维数组,其中唯一的维度有一个元素

如何修复它? 您应该将函数定义为

help?> randn
search: randn rand transcode macroexpand @macroexpand1 @macroexpand CartesianIndex CartesianIndices

  randn([rng=GLOBAL_RNG], [T=Float64], [dims...])

  Generate a normally-distributed random number of type T with mean 0 and standard deviation 1. Optionally
  generate an array of normally-distributed random numbers.
注意,我调用的是rand,它返回一个Float64,而不是rand1,它返回一个包含Float64类型元素的数组

这样,您可以看到您的示例是有效的:

function random_step(x, dx)
    num=randn()
    return x + num*dx
end

如果您尝试添加一个数字和一个列表,Python将告诉您完全相同的事情。不过,朱莉娅不会做的是,在没有你明确要求的情况下进行广播,就像努比一样。@phipsgabler谢谢!如果您尝试添加一个数字和一个列表,Python将告诉您完全相同的事情。不过,朱莉娅不会做的是,在没有你明确要求的情况下进行广播,就像努比一样。@phipsgabler谢谢!谢谢你的详细回答谢谢你的详细回答