2016-12-30 21 views
2
  1. 为什么1.0 = 2.0不工作?是不是真的一个平等的类型?为什么我无法比较Standard ML中的实数?

    它给人的错误:

    Error: operator and operand don't agree [equality type required] 
        operator domain: ''Z * ''Z 
        operand:   real * real 
        in expression: 
        1.0 = 2.0 
    
  2. 为什么不会在模式雷亚尔工作像这样的吗?

    fun fact 0.0 = 1.0 
        | fact x = x * fact (x - 1.0) 
    

    它给出了错误:

    Error: syntax error: inserting EQUALOP 
    
+1

而不是对这个问题的答案作为对不同问题的答案的一部分,在问题再次被问到时,这里更容易连接。 –

+1

[cs.SE]网站有像这样的问题的'reference-question'标签。这样的标签会有帮助吗? –

+1

@AntonTrunov:我不知道。这听起来很有用,但我可能不会自己拿出来使用它。也许这是一种文化事物。也许在常规的StackOverflow中有相同的东西吗? –

回答

4

Why doesn't 1.0 = 2.0 work? Isn't real an equality type?

号的类型变量''Z指示的=的操作数必须具有平等的类型。

Why won't reals in patterns work [...]?

模式匹配隐含地依赖于测试的相等性。神秘的错误信息syntax error: inserting EQUALOP表明SML/NJ解析器不允许在期望模式的情况下使用浮点文字,因此程序员不能接收更有意义的类型错误。

要精心,

http://www.smlnj.org/doc/FAQ/faq.txt

Q: Is real an equality type?

A: It was in SML '90 and SML/NJ 0.93, but it is not in SML '97 and SML/NJ 110. So 1.0 = 1.0 will cause a type error because "=" demands arguments that have an equality type. Also, real literals cannot be used in patterns.

http://mlton.org/PolymorphicEquality

The one ground type that can not be compared is real. So, 13.0 = 14.0 is not type correct. One can use Real.== to compare reals for equality, but beware that this has different algebraic properties than polymorphic equality.

http://sml-family.org/Basis/real.html

Deciding if real should be an equality type, and if so, what should equality mean, was also problematic. IEEE specifies that the sign of zeros be ignored in comparisons, and that equality evaluate to false if either argument is NaN.

These constraints are disturbing to the SML programmer. The former implies that 0 = ~0 is true while r/0 = r/~0 is false. The latter implies such anomalies as r = r is false, or that, for a ref cell rr, we could have rr = rr but not have !rr = !rr. We accepted the unsigned comparison of zeros, but felt that the reflexive property of equality, structural equality, and the equivalence of <> and not o = ought to be preserved.

简短版本:不要使用相等比较实数。执行epsilon测试。我会推荐阅读关于http://floating-point-gui.de/errors/comparison的文章,其中总结了一些陷阱。

相关问题