Bug #67676 [Nab]: (int) return wrong number
Edit report at https://bugs.php.net/bug.php?id=67676&edit=1
ID: 67676
User updated by: andreiotetea at yahoo dot com
Reported by: andreiotetea at yahoo dot com
Summary: (int) return wrong number
Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: Linux, Windows, all
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
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!
Previous Comments:
------------------------------------------------------------------------
[2014-07-24 16:33:01] 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 12:57:00] 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
?>
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67676&edit=1
Thread (3 messages)