php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67676 (int) return wrong number
Submitted: 2014-07-24 12:57 UTC Modified: 2014-07-24 19:21 UTC
From: andreiotetea at yahoo dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS: Linux, Windows, all
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: andreiotetea at yahoo dot com
New email:
PHP Version: OS:

 

 [2014-07-24 12:57 UTC] andreiotetea at yahoo dot com
Description:
------------
this is a general BUG, no matter of system used or php version..on all is same
tested here too: http://sandbox.onlinephpfunctions.com/

(int) is returning wrong number without trim.



Test script:
---------------
<?php
$number = 10.03;

$mfactor = pow(10,2);
echo (int)($number * $mfactor); // 1002 - wrong
echo (int)trim($number * $mfactor); // 1003 - good

?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-07-24 16:33 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2014-07-24 16:33 UTC] requinix@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://www.floating-point-gui.de/

Thank you for your interest in PHP.


 [2014-07-24 19:21 UTC] andreiotetea at yahoo dot com
I see... i understand your technical explanation.
Just when using (int) you are allways expect to convert some non integer values and floor only when is a float result.

echo "\n", $number * $mfactor; // 1003 - good,
echo "\n", (int)($number * $mfactor); // 1002 - wrong

so as a solution i see that when the result is an INTEGER like (int) should just leave as it is and not to get it's real value of 1002.9999...and convert to 1002 as (int) does.
..so in this case it should do a CEIL like a workaround.

maybe will be included in next versions of PHP.

thanks!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 21 14:01:33 2024 UTC