0

I have a simple comparison in my code: [simplified]

if (1.7 >= 17 * 0.1) {
  echo '1.7 is greater than or equal to ' . 17 * 0.1;
} else {
  echo '1.7 is NOT greater than or equal to ' . 17 * 0.1;
}

It gives the result:

1.7 is NOT greater than or equal to 1.7

I wonder can anybody explain this?

Sibin John Mattappallil
  • 1,739
  • 4
  • 23
  • 37

1 Answers1

1

This is because native operator are indeed interpreted in base 2, and most base 10 decimal converts badly in base 2.

If you need precision, you need to either only manipulate integers (converts perfectly to base 2) or use a dedicated lib such as bcmath (I wrote a wrapper to ease bcmath usage Math)

fab2s
  • 838
  • 2
  • 8
  • 14