php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30018 multiplication bug (with float values)
Submitted: 2004-09-07 23:32 UTC Modified: 2004-09-08 06:05 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: phpbug at javision dot de Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.8 OS: linux
Private report: No CVE-ID: None
 [2004-09-07 23:32 UTC] phpbug at javision dot de
Description:
------------
sometimes php doesn't multiplicate float values and cuts the position after the decimal point

Reproduce code:
---------------
//$aRegs[2][0] = 402.83 (regex match from website)
echo "<pre>\n";
var_dump($aRegs[2][0]);
echo "</pre>\n";
$zahl = $aRegs[2][0];
echo "<pre>\n";
var_dump($zahl);
echo "</pre>\n";
$zahl1 = doubleval($zahl);
echo "<pre>\n";
var_dump($zahl1);
echo "</pre>\n";
$zahl2 = (1.16*$zahl1);
echo "<pre>\n";
var_dump($zahl2);
echo "</pre>\n";
$zahl3 = (1.10*$zahl2);
echo "<pre>\n";
var_dump($zahl3);
echo "</pre>\n";

Expected result:
----------------
float(402.83)
float(402.83)
float(402.83)
float(467.2828)
float(514.01108)


Actual result:
--------------
float(402)
float(402)
float(402)
float(402)
float(402)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-09-08 00:26 UTC] rasmus@php.net
Computers can only approximate floating point arithmetic to a given precision level.  Read http://de.php.net/manual/en/language.types.float.php

 [2004-09-08 01:52 UTC] phpbug at javision dot de
i understand the rounding problem, but the bigger problem is that 402.83*1.16 returns 402.83 sometimes and sometimes it returns 467.2828. so sometimes php multiplicates the number with 1.16 and sometimes it doesn't
 [2004-09-08 06:05 UTC] rasmus@php.net
No chance.  Triple-check your input.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 06:01:38 2025 UTC