php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55368 ini_set('precision',16) is affecting a simple math result
Submitted: 2011-08-05 08:21 UTC Modified: 2011-08-08 09:21 UTC
From: ygautheron at absystech dot fr Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: 5.3.6 OS: Linux Gentoo (2.6.38-gentoo-r6)
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ygautheron at absystech dot fr
New email:
PHP Version: OS:

 

 [2011-08-05 08:21 UTC] ygautheron at absystech dot fr
Description:
------------
Hello,

The simple calculation does not result the same if we set the precision to 16. Maybe it's cpu architecture related ?

Precision lower than 16 :
(1.196 - 1) * 100 => 19.6
Precision 16 :
(1.196 - 1) * 100 => 19.59999999999999
Precision 17 :
(1.196 - 1) * 100 => 19.599999999999994
...

Here's the configure parameters :
'./configure' '--prefix=/usr' '--build=x86_64-pc-linux-gnu' '--host=x86_64-pc-linux-gnu' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--datadir=/usr/share' '--sysconfdir=/etc' '--localstatedir=/var/lib' '--prefix=/usr/lib64/php5.3' '--mandir=/usr/lib64/php5.3/man' '--infodir=/usr/lib64/php5.3/info' '--libdir=/usr/lib64/php5.3/lib' '--with-libdir=lib64' '--without-pear' '--disable-maintainer-zts' '--enable-bcmath' '--with-bz2' '--enable-calendar' '--with-curl' '--with-curlwrappers' '--without-enchant' '--enable-exif' '--enable-ftp' '--with-gettext' '--without-gmp' '--without-mhash' '--disable-intl' '--without-kerberos' '--enable-mbstring' '--with-mcrypt' '--without-mssql' '--with-onig=/usr' '--with-openssl' '--with-openssl-dir=/usr' '--disable-pcntl' '--without-pgsql' '--without-pspell' '--without-recode' '--disable-shmop' '--with-snmp' '--disable-soap' '--enable-sockets' '--without-sqlite3' '--without-sybase-ct' '--disable-sysvmsg' '--disable-sysvsem' '--disable-sysvshm' '--with-tidy' '--disable-wddx' '--with-xmlrpc' '--with-xsl' '--enable-zip' '--with-zlib' '--disable-debug' '--enable-dba' '--without-cdb' '--with-db4' '--disable-flatfile' '--with-gdbm' '--disable-inifile' '--without-qdbm' '--with-freetype-dir=/usr' '--with-t1lib=/usr' '--disable-gd-jis-conv' '--with-jpeg-dir=/usr' '--with-png-dir=/usr' '--without-xpm-dir' '--with-gd' '--with-imap' '--with-imap-ssl' '--with-ldap' '--without-ldap-sasl' '--with-mysql=/usr' '--with-mysql-sock=/var/run/mysqld/mysqld.sock' '--with-mysqli=/usr/bin/mysql_config' '--with-unixODBC=/usr' '--without-adabas' '--without-birdstep' '--without-dbmaker' '--without-empress' '--without-esoob' '--without-ibm-db2' '--without-sapdb' '--without-solid' '--with-iodbc=/usr' '--without-oci8' '--with-oci8=instantclient,/usr/lib/oracle/10.2.0.3/client/lib' '--without-pdo-dblib' '--with-pdo-mysql=/usr' '--without-pdo-pgsql' '--without-pdo-sqlite' '--with-pdo-odbc=unixODBC,/usr' '--with-pdo-oci=instantclient,/usr,10.2.0.3' '--with-readline' '--without-libedit' '--without-mm' '--without-sqlite' '--with-pic' '--with-pcre-regex=/usr' '--with-pcre-dir=/usr' '--with-config-file-path=/etc/php/apache2-php5.3' '--with-config-file-scan-dir=/etc/php/apache2-php5.3/ext-active' '--disable-embed' '--disable-cli' '--disable-cgi' '--disable-fpm' '--with-apxs2=/usr/sbin/apxs' 

Test script:
---------------
Interactive shell

php > var_dump(phpversion());
string(16) "5.3.6-pl0-gentoo"
php > var_dump((1.196-1)*100);
float(19.6)
php > var_dump(ini_get('precision'));
string(2) "14"
php > var_dump(ini_set('precision',14));
string(2) "14"
php > var_dump((1.196-1)*100);
float(19.6)
php > var_dump(ini_set('precision',15));
string(2) "14"
php > var_dump((1.196-1)*100);
float(19.6)
php > var_dump(ini_set('precision',16));
string(2) "15"
php > var_dump((1.196-1)*100);
float(19.59999999999999)
php > var_dump(ini_set('precision',17));
string(2) "16"
php > var_dump((1.196-1)*100);
float(19.599999999999994)
php > var_dump(ini_set('precision',18));
string(2) "17"
php > var_dump((1.196-1)*100);
float(19.5999999999999943)


Expected result:
----------------
I think the result must always be the same : (float)19.6

Actual result:
--------------
php > var_dump(ini_set('precision',16));
string(2) "15"
php > var_dump(("1.196"-1)*100);
float(19.59999999999999)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-08-05 08:29 UTC] ygautheron at absystech dot fr
Just noticed that i we try 0.196*100 it works good too.
The problem seems to come from the substract ?
 [2011-08-05 08:40 UTC] jeremie dot gw at gmail dot com
I don't think it is a php bug. I have the same result with C code. 

Source :
--------
#include "stdio.h"
int main(){
        double a;
        a=(1.196-1)*100;
        printf("Result=%.16f\n",a);
        return 0;
}

Result :
--------
19.5999999999999943

Architecture :
--------------
Ubuntu 10.04 x86
Linux 2.6.32-33-generic #71-Ubuntu SMP Wed Jul 20 17:30:40 UTC 2011 i686 
GNU/Linux
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
 [2011-08-05 08:47 UTC] ygautheron at absystech dot fr
Ok, well i assume the only patch is 
var_dump(round(1.196-1,3)*100);

I think it's buggy, but maybe there is no C solution to add in the PHP source ?
 [2011-08-08 09:21 UTC] cataphract@php.net
-Status: Open +Status: Bogus
 [2011-08-08 09:21 UTC] cataphract@php.net
Floating point values have a limited precision. Hence a value might 
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly 
printing it without any mathematical operations.

If you would like to know more about "floats" and what IEEE
754 is, read this:
http://www.floating-point-gui.de/

Thank you for your interest in PHP.

Expected.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 14 16:01:26 2024 UTC