在Julia中:Float64和BigFloat的相等

在Julia中:Float64和BigFloat的相等,julia,precision,arbitrary-precision,Julia,Precision,Arbitrary Precision,在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 eq

在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 doc似乎没有涵盖此类内容:

编辑: 根据下面@EPo的一条有用的评论,很容易将上面的所有比较都变成事实。例如,下面的第1行和第3行为真,但上面的第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

某些浮点数可以精确表示(26.0),但不能全部表示,例如:

julia> using Printf
julia> @printf("%.80f",26.0)
26.00000000000000000000000000000000000000000000000000000000000000000000000000000000
julia> @printf("%.80f",0.1)
0.10000000000000000555111512312578270211815834045410156250000000000000000000000000
例如,小数0.5、0.25、0.125也可以用基于二进制的浮点表示法精确表示。例如,你有:

julia> 26.125 - 0.125 == big"26.125" - 0.125
true
但是0.1在二进制中是一个周期数,所以它是四舍五入的

julia> bitstring(0.1)
"0011111110111001100110011001100110011001100110011001100110011010"

最后52位表示二进制分数。它们不一样的原因是因为它们不一样

julia> using Printf
julia> string(BigFloat("26.1")-BigFloat("26"))
"1.000000000000000000000000000000000000000000000000000000000000000000000000000553e-01"
julia> @printf("%.17e",Float64(26.1)-Float64(26))
1.00000000000001421e-01
julia> Float64(26.1)-Float64(26) > BigFloat("26.1")-BigFloat("26")
true

我不认为朱莉娅在幕后做任何事情,但你正在尝试使用它≈ (近似相等,\近似)@crstnbr,我现在从下面公认的答案中看出你是对的。我没想到26.0会在Float64中如此准确地表示出来。@EPo,谢谢你的建议。回答得好!谢谢。这可能也很有趣:这段视频显示了Julia 1.0中的值是如何比较的。感谢您的评论。但是,它是否解释了上面的第2行,其中
26.0==big“26.0”
的计算结果为
true
?在Float64和BigFloat中,数字26.0都是整数26。任何整数都可以在浮点系统Float64和BigFloat中精确表示。由于整数可以精确表示,它们都是相同的,因此计算结果将为true。感谢您的评论。这就是让我吃惊的核心。