php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29547 Floatingpoint / locale behaviour strange (need to run script twice)
Submitted: 2004-08-06 14:42 UTC Modified: 2005-01-19 01:00 UTC
From: denbraber dot acbjl at avans dot nl Assigned:
Status: No Feedback Package: Math related
PHP Version: 4.3.8 OS: Solaris / nsapi
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: denbraber dot acbjl at avans dot nl
New email:
PHP Version: OS:

 

 [2004-08-06 14:42 UTC] denbraber dot acbjl at avans dot nl
Description:
------------
When I perform floatingpoint calculations I get really unexpected results.

The next three examples should all give 1 as result but they all give 0 as result.
echo "floatval \"0.5\"*2=".floatval((2*"0.5"))."<br>";
echo "floatval \"0,5\"*2=".floatval((2*"0,5"))."<br>";
echo "\"0.5\"*2=".(2*"0.5")."<br>";

I found that this could be locale related and so I tried adding just above the previous code:

$local=setlocale(LC_ALL,"nld_nld");
echo "<p>Country set to: \"nld_nld\": ".$local."<p>\n"; 

(or $local=setlocale(LC_NUMERIC,"C"); )

Now when I run the script for the first time after a server start up this will give:

Country set to: "nld_nld": Dutch_Netherlands.1252

floatval "0.5"*2=1
"0.5"*2=1
0.5*2=0

Still the last one is incorrect.

To make it even weirder, when I hit reload to run the script again it will give:

Country set to: "nld_nld": Dutch_Netherlands.1252

floatval "0.5"*2=1
"0.5"*2=1
0.5*2=1

and that is correct!! 

So every time after a server startup (SunOne webserver with php running as nsapi) I have to run a dummy script to change the locate in order for floatingpoint calculations to be performed correctly in scripts that are run after that.

In the past we used older versions of PHP (4.2.x) and those versions did not have this problem. We did run the older versions as CGIBIN under a Netscape Enterprise server but that is not possible anymore with the newer versions so I can't test if it is a result of running PHP as NSAPI now. 

Is this a know bug and is there a good workaround?







Reproduce code:
---------------
$local=setlocale(LC_ALL,"nld_nld");
echo "<p>Country set to: \"nld_nld\": ".$local."<p>\n";
echo "floatval \"0.5\"*2=".floatval(2*"0.5")."<br>";
echo "\"0.5\"*2=".(2*"0.5")."<br>";
echo "0.5*2=".(2*0.5)."<br>";

Expected result:
----------------
Country set to: "nld_nld": Dutch_Netherlands.1252

floatval "0.5"*2=1
"0.5"*2=1
0.5*2=1

Actual result:
--------------
(without the setlocale:)
floatval "0.5"*2=0
"0.5"*2=0
0.5*2=0

or 

Country set to: "nld_nld": Dutch_Netherlands.1252

floatval "0.5"*2=1
"0.5"*2=1
0.5*2=0

or when teh script is run a secondtime:

Country set to: "nld_nld": Dutch_Netherlands.1252

floatval "0.5"*2=1
"0.5"*2=1
0.5*2=1

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-01-19 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 09:01:28 2024 UTC