php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49187 Misbehaving function : intval ()
Submitted: 2009-08-07 05:20 UTC Modified: 2009-08-07 10:23 UTC
From: belanger dot b at gmail dot com Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: 5.2.5 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: belanger dot b at gmail dot com
New email:
PHP Version: OS:

 

 [2009-08-07 05:20 UTC] belanger dot b at gmail dot com
Description:
------------
intval is not returning the correct value for '995'

echo number_format('9.95'*100,0,'',''); // output 995
echo intval('9.95'*100); // output 994

Sorry but the most up to date server at my disposition is version 5.2.5

POSSIBLY RELATED REFUSED BUGS...
found by me :
http://bugs.php.net/bug.php?id=1960 (set to closed)
found by system :
http://bugs.php.net/bug.php?id=37554 (set to bogus)
http://bugs.php.net/bug.php?id=39900 (set to bogus)

Reproduce code:
---------------
echo number_format('9.95'*100,0,'','')
     .' and '. number_format('1.16'*100,0,'','')
     ."\n". intval('9.95'*100);
     .' and '. intval('1.16'*100);

Expected result:
----------------
995 and 116
995 and 116

Actual result:
--------------
995 and 116
994 and 115

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-08-07 05:51 UTC] belanger dot b at gmail dot com
Tested it with all values from previously rejected or ignored similar bug submissions (code and execution here) :
http://www.benlg.com/test.php

P.S.: intval('9.95'*100) == intval(9.95*100) == 994
 [2009-08-07 07:54 UTC] jani@php.net
Thank you for taking the time to report a problem with PHP.
Unfortunately you are not using a current version of PHP -- 
the problem might already be fixed. Please download a new
PHP version from http://www.php.net/downloads.php

If you are able to reproduce the bug with one of the latest
versions of PHP, please change the PHP version on this bug report
to the version you tested and change the status back to "Open".
Again, thank you for your continued support of PHP.


 [2009-08-07 10:23 UTC] rasmus@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://docs.sun.com/source/806-3568/ncg_goldberg.html
 
Thank you for your interest in PHP.


 [2018-05-09 11:48 UTC] belanger dot b at gmail dot com
This bug still exists in PHP 7.1.14.

$ php -a
Interactive shell

php > echo phpversion();
7.1.14
php > echo intval(9.95 * 100);
994
 [2018-05-09 12:00 UTC] spam2 at rhsoft dot net
> This bug still exists in PHP 7.1.14

it is not a bug and hence it can't be fixed - read the comment before yours

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://docs.sun.com/source/806-3568/ncg_goldberg.html
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 12 20:01:27 2024 UTC