php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43487 [PATCH]?Wrong conversion of float to string
Submitted: 2007-12-03 16:53 UTC Modified: 2008-07-27 12:01 UTC
Votes:5
Avg. Score:4.8 ± 0.4
Reproduced:4 of 4 (100.0%)
Same Version:3 (75.0%)
Same OS:4 (100.0%)
From: jm at wo dot cz Assigned: dmitry (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.2.6 OS: Linux
Private report: No CVE-ID: None
 [2007-12-03 16:53 UTC] jm at wo dot cz
Description:
------------
Some float numbers are incorrectly converted to string values:

I found it by playing with eval and getting wrong results. Then I did:

echo floatval('3.86e-6');    // output: 3.86E-6
echo floatval('1.01e-6');    // output: 1.01E-6

But

echo floatval('1E-6');       // output: :.0E-7

But one can do:

$f=1E-6;
echo $f;                      // output: .0E-7

So it must be a problem with float to string conversion.

Reproduce code:
---------------
$f=1E-6;
echo $f;                      // output: :.0E-7


Expected result:
----------------
1.0E-6

Actual result:
--------------
:.0E-7

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-12-03 19:17 UTC] jm at wo dot cz
Interesting that you can't reproduce it, I did not expect that, otherwise I would have provided the build info. Nevertheless, I have just recompiled php and the bug is still there.

The glibc is 2.3.6 and gcc is 4.2.1.

Here is the build set up:

 PHP_CONFIGURE="\
  --prefix=/usr                 \
  --sysconfdir=/etc             \
  --with-config-file-path=/etc  \
  --disable-static              \
  --disable-debug               \
  --disable-rpath               \
  --enable-inline-optimization  \
  --enable-pic                  \
  --enable-bcmath               \
  --enable-calendar             \
  --enable-magic-quotes         \
  --enable-track-vars           \
  --enable-safe-mode            \
  --enable-sysvsem              \
  --enable-sysvshm              \
  --enable-force-cgi-redirect   \
  --enable-trans-sid            \
  --enable-yp                   \
  --enable-ftp                  \
  --enable-wddx                 \
  --enable-filepro              \
  --enable-dbase                \
  --with-versioning             \
  --with-gettext                \
  --with-mod_charset            \
  --with-layout=GNU             \
  --with-zlib                   \
  --with-bz2                    \
  --enable-exif                 \
  --with-regex=system           \
  --enable-mbstring=all         \
  --enable-mbregex              \
  --with-pspell  \
            --with-mhash \
             --with-mcrypt \
             --with-curl  \
             --with-gmp   \
             --with-openssl \
             --with-pgsql=/usr     \
             --with-gdbm   \
             --with-xml --with-expat-dir=/usr   \
             --with-xsl    \
             --with-apxs2     \
  --with-t1lib=/usr/share/t1lib   \
  --with-freetype-dir=/usr/lib    \
  --with-gd "

 ./configure  $PHP_CONFIGURE
 [2007-12-04 12:33 UTC] jani@php.net
I can't reproduce either:

]# php -r '$f=1E-6; echo $f, "\n";'
1.0E-6

And pretty much the same configure options.
btw. Many of the options you used don't even exist, did you pay attention to the last lines of the output after running configure?
(it warns you about unknown options)

Try this yourself:
# php -n -r '$f=1E-6; echo $f, "\n";'

(-n makes sure you don't use any php.ini which might have different precision set)
 [2007-12-06 23:55 UTC] jm at wo dot cz
I got the same problem on another machine too, again PHP 5.2.5 with 
pretty much the same configuration. In fact, the problem exhibits 
also for version 5.2.4 on my machines.

About the unknown configuration options: Thanks, I had seen the 
warning but did not have the time to edit my scripts and based on 
the message it seemed harmless to leave the options be for now.

I am thinking that the problem and your inability to reproduce it 
could be due to some libraries that PHP uses that may be buggy in 
the versions on my systems. Can you please tell me what libraries, 
if any, to look for? Although the installed version of software are 
generally the latest stable releases, it is possible that a patch or 
a downgrade may be in order on some library/tool.

Finally, the problem has so far appeared for 3 numbers only: 
$ php -n -r '$f=array(1E-4, 1E-5, 1E-6, 1E-7, 1E-8); foreach ($f as 
$fval) echo $fval, "\n";'
0.0001
1.0E-5
:.0E-7
:.0E-8
1.0E-8


Jan
 [2007-12-06 23:59 UTC] jm at wo dot cz
Obviously, I meant to say, the problem is with TWO numbers only: 
1E-6 and 1E-7.

J.
 [2008-01-16 09:20 UTC] lmalgras at tennaxia dot com
I have the same problem with PHP 5.2.4 on Linux

Reproduce code:
---------------
php -n -r '$f=array(1E-4, 1E-5, 1E-6, 1E-7, 1E-8); foreach($f as $fval) echo $fval, "\n";'

Expected result:
----------------
0.0001
1.0E-5
1.0E-6
1.0E-7
1.0E-8

Actual result:
--------------
0.0001
1.0E-5
:.0E-7
:.0E-8
1.0E-8

I have test this code on several configurations with the following results :
PHP 5.2.0 (Linux)  : Actual result
PHP 5.2.2 (Linux)  : Actual result
PHP 5.2.3 (Windows): Actual result
 [2008-01-28 07:59 UTC] oeriksson at mandriva dot com
I get the same problem, but it seems related to what CPU is used.

http://qa.mandriva.com/show_bug.cgi?id=37171


$ uname -a
Linux foo.nux.se 2.6.24-server-0.rc8.2mdv #1 SMP Wed Jan 23 17:15:33 UTC 2008
i686 Intel(R) Pentium(R) 4 CPU 2.80GHz GNU/Linux

$ php -r "var_dump(11.1/111);"
float(0.0:)


$ uname -a
Linux oe.nux.tld 2.6.24-desktop-0.rc8.2mdv #1 SMP Wed Jan 23 18:12:45 CET 2008
x86_64 AMD Athlon(tm) 64 X2 Dual Core Processor 3800+ GNU/Linux

$ php -r "var_dump(11.1/111);"
float(0.1)


It works with 5.1.6 though.
 [2008-01-28 21:30 UTC] jm at wo dot cz
The CVS snapshot makes no difference, still getting the same 
incorrect strings.

[jm@st5mag ~]$ php -n -r '$f=array(1E-4, 1E-5, 1E-6, 1E-7, 1E-8); 
foreach ($f as $fval) echo $fval, "\n";'
0.0001
1.0E-5
:.0E-7
:.0E-8
1.0E-8
[jm@st5mag ~]$ php -v
PHP 5.2.6-dev (cli) (built: Jan 28 2008 16:13:46) 
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies

Previous poster suggested a CPU-related issue. I have encountered 
this issue on the following CPUs:

vendor_id	: AuthenticAMD
cpu family	: 15
model		: 5
model name	: AMD Opteron(tm) Processor 248
stepping	: 8
cpu MHz		: 2205.074

vendor_id	: GenuineIntel
cpu family	: 6
model		: 13
model name	: Intel(R) Pentium(R) M processor 1.80GHz
stepping	: 6
cpu MHz		: 1800.000

vendor_id	: AuthenticAMD
cpu family	: 15
model		: 37
model name	: AMD Opteron(tm) Processor 252
stepping	: 1
cpu MHz		: 2606.078
 [2008-01-30 19:41 UTC] oeriksson at mandriva dot com
I think I found the problem. On Mandriva Linux Cooker we are using:

gcc (GCC) 4.2.2 20071128 (prerelease) (4.2.2-2mdv2008.1)
glibc-2.7-1mdv2008.1

If I change the optimization from -O2 to -O0 (-O+zero) the bug goes away on x86_32.
 [2008-01-31 15:42 UTC] oeriksson at mandriva dot com
Update; taken from our bugzilla:

"as peroyvind found, the miscompilation is inside Zend/zend_strtod.c,
and -no-ftree-vrp workaround the bug."
 [2008-02-01 12:07 UTC] oeriksson at mandriva dot com
Pascal Rigaux at Mandriva has a patch for this, please review it.

http://svn.mandriva.com/cgi-bin/viewvc.cgi/packages/cooker/php/current/SOURCES/php-5.2.5-use-volatile-to-force-float-store.patch?revision=161099&view=markup
 [2008-02-11 17:49 UTC] oeriksson at mandriva dot com
Here's a shorter URL that hopefully won't wrap:

http://n1.nux.se/php-5.2.5-use-volatile-to-force-float-store.patch
 [2008-02-14 23:31 UTC] jani@php.net
Dmitry, can you check this out? There's a patch too. :)
 [2008-02-15 08:54 UTC] tony2001@php.net
Not reproducible with GCC 4.3.0 & 4.3.2 with -O3 on Linux 64bit.
 [2008-07-27 12:01 UTC] dmitry@php.net
The proposed patch has been comitted into CVS HEAD, PHP_5_3 and PHP_5_2, however I was never able to reproduce the bug I cannot be sure that it is fixed.

Please verify it and reopen in case of failure.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Sep 19 12:03:36 2021 UTC