php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53972 Operator Type Casting Issue
Submitted: 2011-02-09 13:56 UTC Modified: 2011-02-10 19:30 UTC
From: thipse_rahul at rediffmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.5 OS: Linux/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: thipse_rahul at rediffmail dot com
New email:
PHP Version: OS:

 

 [2011-02-09 13:56 UTC] thipse_rahul at rediffmail dot com
Description:
------------
Following code snippet -- 

Case 1:
$a = (int)((0.1+0.7) * 10); echo $a; // Result: 7 (Wrong) should give 8 

Case 2:
$a = (int)((0.1+0.6) * 10); echo $a; // Result: 7
Case 3:
$a = (int)((0.1+0.8) * 10); echo $a; // Result: 9


Test script:
---------------
$a = (int)((0.1+0.7) * 10); 
echo $a;




Expected result:
----------------
8

Actual result:
--------------
7

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-09 14:18 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2011-02-09 14:18 UTC] johannes@php.net
Floating point values have a limited precision. Hence a value might 
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly 
printing it without any mathematical operations.

If you would like to know more about "floats" and what IEEE
754 is, read this:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
 
Thank you for your interest in PHP.

.
 [2011-02-09 17:43 UTC] anon at anon dot anon
PHP guys, will you please stop giving people broken links?
 [2011-02-10 05:16 UTC] thipse_rahul at rediffmail dot com
Thanks for reply johannes 

Yes I agree for float, 
Then Whey Case(2) & Case(3) gives wrong result.
Case 2:
$a = (int)((0.1+0.6) * 10); echo $a; // Act.Result: 7  // Exp. : 6
Case 3:
$a = (int)((0.1+0.8) * 10); echo $a; // Act.Result: 9  // Exp.: 8 

Hope you will give satisfactory feedback.

Thanks!
 [2011-02-10 19:30 UTC] johannes@php.net
0.1+0.9 is not exactly 1 but a tiny bit less. (int) rounds down.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 11:01:32 2025 UTC