Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Dzielenie zmiennych, Co jest nie tak z tym skryptem ???
rkolida
post
Post #1





Grupa: Zarejestrowani
Postów: 37
Pomógł: 0
Dołączył: 28.12.2004

Ostrzeżenie: (0%)
-----


Witam,
niech ktos mi powie dlaczego ten prosty skrypt nie wypisuje w wyniku dzialania: STARCZY PUNKTOW !

co tu jest nie tak (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif) ?
  1. <?
  2. $punkty = 0.6;
  3. $ludzie = 3;
  4. $dzielenie = $punkty / $ludzie;
  5.  
  6. if ( $dzielenie >= 0.) {
  7.     echo &#092;"STARCZY PUNKTOW !\";
  8. }
  9.  
  10. ?>


Dodam jeszcze tylko, ze ponizszy dziala, wynik 0.3 jest widziany przez IFa, a 0.2 juz NIE! DLACZEGO (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif) (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)
  1. <?
  2. $punkty = 0.6;
  3. $ludzie = 2;
  4. $dzielenie = $punkty / $ludzie;
  5.  
  6. if ( $dzielenie >= 0.) {
  7.     echo &#092;"STARCZY PUNKTOW !\";
  8. }
  9.  
  10. ?>


Czy moze jest jakas funkcja do dzielenia, bo trace glowe jak widze takie sytuacje..

Ten post edytował rkolida 27.04.2005, 15:56:27
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
dr_bonzo
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

Ostrzeżenie: (0%)
-----


Sorki.
W koncu studia sie do czegos przydadza (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
Floaty sa zapisywane w systemie IEEE 754 (hehe), uzywa on systemu binarnego do zapisania liczby
(1)^(znak) * (znacznik) * 2 ^ (wykladnik) == 32 bity

0.2 w dziesietnym to bedzie
0011 1101 1100 1100 1100 1100 1100 1100b w syst zmiennoprzecinkowym pojedynczej precyzji ieee754

dzielenie 0.6 / 3 tez da ci liczbe z. p. p. ale moze sie troszeczke roznic:
0.6 = 0111 1111 0001 1001 1001 1001 1001 1001b

0.6/3 = 1,(1001)b x 2^(-3) d! =
0111 1101 1100 1100 1100 1100 1100 1100b, hmmm tyle samo

No dobra ja chce miec z tego tylko troje (IMG:http://forum.php.pl/style_emoticons/default/biggrin.gif) , moze stosuja inny system zaokraglania, albo gdzies sie machnalem. Zasada jest prosta: 0.2 nie ma skonczonego rozwiniecia w systemi binarnym i 0.2 i 0.6/3 pewnie gdzies sie roznia na odleglym bicie ale przy wyswietlaniu pokazuje ze to takie same wartosci.

http://us4.php.net/operators.comparison
Cytat
Concerning floats: It is simply pointless to compare a float with the value "0.3". The number 0.3 is not exactly representable in binary. Period. So is the number 0.1, and an infinite number of others numbers. Just like 1/3 is not exactly representable in decimal. How would you code the test for your float to be one third? Maybe $myFloat == 0.33333333333  Hmm: you see: Everyone would agree that this test is not accurate.

The test $myFloat == 0.3 is making exactly the same mistake.

So the float which you think should be 0.3 is really something very close to it; if you print it in decimal, the conversion will end up with the closest decimal representation, which may well be "0.3". But "0.3" is also the "right display decimal" for hundreds of float values.

The correct way to "compare" floats is: ( $myFloat - 0.3 ) < $EPSILON where $EPSILON is something like 1e-10, depending on your applic
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 14.10.2025 - 14:41