Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Problem z operatorem porównania
Forum PHP.pl > Forum > Przedszkole
hubertinio
Witam
Mam problem ze zrozumieniem kawałka kodu.
Czy ktoś mógłby mi wytłumaczyć dlaczego w if jest interpretowane jako false?

  1.  
  2. $a = 0.0;
  3. for ($i =0; $i < 10; $i++) {
  4. $a += 0.1;
  5. }
  6.  
  7. print "$a\n"; // 1
  8.  
  9. if ($a == 1.0) {
  10. print "Equals";
  11. } else {
  12. print "Not Equals";
  13. }
  14.  


Wynik: 1 Not Equals

nospor
It's magic wink.gif

http://www.php.net/manual/pl/language.types.float.php
Cytat
for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118....
So never trust floating number results to the last digit, and never compare floating point numbers for equality.
hubertinio
Dzięki ..a little bit of magic biggrin.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.