Edit report at https://bugs.php.net/bug.php?id=64415&edit=1

 ID:                 64415
 Updated by:         [email protected]
 Reported by:        bb at codepoet dot de
 Summary:            Incorrect conversion between float and int
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            Variables related
 Operating System:   Debian, Ubuntu, OSX
 PHP Version:        5.4.12
 Block user comment: N
 Private report:     N

 New Comment:

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.

PHP's precision option is obscuring that the double value is actually 
1998.9999... — you can see this more clearly at 
http://codepad.viper-7.com/rRWuzg


Previous Comments:
------------------------------------------------------------------------
[2013-03-12 18:44:48] bb at codepoet dot de

Description:
------------
If I multiply 19.99 with 100 and cast it to int, I expect a value of 1999, but 
I 
get 1998. I verified this on 5 different php 5.4 installations on Mac OSX 
10.8.2, 
Debian and Ubuntu.

Test script:
---------------
<?php
var_dump('TEST #1');
var_dump($x = floatval(19.99 * 100));
var_dump($x = intval($x));

var_dump('TEST #2');
var_dump($x = floatval(1999));
var_dump($x = intval($x));


Expected result:
----------------
string(7) "TEST #1"
double(1999)
int(1999)



Actual result:
--------------
string(7) "TEST #2"
double(1999)
int(1998)


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=64415&edit=1

Reply via email to