Possible bug in round function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wescotte

    #1

    Possible bug in round function

    PHP 5.0.5 running on Win2k Server

    $rate = 0.015;
    $value = 15;

    echo $rate * $value;
    echo "<BR>";
    echo round($rate * $value , 2);

    output:

    0.225
    0.22

    Shouldn't this be?
    0.225
    0.23

  • Wescotte

    #2
    Re: Possible bug in round function

    Little update

    if I added echo "<BR>" echo round(.225, 2) the output is .23

    Comment

    • Andy Hassall

      #3
      Re: Possible bug in round function

      On 13 Feb 2006 12:29:18 -0800, "Wescotte" <wescotte@earth link.net> wrote:
      [color=blue]
      >PHP 5.0.5 running on Win2k Server
      >
      >$rate = 0.015;
      >$value = 15;
      >
      >echo $rate * $value;
      >echo "<BR>";
      >echo round($rate * $value , 2);
      >
      >output:
      >
      >0.225
      >0.22
      >
      >Shouldn't this be?
      >0.225
      >0.23[/color]

      On 5.1.2 it does output 0.23.

      Perhaps see http://bugs.php.net/bug.php?id=24142

      --
      Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
      http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

      Comment

      • Janwillem Borleffs

        #4
        Re: Possible bug in round function

        Andy Hassall wrote:[color=blue]
        > On 5.1.2 it does output 0.23.
        >[/color]

        Not on my WinXP installation, which produces 0.22

        One of the related bug reports (http://bugs.php.net/bug.php?id=35986)
        mentions that this isn't a bug, but due to the limited precision of floats.
        Perhaps it's processor related...


        JW


        Comment

        • Andy Hassall

          #5
          Re: Possible bug in round function

          On Mon, 13 Feb 2006 22:24:45 +0100, "Janwillem Borleffs" <jw@jwscripts.c om>
          wrote:
          [color=blue]
          >Andy Hassall wrote:[color=green]
          >> On 5.1.2 it does output 0.23.[/color]
          >
          >Not on my WinXP installation, which produces 0.22
          >
          >One of the related bug reports (http://bugs.php.net/bug.php?id=35986)
          >mentions that this isn't a bug, but due to the limited precision of floats.
          >Perhaps it's processor related...[/color]

          Hum, 0.23 on Linux, 0.22 on Windows 2000. Both are Intel chips. Looks like
          there's Windows-specific code in ext/standard/round.c :

          #ifndef PHP_ROUND_FUZZ
          # ifndef PHP_WIN32
          # define PHP_ROUND_FUZZ 0.50000000001
          # else
          # define PHP_ROUND_FUZZ 0.5
          # endif
          #endif

          #define PHP_ROUND_WITH_ FUZZ(val, places) { \
          double tmp_val=val, f = pow(10.0, (double) places); \
          tmp_val *= f; \
          if (tmp_val >= 0.0) { \
          tmp_val = floor(tmp_val + PHP_ROUND_FUZZ) ; \
          } else { \
          tmp_val = ceil(tmp_val - PHP_ROUND_FUZZ) ; \
          } \
          tmp_val /= f; \
          val = !zend_isnan(tmp _val) ? tmp_val : val; \
          } \


          Shame there's no comments explaining what this is there for.

          --
          Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
          http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

          Comment

          Working...