为什么
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
为什么不会在模式雷亚尔工作像这样的吗?
fun fact 0.0 = 1.0 | fact x = x * fact (x - 1.0)
它给出了错误:
Error: syntax error: inserting EQUALOP
回答
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 useReal.==
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的文章,其中总结了一些陷阱。
- 1. 为什么我无法比较新阵列的数量?
- 2. 这个Standard ML bubblesort的循环为什么只执行一次?
- 3. 为什么我无法在MFC中比较CString
- 4. 为什么我的比较程序无法正常工作?
- 5. 为什么我无法比较此字符?
- 6. Java:为什么我无法读取和比较文本文件?
- 7. 为什么我无法比较这些十六进制值?
- 8. 什么比较方法比较好?
- 9. 什么是无法修复的错误,为什么我无法比较列表中的值?
- 10. 比较器为什么要实现Serializable?
- 11. 为什么我的比较失败?
- 12. 为什么NSNumber比较方法不同?
- 13. 是什么导致此Standard-ML类型错误?
- 14. 为什么整数比较比字符串比较快?
- 15. 为什么string.IsNullOrEmpty比比较快?
- 16. 为什么set_symmetric_difference无法与比较器一起使用?
- 17. 比较无法比较
- 18. String-bool比较 - 为什么?
- 19. 为什么当我运行多线程的方法比较慢?
- 20. 比较两个整数:为什么==真?
- 21. 为什么我使用简单的json库后无法比较字符串?
- 22. 为什么我无法比较getActionCommand()和匹配的字符串值?
- 23. 无法比较int?为null
- 24. pl-sql它无法比较两个字符串?为什么
- 25. 比较运算符在javascript中的数组上比较什么?
- 26. 为什么我的比较计数功能不计算掉期?
- 27. 为什么我们不能使用O-Notation来比较算法?
- 28. 比较ActionScript中的实数
- 29. 实数比较
- 30. 为什么我比较NSString的时候出错? ( - [__ NSCFNumber isEqualToString:]:无法识别的选择器发送到实例
而不是对这个问题的答案作为对不同问题的答案的一部分,在问题再次被问到时,这里更容易连接。 –
[cs.SE]网站有像这样的问题的'reference-question'标签。这样的标签会有帮助吗? –
@AntonTrunov:我不知道。这听起来很有用,但我可能不会自己拿出来使用它。也许这是一种文化事物。也许在常规的StackOverflow中有相同的东西吗? –