Ankündigung

Einklappen
Keine Ankündigung bisher.

[Erledigt] kurioser for-schleifen fehler

Einklappen

Neue Werbung 2019

Einklappen
X
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • [Erledigt] kurioser for-schleifen fehler

    Hallo,ich habe einen merkwürdigen Fehler in PHP endeckt.
    Da ich eine Berechnung mit einer bestimmten Schrittweite benötige,habe ich diese for-schleife verwendet:
    PHP-Code:
    for($i=0;$i<10;$i+=0.01)
    {
        echo 
    $i.'</br>'
       
    //+ noch anderer Code

    Statt 4.43 kommt bei mir 4.4299999999 und das zieht sich dann bis zur 10 so durch.Wenn ich ob_start(),ob_get_contents,ob_end_clear() [Also den Ausgabepuffer] verwende tritt der Fehler bei 3.38->3.39 auf.
    ich habe xampp installiert mit der PHP Version 5.5.11.

    Ich wollte mal wissen,ob das normal ist oder ob des nur bei mir vorkommt


  • #2
    Die Ausgaben sind korrekt. Bei mir kommt das gleiche raus.

    Das ist aber kein direkter Fehler, sondern liegt daran wie Zahlen gespeichert werden. Einen schönen Artikel darüber findest du in Wikipedia.
    Die round-Funktion löst dein Problem.

    Wenn du mit Geldwerten rechnest, solltest du deine Funktionen aus diesem Grund auch immer ausführlich testen (ein paar Standardwerte UND Extremwerte). Es gibt außerdem die Möglichkeit die Zahlenwerte in Zeichenketten umzuwandeln. Das wird vor allem dann gemacht, wenn die Zahlenwerte den Wertebereich der Variablen sprengen könnten.

    Kommentar


    • #3
      http://php.net/manual/de/language.types.float.php

      Großen roten Kasten lesen.

      Kommentar


      • #4
        Das kannst du auch bit-wise precision-safe kalkulieren innerhalb einer for-schleife:

        PHP-Code:
        bcscale(2);

        for ( 
        $i '0'; -=== bccomp($i'10'); $i bcadd($i'0.01') ) {
            
        var_dump($i);

        Code:
        string(1) "0"
        string(4) "0.01"
        string(4) "0.02"
        string(4) "0.03"
        string(4) "0.04"
        string(4) "0.05"
        string(4) "0.06"
        string(4) "0.07"
        string(4) "0.08"
        string(4) "0.09"
        string(4) "0.10"
        string(4) "0.11"
        string(4) "0.12"
        string(4) "0.13"
        string(4) "0.14"
        string(4) "0.15"
        string(4) "0.16"
        string(4) "0.17"
        string(4) "0.18"
        string(4) "0.19"
        string(4) "0.20"
        string(4) "0.21"
        string(4) "0.22"
        string(4) "0.23"
        string(4) "0.24"
        string(4) "0.25"
        string(4) "0.26"
        string(4) "0.27"
        string(4) "0.28"
        string(4) "0.29"
        string(4) "0.30"
        string(4) "0.31"
        string(4) "0.32"
        string(4) "0.33"
        string(4) "0.34"
        string(4) "0.35"
        string(4) "0.36"
        string(4) "0.37"
        string(4) "0.38"
        string(4) "0.39"
        string(4) "0.40"
        string(4) "0.41"
        string(4) "0.42"
        string(4) "0.43"
        string(4) "0.44"
        string(4) "0.45"
        string(4) "0.46"
        string(4) "0.47"
        string(4) "0.48"
        string(4) "0.49"
        string(4) "0.50"
        string(4) "0.51"
        string(4) "0.52"
        string(4) "0.53"
        string(4) "0.54"
        string(4) "0.55"
        string(4) "0.56"
        string(4) "0.57"
        string(4) "0.58"
        string(4) "0.59"
        string(4) "0.60"
        string(4) "0.61"
        string(4) "0.62"
        string(4) "0.63"
        string(4) "0.64"
        string(4) "0.65"
        string(4) "0.66"
        string(4) "0.67"
        string(4) "0.68"
        string(4) "0.69"
        string(4) "0.70"
        string(4) "0.71"
        string(4) "0.72"
        string(4) "0.73"
        string(4) "0.74"
        string(4) "0.75"
        string(4) "0.76"
        string(4) "0.77"
        string(4) "0.78"
        string(4) "0.79"
        string(4) "0.80"
        string(4) "0.81"
        string(4) "0.82"
        string(4) "0.83"
        string(4) "0.84"
        string(4) "0.85"
        string(4) "0.86"
        string(4) "0.87"
        string(4) "0.88"
        string(4) "0.89"
        string(4) "0.90"
        string(4) "0.91"
        string(4) "0.92"
        string(4) "0.93"
        string(4) "0.94"
        string(4) "0.95"
        string(4) "0.96"
        string(4) "0.97"
        string(4) "0.98"
        string(4) "0.99"
        string(4) "1.00"
        string(4) "1.01"
        string(4) "1.02"
        string(4) "1.03"
        string(4) "1.04"
        string(4) "1.05"
        string(4) "1.06"
        string(4) "1.07"
        string(4) "1.08"
        string(4) "1.09"
        string(4) "1.10"
        string(4) "1.11"
        string(4) "1.12"
        string(4) "1.13"
        string(4) "1.14"
        string(4) "1.15"
        string(4) "1.16"
        string(4) "1.17"
        string(4) "1.18"
        string(4) "1.19"
        string(4) "1.20"
        string(4) "1.21"
        string(4) "1.22"
        string(4) "1.23"
        string(4) "1.24"
        string(4) "1.25"
        string(4) "1.26"
        string(4) "1.27"
        string(4) "1.28"
        string(4) "1.29"
        string(4) "1.30"
        string(4) "1.31"
        string(4) "1.32"
        string(4) "1.33"
        string(4) "1.34"
        string(4) "1.35"
        string(4) "1.36"
        string(4) "1.37"
        string(4) "1.38"
        string(4) "1.39"
        string(4) "1.40"
        string(4) "1.41"
        string(4) "1.42"
        string(4) "1.43"
        string(4) "1.44"
        string(4) "1.45"
        string(4) "1.46"
        string(4) "1.47"
        string(4) "1.48"
        string(4) "1.49"
        string(4) "1.50"
        string(4) "1.51"
        string(4) "1.52"
        string(4) "1.53"
        string(4) "1.54"
        string(4) "1.55"
        string(4) "1.56"
        string(4) "1.57"
        string(4) "1.58"
        string(4) "1.59"
        string(4) "1.60"
        string(4) "1.61"
        string(4) "1.62"
        string(4) "1.63"
        string(4) "1.64"
        string(4) "1.65"
        string(4) "1.66"
        string(4) "1.67"
        string(4) "1.68"
        string(4) "1.69"
        string(4) "1.70"
        string(4) "1.71"
        string(4) "1.72"
        string(4) "1.73"
        string(4) "1.74"
        string(4) "1.75"
        string(4) "1.76"
        string(4) "1.77"
        string(4) "1.78"
        string(4) "1.79"
        string(4) "1.80"
        string(4) "1.81"
        string(4) "1.82"
        string(4) "1.83"
        string(4) "1.84"
        string(4) "1.85"
        string(4) "1.86"
        string(4) "1.87"
        string(4) "1.88"
        string(4) "1.89"
        string(4) "1.90"
        string(4) "1.91"
        string(4) "1.92"
        string(4) "1.93"
        string(4) "1.94"
        string(4) "1.95"
        string(4) "1.96"
        string(4) "1.97"
        string(4) "1.98"
        string(4) "1.99"
        string(4) "2.00"
        string(4) "2.01"
        string(4) "2.02"
        string(4) "2.03"
        string(4) "2.04"
        string(4) "2.05"
        string(4) "2.06"
        string(4) "2.07"
        string(4) "2.08"
        string(4) "2.09"
        string(4) "2.10"
        string(4) "2.11"
        string(4) "2.12"
        string(4) "2.13"
        string(4) "2.14"
        string(4) "2.15"
        string(4) "2.16"
        string(4) "2.17"
        string(4) "2.18"
        string(4) "2.19"
        string(4) "2.20"
        string(4) "2.21"
        string(4) "2.22"
        string(4) "2.23"
        string(4) "2.24"
        string(4) "2.25"
        string(4) "2.26"
        string(4) "2.27"
        string(4) "2.28"
        string(4) "2.29"
        string(4) "2.30"
        string(4) "2.31"
        string(4) "2.32"
        string(4) "2.33"
        string(4) "2.34"
        string(4) "2.35"
        string(4) "2.36"
        string(4) "2.37"
        string(4) "2.38"
        string(4) "2.39"
        string(4) "2.40"
        string(4) "2.41"
        string(4) "2.42"
        string(4) "2.43"
        string(4) "2.44"
        string(4) "2.45"
        string(4) "2.46"
        string(4) "2.47"
        string(4) "2.48"
        string(4) "2.49"
        string(4) "2.50"
        string(4) "2.51"
        string(4) "2.52"
        string(4) "2.53"
        string(4) "2.54"
        string(4) "2.55"
        string(4) "2.56"
        string(4) "2.57"
        string(4) "2.58"
        string(4) "2.59"
        string(4) "2.60"
        string(4) "2.61"
        string(4) "2.62"
        string(4) "2.63"
        string(4) "2.64"
        string(4) "2.65"
        string(4) "2.66"
        string(4) "2.67"
        string(4) "2.68"
        string(4) "2.69"
        string(4) "2.70"
        string(4) "2.71"
        string(4) "2.72"
        string(4) "2.73"
        string(4) "2.74"
        string(4) "2.75"
        string(4) "2.76"
        string(4) "2.77"
        string(4) "2.78"
        string(4) "2.79"
        string(4) "2.80"
        string(4) "2.81"
        string(4) "2.82"
        string(4) "2.83"
        string(4) "2.84"
        string(4) "2.85"
        string(4) "2.86"
        string(4) "2.87"
        string(4) "2.88"
        string(4) "2.89"
        string(4) "2.90"
        string(4) "2.91"
        string(4) "2.92"
        string(4) "2.93"
        string(4) "2.94"
        string(4) "2.95"
        string(4) "2.96"
        string(4) "2.97"
        string(4) "2.98"
        string(4) "2.99"
        string(4) "3.00"
        string(4) "3.01"
        string(4) "3.02"
        string(4) "3.03"
        string(4) "3.04"
        string(4) "3.05"
        string(4) "3.06"
        string(4) "3.07"
        string(4) "3.08"
        string(4) "3.09"
        string(4) "3.10"
        string(4) "3.11"
        string(4) "3.12"
        string(4) "3.13"
        string(4) "3.14"
        string(4) "3.15"
        string(4) "3.16"
        string(4) "3.17"
        string(4) "3.18"
        string(4) "3.19"
        string(4) "3.20"
        string(4) "3.21"
        string(4) "3.22"
        string(4) "3.23"
        string(4) "3.24"
        string(4) "3.25"
        string(4) "3.26"
        string(4) "3.27"
        string(4) "3.28"
        string(4) "3.29"
        string(4) "3.30"
        string(4) "3.31"
        string(4) "3.32"
        string(4) "3.33"
        string(4) "3.34"
        string(4) "3.35"
        string(4) "3.36"
        string(4) "3.37"
        string(4) "3.38"
        string(4) "3.39"
        string(4) "3.40"
        string(4) "3.41"
        string(4) "3.42"
        string(4) "3.43"
        string(4) "3.44"
        string(4) "3.45"
        string(4) "3.46"
        string(4) "3.47"
        string(4) "3.48"
        string(4) "3.49"
        string(4) "3.50"
        string(4) "3.51"
        string(4) "3.52"
        string(4) "3.53"
        string(4) "3.54"
        string(4) "3.55"
        string(4) "3.56"
        string(4) "3.57"
        string(4) "3.58"
        string(4) "3.59"
        string(4) "3.60"
        string(4) "3.61"
        string(4) "3.62"
        string(4) "3.63"
        string(4) "3.64"
        string(4) "3.65"
        string(4) "3.66"
        string(4) "3.67"
        string(4) "3.68"
        string(4) "3.69"
        string(4) "3.70"
        string(4) "3.71"
        string(4) "3.72"
        string(4) "3.73"
        string(4) "3.74"
        string(4) "3.75"
        string(4) "3.76"
        string(4) "3.77"
        string(4) "3.78"
        string(4) "3.79"
        string(4) "3.80"
        string(4) "3.81"
        string(4) "3.82"
        string(4) "3.83"
        string(4) "3.84"
        string(4) "3.85"
        string(4) "3.86"
        string(4) "3.87"
        string(4) "3.88"
        string(4) "3.89"
        string(4) "3.90"
        string(4) "3.91"
        string(4) "3.92"
        string(4) "3.93"
        string(4) "3.94"
        string(4) "3.95"
        string(4) "3.96"
        string(4) "3.97"
        string(4) "3.98"
        string(4) "3.99"
        string(4) "4.00"
        string(4) "4.01"
        string(4) "4.02"
        string(4) "4.03"
        string(4) "4.04"
        string(4) "4.05"
        string(4) "4.06"
        string(4) "4.07"
        string(4) "4.08"
        string(4) "4.09"
        string(4) "4.10"
        string(4) "4.11"
        string(4) "4.12"
        string(4) "4.13"
        string(4) "4.14"
        string(4) "4.15"
        string(4) "4.16"
        string(4) "4.17"
        string(4) "4.18"
        string(4) "4.19"
        string(4) "4.20"
        string(4) "4.21"
        string(4) "4.22"
        string(4) "4.23"
        string(4) "4.24"
        string(4) "4.25"
        string(4) "4.26"
        string(4) "4.27"
        string(4) "4.28"
        string(4) "4.29"
        string(4) "4.30"
        string(4) "4.31"
        string(4) "4.32"
        string(4) "4.33"
        string(4) "4.34"
        string(4) "4.35"
        string(4) "4.36"
        string(4) "4.37"
        string(4) "4.38"
        string(4) "4.39"
        string(4) "4.40"
        string(4) "4.41"
        string(4) "4.42"
        string(4) "4.43"
        string(4) "4.44"
        string(4) "4.45"
        string(4) "4.46"
        string(4) "4.47"
        string(4) "4.48"
        string(4) "4.49"
        string(4) "4.50"
        string(4) "4.51"
        string(4) "4.52"
        string(4) "4.53"
        string(4) "4.54"
        string(4) "4.55"
        string(4) "4.56"
        string(4) "4.57"
        string(4) "4.58"
        string(4) "4.59"
        string(4) "4.60"
        string(4) "4.61"
        string(4) "4.62"
        string(4) "4.63"
        string(4) "4.64"
        string(4) "4.65"
        string(4) "4.66"
        string(4) "4.67"
        string(4) "4.68"
        string(4) "4.69"
        string(4) "4.70"
        string(4) "4.71"
        string(4) "4.72"
        string(4) "4.73"
        string(4) "4.74"
        string(4) "4.75"
        string(4) "4.76"
        string(4) "4.77"
        string(4) "4.78"
        string(4) "4.79"
        string(4) "4.80"
        string(4) "4.81"
        string(4) "4.82"
        string(4) "4.83"
        string(4) "4.84"
        string(4) "4.85"
        string(4) "4.86"
        string(4) "4.87"
        string(4) "4.88"
        string(4) "4.89"
        string(4) "4.90"
        string(4) "4.91"
        string(4) "4.92"
        string(4) "4.93"
        string(4) "4.94"
        string(4) "4.95"
        string(4) "4.96"
        string(4) "4.97"
        string(4) "4.98"
        string(4) "4.99"
        string(4) "5.00"
        string(4) "5.01"
        string(4) "5.02"
        string(4) "5.03"
        string(4) "5.04"
        string(4) "5.05"
        string(4) "5.06"
        string(4) "5.07"
        string(4) "5.08"
        string(4) "5.09"
        string(4) "5.10"
        string(4) "5.11"
        string(4) "5.12"
        string(4) "5.13"
        string(4) "5.14"
        string(4) "5.15"
        string(4) "5.16"
        string(4) "5.17"
        string(4) "5.18"
        string(4) "5.19"
        string(4) "5.20"
        string(4) "5.21"
        string(4) "5.22"
        string(4) "5.23"
        string(4) "5.24"
        string(4) "5.25"
        string(4) "5.26"
        string(4) "5.27"
        string(4) "5.28"
        string(4) "5.29"
        string(4) "5.30"
        string(4) "5.31"
        string(4) "5.32"
        string(4) "5.33"
        string(4) "5.34"
        string(4) "5.35"
        string(4) "5.36"
        string(4) "5.37"
        string(4) "5.38"
        string(4) "5.39"
        string(4) "5.40"
        string(4) "5.41"
        string(4) "5.42"
        string(4) "5.43"
        string(4) "5.44"
        string(4) "5.45"
        string(4) "5.46"
        string(4) "5.47"
        string(4) "5.48"
        string(4) "5.49"
        string(4) "5.50"
        string(4) "5.51"
        string(4) "5.52"
        string(4) "5.53"
        string(4) "5.54"
        string(4) "5.55"
        string(4) "5.56"
        string(4) "5.57"
        string(4) "5.58"
        string(4) "5.59"
        string(4) "5.60"
        string(4) "5.61"
        string(4) "5.62"
        string(4) "5.63"
        string(4) "5.64"
        string(4) "5.65"
        string(4) "5.66"
        string(4) "5.67"
        string(4) "5.68"
        string(4) "5.69"
        string(4) "5.70"
        string(4) "5.71"
        string(4) "5.72"
        string(4) "5.73"
        string(4) "5.74"
        string(4) "5.75"
        string(4) "5.76"
        string(4) "5.77"
        string(4) "5.78"
        string(4) "5.79"
        string(4) "5.80"
        string(4) "5.81"
        string(4) "5.82"
        string(4) "5.83"
        string(4) "5.84"
        string(4) "5.85"
        string(4) "5.86"
        string(4) "5.87"
        string(4) "5.88"
        string(4) "5.89"
        string(4) "5.90"
        string(4) "5.91"
        string(4) "5.92"
        string(4) "5.93"
        string(4) "5.94"
        string(4) "5.95"
        string(4) "5.96"
        string(4) "5.97"
        string(4) "5.98"
        string(4) "5.99"
        string(4) "6.00"
        string(4) "6.01"
        string(4) "6.02"
        string(4) "6.03"
        string(4) "6.04"
        string(4) "6.05"
        string(4) "6.06"
        string(4) "6.07"
        string(4) "6.08"
        string(4) "6.09"
        string(4) "6.10"
        string(4) "6.11"
        string(4) "6.12"
        string(4) "6.13"
        string(4) "6.14"
        string(4) "6.15"
        string(4) "6.16"
        string(4) "6.17"
        string(4) "6.18"
        string(4) "6.19"
        string(4) "6.20"
        string(4) "6.21"
        string(4) "6.22"
        string(4) "6.23"
        string(4) "6.24"
        string(4) "6.25"
        string(4) "6.26"
        string(4) "6.27"
        string(4) "6.28"
        string(4) "6.29"
        string(4) "6.30"
        string(4) "6.31"
        string(4) "6.32"
        string(4) "6.33"
        string(4) "6.34"
        string(4) "6.35"
        string(4) "6.36"
        string(4) "6.37"
        string(4) "6.38"
        string(4) "6.39"
        string(4) "6.40"
        string(4) "6.41"
        string(4) "6.42"
        string(4) "6.43"
        string(4) "6.44"
        string(4) "6.45"
        string(4) "6.46"
        string(4) "6.47"
        string(4) "6.48"
        string(4) "6.49"
        string(4) "6.50"
        string(4) "6.51"
        string(4) "6.52"
        string(4) "6.53"
        string(4) "6.54"
        string(4) "6.55"
        string(4) "6.56"
        string(4) "6.57"
        string(4) "6.58"
        string(4) "6.59"
        string(4) "6.60"
        string(4) "6.61"
        string(4) "6.62"
        string(4) "6.63"
        string(4) "6.64"
        string(4) "6.65"
        string(4) "6.66"
        string(4) "6.67"
        string(4) "6.68"
        string(4) "6.69"
        string(4) "6.70"
        string(4) "6.71"
        string(4) "6.72"
        string(4) "6.73"
        string(4) "6.74"
        string(4) "6.75"
        string(4) "6.76"
        string(4) "6.77"
        string(4) "6.78"
        string(4) "6.79"
        string(4) "6.80"
        string(4) "6.81"
        string(4) "6.82"
        string(4) "6.83"
        string(4) "6.84"
        string(4) "6.85"
        string(4) "6.86"
        string(4) "6.87"
        string(4) "6.88"
        string(4) "6.89"
        string(4) "6.90"
        string(4) "6.91"
        string(4) "6.92"
        string(4) "6.93"
        string(4) "6.94"
        string(4) "6.95"
        string(4) "6.96"
        string(4) "6.97"
        string(4) "6.98"
        string(4) "6.99"
        string(4) "7.00"
        string(4) "7.01"
        string(4) "7.02"
        string(4) "7.03"
        string(4) "7.04"
        string(4) "7.05"
        string(4) "7.06"
        string(4) "7.07"
        string(4) "7.08"
        string(4) "7.09"
        string(4) "7.10"
        string(4) "7.11"
        string(4) "7.12"
        string(4) "7.13"
        string(4) "7.14"
        string(4) "7.15"
        string(4) "7.16"
        string(4) "7.17"
        string(4) "7.18"
        string(4) "7.19"
        string(4) "7.20"
        string(4) "7.21"
        string(4) "7.22"
        string(4) "7.23"
        string(4) "7.24"
        string(4) "7.25"
        string(4) "7.26"
        string(4) "7.27"
        string(4) "7.28"
        string(4) "7.29"
        string(4) "7.30"
        string(4) "7.31"
        string(4) "7.32"
        string(4) "7.33"
        string(4) "7.34"
        string(4) "7.35"
        string(4) "7.36"
        string(4) "7.37"
        string(4) "7.38"
        string(4) "7.39"
        string(4) "7.40"
        string(4) "7.41"
        string(4) "7.42"
        string(4) "7.43"
        string(4) "7.44"
        string(4) "7.45"
        string(4) "7.46"
        string(4) "7.47"
        string(4) "7.48"
        string(4) "7.49"
        string(4) "7.50"
        string(4) "7.51"
        string(4) "7.52"
        string(4) "7.53"
        string(4) "7.54"
        string(4) "7.55"
        string(4) "7.56"
        string(4) "7.57"
        string(4) "7.58"
        string(4) "7.59"
        string(4) "7.60"
        string(4) "7.61"
        string(4) "7.62"
        string(4) "7.63"
        string(4) "7.64"
        string(4) "7.65"
        string(4) "7.66"
        string(4) "7.67"
        string(4) "7.68"
        string(4) "7.69"
        string(4) "7.70"
        string(4) "7.71"
        string(4) "7.72"
        string(4) "7.73"
        string(4) "7.74"
        string(4) "7.75"
        string(4) "7.76"
        string(4) "7.77"
        string(4) "7.78"
        string(4) "7.79"
        string(4) "7.80"
        string(4) "7.81"
        string(4) "7.82"
        string(4) "7.83"
        string(4) "7.84"
        string(4) "7.85"
        string(4) "7.86"
        string(4) "7.87"
        string(4) "7.88"
        string(4) "7.89"
        string(4) "7.90"
        string(4) "7.91"
        string(4) "7.92"
        string(4) "7.93"
        string(4) "7.94"
        string(4) "7.95"
        string(4) "7.96"
        string(4) "7.97"
        string(4) "7.98"
        string(4) "7.99"
        string(4) "8.00"
        string(4) "8.01"
        string(4) "8.02"
        string(4) "8.03"
        string(4) "8.04"
        string(4) "8.05"
        string(4) "8.06"
        string(4) "8.07"
        string(4) "8.08"
        string(4) "8.09"
        string(4) "8.10"
        string(4) "8.11"
        string(4) "8.12"
        string(4) "8.13"
        string(4) "8.14"
        string(4) "8.15"
        string(4) "8.16"
        string(4) "8.17"
        string(4) "8.18"
        string(4) "8.19"
        string(4) "8.20"
        string(4) "8.21"
        string(4) "8.22"
        string(4) "8.23"
        string(4) "8.24"
        string(4) "8.25"
        string(4) "8.26"
        string(4) "8.27"
        string(4) "8.28"
        string(4) "8.29"
        string(4) "8.30"
        string(4) "8.31"
        string(4) "8.32"
        string(4) "8.33"
        string(4) "8.34"
        string(4) "8.35"
        string(4) "8.36"
        string(4) "8.37"
        string(4) "8.38"
        string(4) "8.39"
        string(4) "8.40"
        string(4) "8.41"
        string(4) "8.42"
        string(4) "8.43"
        string(4) "8.44"
        string(4) "8.45"
        string(4) "8.46"
        string(4) "8.47"
        string(4) "8.48"
        string(4) "8.49"
        string(4) "8.50"
        string(4) "8.51"
        string(4) "8.52"
        string(4) "8.53"
        string(4) "8.54"
        string(4) "8.55"
        string(4) "8.56"
        string(4) "8.57"
        string(4) "8.58"
        string(4) "8.59"
        string(4) "8.60"
        string(4) "8.61"
        string(4) "8.62"
        string(4) "8.63"
        string(4) "8.64"
        string(4) "8.65"
        string(4) "8.66"
        string(4) "8.67"
        string(4) "8.68"
        string(4) "8.69"
        string(4) "8.70"
        string(4) "8.71"
        string(4) "8.72"
        string(4) "8.73"
        string(4) "8.74"
        string(4) "8.75"
        string(4) "8.76"
        string(4) "8.77"
        string(4) "8.78"
        string(4) "8.79"
        string(4) "8.80"
        string(4) "8.81"
        string(4) "8.82"
        string(4) "8.83"
        string(4) "8.84"
        string(4) "8.85"
        string(4) "8.86"
        string(4) "8.87"
        string(4) "8.88"
        string(4) "8.89"
        string(4) "8.90"
        string(4) "8.91"
        string(4) "8.92"
        string(4) "8.93"
        string(4) "8.94"
        string(4) "8.95"
        string(4) "8.96"
        string(4) "8.97"
        string(4) "8.98"
        string(4) "8.99"
        string(4) "9.00"
        string(4) "9.01"
        string(4) "9.02"
        string(4) "9.03"
        string(4) "9.04"
        string(4) "9.05"
        string(4) "9.06"
        string(4) "9.07"
        string(4) "9.08"
        string(4) "9.09"
        string(4) "9.10"
        string(4) "9.11"
        string(4) "9.12"
        string(4) "9.13"
        string(4) "9.14"
        string(4) "9.15"
        string(4) "9.16"
        string(4) "9.17"
        string(4) "9.18"
        string(4) "9.19"
        string(4) "9.20"
        string(4) "9.21"
        string(4) "9.22"
        string(4) "9.23"
        string(4) "9.24"
        string(4) "9.25"
        string(4) "9.26"
        string(4) "9.27"
        string(4) "9.28"
        string(4) "9.29"
        string(4) "9.30"
        string(4) "9.31"
        string(4) "9.32"
        string(4) "9.33"
        string(4) "9.34"
        string(4) "9.35"
        string(4) "9.36"
        string(4) "9.37"
        string(4) "9.38"
        string(4) "9.39"
        string(4) "9.40"
        string(4) "9.41"
        string(4) "9.42"
        string(4) "9.43"
        string(4) "9.44"
        string(4) "9.45"
        string(4) "9.46"
        string(4) "9.47"
        string(4) "9.48"
        string(4) "9.49"
        string(4) "9.50"
        string(4) "9.51"
        string(4) "9.52"
        string(4) "9.53"
        string(4) "9.54"
        string(4) "9.55"
        string(4) "9.56"
        string(4) "9.57"
        string(4) "9.58"
        string(4) "9.59"
        string(4) "9.60"
        string(4) "9.61"
        string(4) "9.62"
        string(4) "9.63"
        string(4) "9.64"
        string(4) "9.65"
        string(4) "9.66"
        string(4) "9.67"
        string(4) "9.68"
        string(4) "9.69"
        string(4) "9.70"
        string(4) "9.71"
        string(4) "9.72"
        string(4) "9.73"
        string(4) "9.74"
        string(4) "9.75"
        string(4) "9.76"
        string(4) "9.77"
        string(4) "9.78"
        string(4) "9.79"
        string(4) "9.80"
        string(4) "9.81"
        string(4) "9.82"
        string(4) "9.83"
        string(4) "9.84"
        string(4) "9.85"
        string(4) "9.86"
        string(4) "9.87"
        string(4) "9.88"
        string(4) "9.89"
        string(4) "9.90"
        string(4) "9.91"
        string(4) "9.92"
        string(4) "9.93"
        string(4) "9.94"
        string(4) "9.95"
        string(4) "9.96"
        string(4) "9.97"
        string(4) "9.98"
        string(4) "9.99"
        [URL="https://gitter.im/php-de/chat?utm_source=share-link&utm_medium=link&utm_campaign=share-link"]PHP.de Gitter.im Chat[/URL] - [URL="https://raindrop.io/user/32178"]Meine öffentlichen Bookmarks[/URL] ← Ich habe dir geholfen ? [B][URL="https://www.amazon.de/gp/wishlist/348FHGUZWTNL0"]Beschenk mich[/URL][/B].

        Kommentar


        • #5
          damit wäre das wohl geklärt Das mit der bitweisen Präzision wusste ich nicht. round() reicht für mich aber erst mal völlig aus
          Danke

          Kommentar


          • #6
            Nimm nicht round() vor der Ausgabe, sondern sprintf(). Damit hast du neben der Rundung zugleich dein gewünschtes Format. z.B.
            PHP-Code:
            for($i=0;$i<10;$i+=0.01)
            {
                echo 
            sprintf('%0.2f',$i).'</br>'

            PHP-Klassen auf github

            Kommentar


            • #7
              Was ja prinzipiell das ist was bcmath schon beim erzeugen der Zahlen tut.. nur das nicht jedesmal ein string nach einem benutzer-definierten format gescannt wird..
              [URL="https://gitter.im/php-de/chat?utm_source=share-link&utm_medium=link&utm_campaign=share-link"]PHP.de Gitter.im Chat[/URL] - [URL="https://raindrop.io/user/32178"]Meine öffentlichen Bookmarks[/URL] ← Ich habe dir geholfen ? [B][URL="https://www.amazon.de/gp/wishlist/348FHGUZWTNL0"]Beschenk mich[/URL][/B].

              Kommentar


              • #8
                PHP-Code:
                $precision 3;

                $factor pow (10,$precision);

                for(
                $i=17$factor;$i<22 $factor;$i+=1)
                {
                    
                printf ("%1." $precision "f\n" $i/$factor); 
                   
                //+ noch anderer Code

                PHP-Code:
                abstract class PrecisionLoop {
                  public function 
                __construct ($iPrecision) {
                    
                $this->iPrecision $iPrecision;
                  }

                  public function 
                run ($iStart $iEnd $iIncrement 1) {
                    if (
                $iIncrement == 0) return;
                    if (
                $iStart >= $iEnd && $iIncrement 0) return;
                    
                    
                $iFactor pow (10 $this->iPrecision);
                    
                    
                $iStart  $iStart $iFactor;
                    
                $iEnd    $iEnd   $iFactor;

                    for (
                $i $iStart $i $iEnd $i+=$iIncrement) {
                      
                $this->loopCode ($i/$iFactor);
                    }  
                  }
                }

                class 
                Example extends PrecisionLoop {

                  public function 
                __construct ($iPrecision) {
                    
                $this->sFormat "%1." $iPrecision "f\n";
                    
                parent::__construct ($iPrecision);
                  }

                  protected function 
                loopCode ($i) {
                    
                printf ($this->sFormat $i); 
                  }
                }

                $l = new Example (3);
                $l->run (17 22); 
                --

                „Emoticons machen einen Beitrag etwas freundlicher. Deine wirken zwar fachlich richtig sein, aber meist ziemlich uninteressant.
                Wenn man nur Text sieht, haben viele junge Entwickler keine interesse, diese stumpfen Texte zu lesen.“


                --

                Kommentar

                Lädt...
                X