php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64742 float(1) casts to int as 0
Submitted: 2013-04-30 14:37 UTC Modified: 2013-04-30 14:40 UTC
From: bondo dot the dot clown at gmail dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: Irrelevant OS: linux
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: bondo dot the dot clown at gmail dot com
New email:
PHP Version: OS:

 

 [2013-04-30 14:37 UTC] bondo dot the dot clown at gmail dot com
Description:
------------
code speaks for himself

php --version
  PHP 5.4.6-1ubuntu1.2 (cli) (built: Mar 11 2013 14:57:54) 
  Copyright (c) 1997-2012 The PHP Group
  Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
    with XCache v2.0.0, Copyright (c) 2005-2012, by mOo
    with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans



Test script:
---------------
echo (int)(8.95 - 7.95);

Expected result:
----------------
1

Actual result:
--------------
0

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-04-30 14:40 UTC] johannes@php.net
-Status: Open +Status: Not a bug
 [2013-04-30 14:40 UTC] johannes@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.

8.95 - 7.95 is not exactly 1 (int) rounds down.

$ php -d precision=99 -r 'echo 8.95 - 7.95;'
0.99999999999999911182158029987476766109466552734375
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 04:01:29 2024 UTC