 php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login go to bug id or search bugs for
Bug #21769 Submitted: round error 2003-01-20 03:48 UTC 2003-01-20 03:54 UTC ltaupiac at lfdj dot com Not a bug Math related 4.3.0 Solaris 7 No None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.

[2003-01-20 03:48 UTC] ltaupiac at lfdj dot com
```<?
\$a=4.31;
\$b=5;
\$m=\$a*\$b ;
\$res=21.55 ;

\$v1=round(4.31*5,1);
\$v2=round(\$m,1) ;
\$v3=round(\$a*\$b,1) ;
\$v4=round(\$res,1) ;

\$vbiz=round(\$a*\$b,2) ;

\$r=\$v2-\$v1 ;
\$r2=\$v3-\$v1 ;
\$r3=\$res-\$m ;

print ("<b>a=-\$a-  b=-\$b-  m=\$a*\$b=-\$m- and res=\$res")  ."-<br></b>" ;
print ("v1=round(4.31 * 5 , 1)=-\$v1-  -") . round(\$v1,1) ."-<br>" ;
print ("v2=round(m, 1)=-\$v2-  -") . round(\$v2,1) ."-<br>" ;
print ("v3=round(a*b , 1)=-\$v3-  -") . round(\$v3,1) ."-<br>" ;
print ("v4=round(res , 1)=-\$v4-  -") . round(\$v4,1) ."-<br>" ;
print ("expected round by 2 round... <br>") ;
print ("vbiz=round(\$a*\$b ,2)=\$vbiz -- and round(\$vbiz, 1)=") . round(\$vbiz,1) ."<br><br>" ;

print ("r=\$v2-\$v1=\$r -good-<br>") ;
print ("r2=\$v3-\$v1=\$r2 -good-<br>") ;
print ("unexpected result... <br>") ;
print ("r3=\$res-\$m=\$r3 -Huh ?-<br>") ;

?>

will give that output

a=-4.31- b=-5- m=4.31*5=-21.55- and res=21.55-
v1=round(4.31 * 5 , 1)=-21.5- -21.5-
v2=round(m, 1)=-21.5- -21.5-
v3=round(a*b , 1)=-21.5- -21.5-
v4=round(res , 1)=-21.6- -21.6-
expected round by 2 round...
vbiz=round(4.31*5 ,2)=21.55 -- and round(21.55, 1)=21.6

r=21.5-21.5=0 -good-
r2=21.5-21.5=0 -good-
unexpected result...
r3=21.55-21.55=3.5527136788005E-15 -Huh ?-

```

## Pull Requests

```Thank you for taking the time to write to us, but this is not Copyright © 2001-2020 The PHP Group All rights reserved. Last updated: Mon Jan 27 21:01:23 2020 UTC