UnS*_*Sat 2 floating-point ocaml comparison-operators
在OCaml中,将Integer 0与Integer 进行比较0返回true; 但是,比较Float 0.和Float 0.返回false:
# 0 == 0;;
- : bool = true
# 0. == 0.;;
- : bool = false
Run Code Online (Sandbox Code Playgroud)
如何比较浮动正确?
不要使用==,这是一种专门的"物理平等".使用=日常的代码.
# 0 = 0;;
- : bool = true
# 0.0 = 0.0;;
- : bool = true
Run Code Online (Sandbox Code Playgroud)
对于不平等,请使用<>.该!=操作者为"物理不等式",这也应避免象在日常代码瘟疫.
# 0 <> 0;;
- : bool = false
# 0.0 <> 0.0;;
- : bool = false
Run Code Online (Sandbox Code Playgroud)