Debian Bug report logs - #603292
libapache2-mod-php5: PHP5 does not round correctly

version graph

Package: libapache2-mod-php5; Maintainer for libapache2-mod-php5 is (unknown);

Reported by: Markus Knaup <mk@digitaleprodukte.de>

Date: Fri, 12 Nov 2010 16:09:05 UTC

Severity: minor

Tags: upstream, wontfix

Found in version php5/5.2.6.dfsg.1-1+lenny9

Done: Ondřej Surý <ondrej@debian.org>

Bug is archived. No further changes may be made.

Toggle useless messages

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to debian-bugs-dist@lists.debian.org, Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>:
Bug#603292; Package libapache2-mod-php5. (Fri, 12 Nov 2010 16:09:08 GMT) (full text, mbox, link).


Acknowledgement sent to Markus Knaup <mk@digitaleprodukte.de>:
New Bug report received and forwarded. Copy sent to Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>. (Fri, 12 Nov 2010 16:09:08 GMT) (full text, mbox, link).


Message #5 received at submit@bugs.debian.org (full text, mbox, reply):

From: Markus Knaup <mk@digitaleprodukte.de>
To: Debian Bug Tracking System <submit@bugs.debian.org>
Subject: libapache2-mod-php5: PHP5 does not round correctly
Date: Fri, 12 Nov 2010 17:01:11 +0100
Package: libapache2-mod-php5
Version: 5.2.6.dfsg.1-1+lenny9
Severity: important

PHP5 does not round correctly.

Example code:
<?php
echo round(1.025, 2) . "<br />";
echo round(1.125, 2) . "<br />";
echo round(1.225, 2) . "<br />";
echo number_format(1.025, 2) . "<br />";
echo number_format(1.125, 2) . "<br />";
echo number_format(1.225, 2) . "<br />";
?>

This prints:
1.02
1.13
1.23
1.02
1.13
1.23

The first and fourth value should be 1.03 and not 1.02.


-- System Information:
Debian Release: 5.0.6
  APT prefers stable
  APT policy: (500, 'stable')
Architecture: i386 (i686)

Kernel: Linux 2.6.28.2 (SMP w/2 CPU cores)
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/bash

Versions of packages libapache2-mod-php5 depends on:
ii  apache2-mpm-pre 2.2.9-10+lenny8          Apache HTTP Server - traditional n
ii  apache2.2-commo 2.2.9-10+lenny8          Apache HTTP Server common files
ii  libbz2-1.0      1.0.5-1+lenny1           high-quality block-sorting file co
ii  libc6           2.7-18lenny6             GNU C Library: Shared libraries
ii  libcomerr2      1.41.3-1                 common error description library
ii  libdb4.6        4.6.21-11                Berkeley v4.6 Database Libraries [
ii  libkrb53        1.6.dfsg.4~beta1-5lenny4 MIT Kerberos runtime libraries
ii  libmagic1       4.26-1                   File type determination library us
ii  libpcre3        7.6-2.1                  Perl 5 Compatible Regular Expressi
ii  libssl0.9.8     0.9.8g-15+lenny8         SSL shared libraries
ii  libxml2         2.6.32.dfsg-5+lenny1     GNOME XML library
ii  mime-support    3.44-1                   MIME files 'mime.types' & 'mailcap
ii  php5-common     5.2.6.dfsg.1-1+lenny9    Common files for packages built fr
ii  tzdata          2010j-0lenny1            time zone and daylight-saving time
ii  ucf             3.0016                   Update Configuration File: preserv
ii  zlib1g          1:1.2.3.3.dfsg-12        compression library - runtime

libapache2-mod-php5 recommends no packages.

Versions of packages libapache2-mod-php5 suggests:
ii  php-pear           5.2.6.dfsg.1-1+lenny9 PEAR - PHP Extension and Applicati

-- no debconf information




Reply sent to Ondřej Surý <ondrej@debian.org>:
You have taken responsibility. (Sat, 13 Nov 2010 09:39:12 GMT) (full text, mbox, link).


Notification sent to Markus Knaup <mk@digitaleprodukte.de>:
Bug acknowledged by developer. (Sat, 13 Nov 2010 09:39:12 GMT) (full text, mbox, link).


Message #10 received at 603292-done@bugs.debian.org (full text, mbox, reply):

From: Ondřej Surý <ondrej@debian.org>
To: Markus Knaup <mk@digitaleprodukte.de>, 603292-done@bugs.debian.org
Subject: Re: [php-maint] Bug#603292: libapache2-mod-php5: PHP5 does not round correctly
Date: Sat, 13 Nov 2010 10:37:15 +0100
This is not a bug, this is just the way the floating numbers are
handled in PHP (and in fact the math library, gives the same result.)

Please read http://wiki.php.net/rfc/rounding, especially:

"""
PHP offers a method round to round floating point numbers to a certain
precision. This, however, does not always work as a user would expect.
This is due to the fact that IEEE 754 floating point values are stored
in the binary system which cannot represent every decimal value
exactly.
"""

Also http://www.gnu.org/software/libc/manual/html_mono/libc.html#Rounding
is a good reading on C99 rounding.

Ondrej.

On Fri, Nov 12, 2010 at 17:01, Markus Knaup <mk@digitaleprodukte.de> wrote:
> Package: libapache2-mod-php5
> Version: 5.2.6.dfsg.1-1+lenny9
> Severity: important
>
> PHP5 does not round correctly.
>
> Example code:
> <?php
> echo round(1.025, 2) . "<br />";
> echo round(1.125, 2) . "<br />";
> echo round(1.225, 2) . "<br />";
> echo number_format(1.025, 2) . "<br />";
> echo number_format(1.125, 2) . "<br />";
> echo number_format(1.225, 2) . "<br />";
> ?>
>
> This prints:
> 1.02
> 1.13
> 1.23
> 1.02
> 1.13
> 1.23
>
> The first and fourth value should be 1.03 and not 1.02.
>
>
> -- System Information:
> Debian Release: 5.0.6
>  APT prefers stable
>  APT policy: (500, 'stable')
> Architecture: i386 (i686)
>
> Kernel: Linux 2.6.28.2 (SMP w/2 CPU cores)
> Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)
> Shell: /bin/sh linked to /bin/bash
>
> Versions of packages libapache2-mod-php5 depends on:
> ii  apache2-mpm-pre 2.2.9-10+lenny8          Apache HTTP Server - traditional n
> ii  apache2.2-commo 2.2.9-10+lenny8          Apache HTTP Server common files
> ii  libbz2-1.0      1.0.5-1+lenny1           high-quality block-sorting file co
> ii  libc6           2.7-18lenny6             GNU C Library: Shared libraries
> ii  libcomerr2      1.41.3-1                 common error description library
> ii  libdb4.6        4.6.21-11                Berkeley v4.6 Database Libraries [
> ii  libkrb53        1.6.dfsg.4~beta1-5lenny4 MIT Kerberos runtime libraries
> ii  libmagic1       4.26-1                   File type determination library us
> ii  libpcre3        7.6-2.1                  Perl 5 Compatible Regular Expressi
> ii  libssl0.9.8     0.9.8g-15+lenny8         SSL shared libraries
> ii  libxml2         2.6.32.dfsg-5+lenny1     GNOME XML library
> ii  mime-support    3.44-1                   MIME files 'mime.types' & 'mailcap
> ii  php5-common     5.2.6.dfsg.1-1+lenny9    Common files for packages built fr
> ii  tzdata          2010j-0lenny1            time zone and daylight-saving time
> ii  ucf             3.0016                   Update Configuration File: preserv
> ii  zlib1g          1:1.2.3.3.dfsg-12        compression library - runtime
>
> libapache2-mod-php5 recommends no packages.
>
> Versions of packages libapache2-mod-php5 suggests:
> ii  php-pear           5.2.6.dfsg.1-1+lenny9 PEAR - PHP Extension and Applicati
>
> -- no debconf information
>
>
>
> _______________________________________________
> pkg-php-maint mailing list
> pkg-php-maint@lists.alioth.debian.org
> http://lists.alioth.debian.org/mailman/listinfo/pkg-php-maint
>



-- 
Ondřej Surý <ondrej@sury.org>
http://blog.rfc1925.org/




Information forwarded to debian-bugs-dist@lists.debian.org, Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>:
Bug#603292; Package libapache2-mod-php5. (Mon, 15 Nov 2010 05:21:03 GMT) (full text, mbox, link).


Acknowledgement sent to Bob Proulx <bob@proulx.com>:
Extra info received and forwarded to list. Copy sent to Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>. (Mon, 15 Nov 2010 05:21:03 GMT) (full text, mbox, link).


Message #15 received at 603292@bugs.debian.org (full text, mbox, reply):

From: Bob Proulx <bob@proulx.com>
To: Markus Knaup <mk@digitaleprodukte.de>, 603292@bugs.debian.org
Subject: Re: Bug#603292: libapache2-mod-php5: PHP5 does not round correctly
Date: Sun, 14 Nov 2010 22:13:38 -0700
Markus Knaup wrote:
> PHP5 does not round correctly.
> echo round(1.025, 2) . "<br />";

Note that this is the same behavior as C's printf.  Also the shell's
printf which makes that behavior available to the command line
produces the same behavior.

  $ printf "%.2f\n" 1.025
  1.02

But it just didn't seem satisfying to say that it isn't a bug in PHP
because that is just the way floating point computer engines work.
But that is basically the reason.  That is the way floating point
works on machines.  But being unsatisfying I wanted to add some more
information about the problem.

For the underlying reasons you need to look at the encoded binary
values.  A typical issue is that exact terminating decimal fractions
may not have an exact binary representation.  Therefore converting
from decimal to binary creates an error.

  1.020 => 1.00000101000111101011100001010001111010111000010100011110101110...
  1.025 => 1.00000110011001100110011001100110011001100110011001100110011001...
  1.030 => 1.00000111101011100001010001111010111000010100011110101110000101...

Since there are an infinite number of digits needed this can't be used
without reducing the number of digits in the calculation.  That means
that floating point operations routinely introduce errors into
calculations.  As to the exact details of rounding fractional digits I
have always avoided needing to know that information.  I have avoided
it because when I needed an exact value I knew about the problems and
instead used scaled integers.  And so I can't give a better
explanation now.  Only a recommendation to avoid it too.

Remember that floating point is only an approximation of reality.  For
more exact answers you may want to use binary coded decimal or scaled
integers.  This example is a good one to show why floating point isn't
used for monetary transactions.  My personal history is with CAD/EDA
software and in that problem domain most software uses scaled integers
to avoid these types of errors.

There have been recent changes in the way PHP handles rounding.
The upstream PHP documentation lists:

  5.3.0  The mode parameter was introduced.

  5.2.7  The inner workings of round() was changed to conform to the
         C99 standard.

In 5.3.0 and later (Sid or Squeeze) the mode parameter gives you some
control over rounding.

  $ php -r 'echo round(1.025,2,PHP_ROUND_HALF_UP) . "\n";'
  1.03

Also the default rounding mode has changed too.

  $ php -r 'echo round(1.025,2) . "\n";'
  1.03

Hope that helps,
Bob

P.S. Here are some useful references.

  http://en.wikipedia.org/wiki/Double_precision_floating-point_format

  http://en.wikipedia.org/wiki/Floating_point#Rounding_modes

  http://en.wikipedia.org/wiki/Binary_numeral_system#Decimal

  http://en.wikipedia.org/wiki/Binary_numeral_system#Representing_real_numbers

  http://cs.furman.edu/digitaldomain/more/ch6/dec_frac_to_bin.htm

  http://www.exploringbinary.com/binary-converter/




Information forwarded to debian-bugs-dist@lists.debian.org, Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>:
Bug#603292; Package libapache2-mod-php5. (Mon, 15 Nov 2010 06:57:03 GMT) (full text, mbox, link).


Acknowledgement sent to Ondřej Surý <ondrej@debian.org>:
Extra info received and forwarded to list. Copy sent to Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>. (Mon, 15 Nov 2010 06:57:03 GMT) (full text, mbox, link).


Message #20 received at 603292@bugs.debian.org (full text, mbox, reply):

From: Ondřej Surý <ondrej@debian.org>
To: Bob Proulx <bob@proulx.com>, 603292@bugs.debian.org
Subject: Re: [php-maint] Bug#603292: libapache2-mod-php5: PHP5 does not round correctly
Date: Mon, 15 Nov 2010 07:52:27 +0100
Bob,

thanks for comprehensive explanation. Would you be willing to put this
information to f.e. http://wiki.debian.org/PHP/Rounding and link it
from http://wiki.debian.org/PHP (or just in
http://wiki.debian.org/PHP/Rounding), so it doesn't disappear when
this bug is archived?

Ondrej

On Mon, Nov 15, 2010 at 06:13, Bob Proulx <bob@proulx.com> wrote:
> Markus Knaup wrote:
>> PHP5 does not round correctly.
>> echo round(1.025, 2) . "<br />";
>
> Note that this is the same behavior as C's printf.  Also the shell's
> printf which makes that behavior available to the command line
> produces the same behavior.
>
>  $ printf "%.2f\n" 1.025
>  1.02
>
> But it just didn't seem satisfying to say that it isn't a bug in PHP
> because that is just the way floating point computer engines work.
> But that is basically the reason.  That is the way floating point
> works on machines.  But being unsatisfying I wanted to add some more
> information about the problem.
>
> For the underlying reasons you need to look at the encoded binary
> values.  A typical issue is that exact terminating decimal fractions
> may not have an exact binary representation.  Therefore converting
> from decimal to binary creates an error.
>
>  1.020 => 1.00000101000111101011100001010001111010111000010100011110101110...
>  1.025 => 1.00000110011001100110011001100110011001100110011001100110011001...
>  1.030 => 1.00000111101011100001010001111010111000010100011110101110000101...
>
> Since there are an infinite number of digits needed this can't be used
> without reducing the number of digits in the calculation.  That means
> that floating point operations routinely introduce errors into
> calculations.  As to the exact details of rounding fractional digits I
> have always avoided needing to know that information.  I have avoided
> it because when I needed an exact value I knew about the problems and
> instead used scaled integers.  And so I can't give a better
> explanation now.  Only a recommendation to avoid it too.
>
> Remember that floating point is only an approximation of reality.  For
> more exact answers you may want to use binary coded decimal or scaled
> integers.  This example is a good one to show why floating point isn't
> used for monetary transactions.  My personal history is with CAD/EDA
> software and in that problem domain most software uses scaled integers
> to avoid these types of errors.
>
> There have been recent changes in the way PHP handles rounding.
> The upstream PHP documentation lists:
>
>  5.3.0  The mode parameter was introduced.
>
>  5.2.7  The inner workings of round() was changed to conform to the
>         C99 standard.
>
> In 5.3.0 and later (Sid or Squeeze) the mode parameter gives you some
> control over rounding.
>
>  $ php -r 'echo round(1.025,2,PHP_ROUND_HALF_UP) . "\n";'
>  1.03
>
> Also the default rounding mode has changed too.
>
>  $ php -r 'echo round(1.025,2) . "\n";'
>  1.03
>
> Hope that helps,
> Bob
>
> P.S. Here are some useful references.
>
>  http://en.wikipedia.org/wiki/Double_precision_floating-point_format
>
>  http://en.wikipedia.org/wiki/Floating_point#Rounding_modes
>
>  http://en.wikipedia.org/wiki/Binary_numeral_system#Decimal
>
>  http://en.wikipedia.org/wiki/Binary_numeral_system#Representing_real_numbers
>
>  http://cs.furman.edu/digitaldomain/more/ch6/dec_frac_to_bin.htm
>
>  http://www.exploringbinary.com/binary-converter/
>
>
>
> _______________________________________________
> pkg-php-maint mailing list
> pkg-php-maint@lists.alioth.debian.org
> http://lists.alioth.debian.org/mailman/listinfo/pkg-php-maint
>



-- 
Ondřej Surý <ondrej@sury.org>
http://blog.rfc1925.org/




Information forwarded to debian-bugs-dist@lists.debian.org, Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>:
Bug#603292; Package libapache2-mod-php5. (Tue, 07 Dec 2010 22:21:06 GMT) (full text, mbox, link).


Acknowledgement sent to Bob Proulx <bob@proulx.com>:
Extra info received and forwarded to list. Copy sent to Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>. (Tue, 07 Dec 2010 22:21:07 GMT) (full text, mbox, link).


Message #25 received at 603292@bugs.debian.org (full text, mbox, reply):

From: Bob Proulx <bob@proulx.com>
To: Ondřej Surý <ondrej@debian.org>, 603292@bugs.debian.org
Subject: Re: Bug#603292: [php-maint] Bug#603292: libapache2-mod-php5: PHP5 does not round correctly
Date: Tue, 7 Dec 2010 15:19:17 -0700
Ondřej Surý wrote:
> thanks for comprehensive explanation. Would you be willing to put this
> information to f.e. http://wiki.debian.org/PHP/Rounding and link it
> from http://wiki.debian.org/PHP (or just in
> http://wiki.debian.org/PHP/Rounding), so it doesn't disappear when
> this bug is archived?

Done.  I captured this discussion here in:

  http://wiki.debian.org/PHP/Rounding

And linked it to the main http://wiki.debian.org/PHP page.

Bob




Information forwarded to debian-bugs-dist@lists.debian.org, Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>:
Bug#603292; Package libapache2-mod-php5. (Wed, 08 Dec 2010 08:27:03 GMT) (full text, mbox, link).


Acknowledgement sent to Ondřej Surý <ondrej@debian.org>:
Extra info received and forwarded to list. Copy sent to Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>. (Wed, 08 Dec 2010 08:27:03 GMT) (full text, mbox, link).


Message #30 received at 603292@bugs.debian.org (full text, mbox, reply):

From: Ondřej Surý <ondrej@debian.org>
To: Bob Proulx <bob@proulx.com>
Cc: 603292@bugs.debian.org
Subject: Re: Bug#603292: [php-maint] Bug#603292: libapache2-mod-php5: PHP5 does not round correctly
Date: Wed, 8 Dec 2010 09:23:56 +0100
severity 603292 minor
tags 603292 +wontfix upstream
thank you

Bob, thank you very much. I am going to keep this bug open with
wontfix tag, so we avoid future duplicate bugreports about rounding.

Ondrej.

2010/12/7 Bob Proulx <bob@proulx.com>:
> Ondřej Surý wrote:
>> thanks for comprehensive explanation. Would you be willing to put this
>> information to f.e. http://wiki.debian.org/PHP/Rounding and link it
>> from http://wiki.debian.org/PHP (or just in
>> http://wiki.debian.org/PHP/Rounding), so it doesn't disappear when
>> this bug is archived?
>
> Done.  I captured this discussion here in:
>
>  http://wiki.debian.org/PHP/Rounding
>
> And linked it to the main http://wiki.debian.org/PHP page.
>
> Bob
>



-- 
Ondřej Surý <ondrej@sury.org>
http://blog.rfc1925.org/




Severity set to 'minor' from 'important' Request was from Ondřej Surý <ondrej@debian.org> to control@bugs.debian.org. (Wed, 08 Dec 2010 08:27:04 GMT) (full text, mbox, link).


Added tag(s) upstream and wontfix. Request was from Ondřej Surý <ondrej@debian.org> to control@bugs.debian.org. (Wed, 08 Dec 2010 08:27:05 GMT) (full text, mbox, link).


Bug archived. Request was from Debbugs Internal Request <owner@bugs.debian.org> to internal_control@bugs.debian.org. (Thu, 06 Jan 2011 07:35:07 GMT) (full text, mbox, link).


Send a report that this bug log contains spam.


Debian bug tracking system administrator <owner@bugs.debian.org>. Last modified: Sun Jul 2 01:52:19 2023; Machine Name: bembo

Debian Bug tracking system

Debbugs is free software and licensed under the terms of the GNU Public License version 2. The current version can be obtained from https://bugs.debian.org/debbugs-source/.

Copyright © 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson, 2005-2017 Don Armstrong, and many other contributors.