4

In OCaml, comparing Integer 0 with Integer 0 returns true; however, comparing Float 0. to Float 0. returns false:

# 0 == 0;;
- : bool = true
# 0. == 0.;;
- : bool = false

How does one compare floats correctly?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
UnSat
  • 1,347
  • 2
  • 14
  • 28

1 Answers1

7

Don't use ==, which is a specialized "physical equality". Use = for everyday code.

# 0 = 0;;
- : bool = true
# 0.0 = 0.0;;
- : bool = true

For inequality, use <>. The != operator is for "physical inequality", which should also be avoided like the plague in everyday code.

# 0 <> 0;;
- : bool = false
# 0.0 <> 0.0;;
- : bool = false
Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108