Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Arno Welzel Newsgroups: de.comp.lang.php Subject: Re: var_dump(8.7 <= 8.7) / false? Date: Thu, 15 Nov 2018 09:15:05 +0100 Lines: 60 Message-ID: References: <61959674.20181115012304@gmx.net> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: individual.net miqx6nVhTmt0gMsbnrG0MwRp3fDuS9Hawzfh0CX6jdg3nzX3VJ Cancel-Lock: sha1:NdNfU63vjEPYjtaRQDTrhPXep4I= Openpgp: preference=signencrypt In-Reply-To: Xref: csiph.com de.comp.lang.php:4431 Arno Welzel: > Stefan Mayer: > >> Hallo Leute, >> >> eine kleine Funktion die Werte zwischen "min" und "max" >> >> ``` >> > function f($min, $max, $step) { >> $values = []; >> for ($i = $min; $i<=$max; $i+=$step) { >> $values[] = $i; >> } >> return $values; >> } >> ``` >> >> Eigentlich dachte ich das funktioniert, aber irgendwas funktioniert überhaupt >> nicht. >> >> Wieso unterscheidet sich das Ergebnis z.B. bei diesen beiden? >> >> print_r(f(11.3, 11.7, .4)); // 11.7 fehlt >> print_r(f(20.3, 20.7, .4)); // OK > > > Fließkommazahlen für Vergleiche zu benutzen, ist generell problematisch. > > Siehe auch: > > > > Zitat: > > "If you do it like this they should be the same. But note that a > characteristic of floating-point values is that calculations which seem > to result in the same value do not need to actually be identical. So if > $a is a literal .17 and $b arrives there through a calculation it can > well be that they are different, albeit both display the same value. > > Usually you never compare floating-point values for equality like this, > you need to use a smallest acceptable difference: > > if (abs(($a-$b)/$b) < 0.00001) { > echo "same"; > } > > Something like that." Ingrid weist mich noch darauf hin, dass das auch in der Dokumentation von PHP erwähnt ist: -- Arno Welzel https://arnowelzel.de