php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64459 Wrong value on casting a float point number into int
Submitted: 2013-03-19 20:18 UTC Modified: 2013-03-19 20:27 UTC
From: marc-bennewitz at arcor dot de Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: master-Git-2013-03-19 (Git) OS: Linux acer-aspire-5680 3.2.0-38-
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: marc-bennewitz at arcor dot de
New email:
PHP Version: OS:

 

 [2013-03-19 20:18 UTC] marc-bennewitz at arcor dot de
Description:
------------
On some circumstances it's possible to get a wrong integer value on casting from float.

I can reproduce this bug with the following installations:
php -v
PHP 5.3.10-1ubuntu3.6 with Suhosin-Patch (cli) (built: Mar 11 2013 14:34:31) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
    with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans

./sapi/cli/php -v
PHP 5.6.0-dev (cli) (built: Mar 17 2013 22:02:54) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.6.0-dev, Copyright (c) 1998-2013 Zend Technologies

Test script:
---------------
// this works
$v = (float) 6;
var_dump($v, (int)$v);

// this is wrong
$v = log(64, 2);
var_dump($v, (int)$v);

Expected result:
----------------
float(6)
int(6)
float(6)
int(6)


Actual result:
--------------
float(6)
int(6)
float(6)
int(5)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-03-19 20:27 UTC] aharvey@php.net
-Status: Open +Status: Not a bug
 [2013-03-19 20:27 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 Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 15:01:33 2025 UTC