php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64415 Incorrect conversion between float and int
Submitted: 2013-03-12 18:44 UTC Modified: 2013-03-12 18:47 UTC
From: bb at codepoet dot de Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.4.12 OS: Debian, Ubuntu, OSX
Private report: No CVE-ID: None
 [2013-03-12 18:44 UTC] 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)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-03-12 18:47 UTC] aharvey@php.net
-Status: Open +Status: Not a bug
 [2013-03-12 18:47 UTC] aharvey@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.

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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC