2

Can somebody please explain to me step by step, why does the following code print out 7?

echo (int)((0.7+0.1)*'10th');
Ofelia Muradova
  • 127
  • 3
  • 17
  • that is very interesting: I also tried to force casting with `(int)((0.7 + 0.1)*(int)('10th'))` and it still prints `7` – Alexandre Dec 14 '16 at 12:42
  • 1
    What would you *expect* the "correct" result to be? Are you aware of floating point imprecision and that you cannot *expect* any particular result to begin with? – deceze Dec 14 '16 at 12:50
  • 1
    I don't see any resemblances with the duplicate. And this is not the first time. I guess the permissiveness of people with the high score will kill this site. It is really a pity. – sevavietl Dec 14 '16 at 12:52
  • @deceze, if you remove `(int)` casting you will get `8`, so `7` is really unexpected result. – sevavietl Dec 14 '16 at 12:54
  • 3
    @Rizier123 it's not duplicate. `http://php.net/manual/en/language.types.float.php` have "big red warning" with your example @ShiroiOkami – Yupik Dec 14 '16 at 13:00
  • 1
    @sevavietl You will get a floating point number which is so close to 8 that it's displayed rounded up as 8, but when you floor the value (cast to int, same thing here) it's *actually* 7… – deceze Dec 14 '16 at 13:02
  • @deceze, I see. thanks. – sevavietl Dec 14 '16 at 13:08
  • @deceze Thank you, it's clear now! – Ofelia Muradova Dec 14 '16 at 13:15
  • @Yupik I will read it, thank you! – Ofelia Muradova Dec 14 '16 at 13:16

0 Answers0