php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #20688 Math zero
Submitted: 2002-11-27 19:42 UTC Modified: 2003-05-20 04:21 UTC
From: xqcheng at mail dot xjtu dot edu dot cn Assigned:
Status: Not a bug Package: Math related
PHP Version: 4.2.3 OS: windows
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: xqcheng at mail dot xjtu dot edu dot cn
New email:
PHP Version: OS:

 

 [2002-11-27 19:42 UTC] xqcheng at mail dot xjtu dot edu dot cn
<?php
$temp=-1.0;
for($i=0;$i<=10;$i++)
{
$temp=$temp+0.2;
echo "<br>".$temp;
}
?>

the output is :

-0.8
-0.6
-0.4
-0.2
-5.55111512313E-017
0.2
0.4
0.6
0.8
1
1.2

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-11-27 23:18 UTC] alan_k@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

there are quite a few bug reports on this.

adding floats can produce unpredicable results
you could try using bcmath or sprintf

$temp=-1.0;
for($i=0;$i<=10;$i++){
$temp=(float) sprintf(\"%.2f\",$temp+0.2);
echo \"<br>\".$temp;
}
 [2003-05-19 13:08 UTC] rmendes at agencyweb dot net
I have a similar problem :

<?
$a = 0.02;
$b = -0.02;
$total = $a +$b;
echo "$total";
?>

the output is :

Total : 5.55111512313E-017

Why ? and i am forced tio use bcmath ??

Thanks.
 [2003-05-20 04:21 UTC] mgf@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

For "why", please see http://www.php.net/language.types.float (the box headed "Floating point precision").

For more help on working with floating point numbers, please try a support forum such as php-general@lists.php.net.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 18:01:29 2024 UTC