【发布时间】:2018-09-09 20:15:09
【问题描述】:
在 Julia 1.0.0 REPL 中,我得到以下结果:
# Line 1: This make sense. I did not expect a Float64 to equal a BigFloat.
julia> 26.1 == big"26.1"
false
# Line 2: This surprised me when Line 1 is considered. Again, like Line 1, I
# did not expect a Float64 to equal an equivalent BigFloat.
julia> 26.0 == big"26.0"
true
# Line 3: This I expected based on Line 1 behavior.
julia> 26.1 - 0.1 == big"26.1" - 0.1
false
# Line 4: This surprised me based on Line 1 behavior, but it might be
# explained based on Line 2 behavior. It seems to imply that if a Float64
# can be converted to an Integer it will compare equal to an equivalent BigFloat.
julia> 26.1 - 0.1 == big"26.1" - big"0.1"
true
似乎 Julia 在这里做了一些事情来与 Float64 和 BigFloat 进行相等比较,这使得第 2 行和第 4 行为真,而第 1 和第 3 行为假。有什么建议吗?
关于“==”的 Julia 文档似乎没有涵盖这种事情: https://docs.julialang.org/en/v1/base/math/#Base.:==
编辑: 根据下面@EPo 的有用评论,很容易使上面的所有比较都成真。例如,第 1 行和第 3 行在下面为真,尽管它们在上面为假:
# Line 1 is now true.
julia> 26.1 ≈ big"26.1"
true
# Line 3 is now true.
julia> 26.1 - 0.1 ≈ big"26.1" - 0.1
true
【问题讨论】:
-
我认为 Julia 并没有在幕后做任何事情,但你遇到了 exploringbinary.com/…
-
尝试使用 ≈ (近似相等,\approx
) -
@crstnbr,我现在从下面接受的答案中看到你是对的。我没想到 26.0 会在 Float64 中如此准确地表示。
-
@EPo,感谢您的建议。
标签: julia precision arbitrary-precision