2016-12-30 2 views
2
  1. 1.0 = 2.0はなぜ機能しませんか? 本物は同等型ではありませんか?なぜスタンダード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.

短いバージョン:等しいを使って実数を比較しないでください。 イプシロンテストを実行します。私はhttp://floating-point-gui.de/errors/comparisonの記事を読むことをお勧めします。これは落とし穴のまとめです。