Can PHP run decimal loop step, such as 0.1, correctly?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • duzhidian@gmail.com

    Can PHP run decimal loop step, such as 0.1, correctly?

    I want to use the following code to print all value of alpha + beta _
    gamme = 1;

    But I miss some of the results. I suspect PHP cannot run decimal loop
    correctly. I should get 66 groups.



    $index = 0;
    for ($alpha = 0.0; $alpha <= 1.0; $alpha += 0.1) {
    for ($beta = 0.0; $beta <= 1.0; $beta += 0.1) {
    for ($gamma = 0.0; $gamma <= 1.0; $gamma += 0.1) {
    $temp = $alpha + $beta + $gamma;
    if ($temp != 1) {
    continue;
    }


    echo "\n<p>$inde x: $alpha $beta $gamma. "</p>";

    $index ++;
    }}}

    Output as follows:

    0: 0 0.2 0.8

    1: 0 0.3 0.7

    2: 0 0.4 0.6

    3: 0 0.5 0.5

    4: 0 0.6 0.4

    5: 0 0.7 0.3

    6: 0 0.8 0.2

    7: 0.1 0.1 0.8

    8: 0.1 0.2 0.7

    9: 0.1 0.3 0.6

    10: 0.1 0.4 0.5

    11: 0.1 0.5 0.4

    12: 0.1 0.6 0.3

    13: 0.1 0.7 0.2

    14: 0.2 0 0.8

    15: 0.2 0.1 0.7

    16: 0.2 0.2 0.6

    17: 0.2 0.3 0.5

    18: 0.2 0.4 0.4

    19: 0.2 0.5 0.3

    20: 0.2 0.6 0.2

    21: 0.2 0.8 0

    22: 0.3 0 0.7

    23: 0.3 0.1 0.6

    24: 0.3 0.2 0.5

    25: 0.3 0.3 0.4

    26: 0.3 0.4 0.3

    27: 0.3 0.5 0.2

    28: 0.3 0.6 0.1

    29: 0.3 0.7 0

    30: 0.4 0 0.6

    31: 0.4 0.1 0.5

    32: 0.4 0.2 0.4

    33: 0.4 0.3 0.3

    34: 0.4 0.4 0.2

    35: 0.4 0.5 0.1

    36: 0.4 0.6 0

    37: 0.5 0 0.5

    38: 0.5 0.1 0.4

    39: 0.5 0.2 0.3

    40: 0.5 0.3 0.2

    41: 0.5 0.4 0.1

    42: 0.5 0.5 0

    43: 0.6 0 0.4

    44: 0.6 0.1 0.3

    45: 0.6 0.2 0.2

    46: 0.6 0.3 0.1

    47: 0.6 0.4 0

    48: 0.7 0 0.3

    49: 0.7 0.1 0.2

    50: 0.7 0.3 0

    51: 0.8 0 0.2

    52: 0.8 0.2 0


  • rf

    #2
    Re: Can PHP run decimal loop step, such as 0.1, correctly?


    <duzhidian@gmai l.comwrote in message
    news:eb6f99d6-9a8b-469b-8099-33203769f0c3@o7 7g2000hsf.googl egroups.com...
    >I want to use the following code to print all value of alpha + beta _
    gamme = 1;
    >
    But I miss some of the results. I suspect PHP cannot run decimal loop
    correctly. I should get 66 groups.
    Floating point.

    Not all decimal fractions can be expressed exactly in base two floating
    point.

    Discussed here and elsewhere many times.

    --
    Richard.


    Comment

    • Rik Wasmus

      #3
      Re: Can PHP run decimal loop step, such as 0.1, correctly?

      On Fri, 22 Feb 2008 10:27:08 +0100, duzhidian@gmail .com
      <duzhidian@gmai l.comwrote:
      I want to use the following code to print all value of alpha + beta _
      gamme = 1;
      >
      But I miss some of the results. I suspect PHP cannot run decimal loop
      correctly. I should get 66 groups.
      >
      >
      >
      $index = 0;
      for ($alpha = 0.0; $alpha <= 1.0; $alpha += 0.1) {
      for ($beta = 0.0; $beta <= 1.0; $beta += 0.1) {
      for ($gamma = 0.0; $gamma <= 1.0; $gamma += 0.1) {
      $temp = $alpha + $beta + $gamma;
      if ($temp != 1) {
      An if ($temp != 1.0) would probably help to remove some quickyness.
      continue;
      }
      >
      >
      echo "\n<p>$inde x: $alpha $beta $gamma. "</p>";
      >
      $index ++;
      }}}
      When working with floating points, don't look for equality, but for within
      a range of error:
      (1 - 0.05) < $temp < (1 + 0.05)

      To avoid this, use integers where you can (I usually multply by powers of
      10 untill satisfied with the accuracy).
      --
      Rik Wasmus

      Comment

      Working...