php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72938 Wrong output for intval of floats
Submitted: 2016-08-25 08:10 UTC Modified: 2016-08-25 09:35 UTC
From: inggo dot espinosa at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: Math related
PHP Version: 7.0.10 OS: Ubuntu 14.04
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: inggo dot espinosa at gmail dot com
New email:
PHP Version: OS:

 

 [2016-08-25 08:10 UTC] inggo dot espinosa at gmail dot com
Description:
------------
When using intval, float conversion is off for some very specific numbers. Please see my Test script below.

Test script:
---------------
<?php

echo intval(160.70 * 100) . "\n"; // Expected 16070, got 16069
echo intval(150.70 * 100) . "\n"; // Expected 15070, got 15069
echo intval(140.70 * 100) . "\n"; // Expected 14070, got 14069
echo intval(130.70 * 100) . "\n"; // Expected 13070, got 13069
echo intval(120.70 * 100) . "\n"; // Strangely, this gives the correct value of 12070
echo intval(160.50 * 100) . "\n"; // This also gives the correct value of 16050
echo intval(160.60 * 100) . "\n"; // This also gives the correct value of 16060
echo intval(170.70 * 100) . "\n"; // This also gives the correct value of 17070
echo intval(strval(160.70 * 100)); // This is the workaround that I am doing for now


Expected result:
----------------
16070
15070
14070
13070
12070
16050
16060
17070
16070

Actual result:
--------------
16069
15069
14069
13069
12070
16050
16060
17070
16070

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-08-25 09:35 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2016-08-25 09:35 UTC] cmb@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-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 05:01:28 2024 UTC