php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75558 Float to integer conversion
Submitted: 2017-11-23 10:50 UTC Modified: 2017-11-23 15:50 UTC
From: cyrille dot php at giquello dot fr Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.1.11 OS: Ubuntu 16.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: cyrille dot php at giquello dot fr
New email:
PHP Version: OS:

 

 [2017-11-23 10:50 UTC] cyrille dot php at giquello dot fr
Description:
------------
Hi,

I've read the warning about float on documentation  http://php.net/manual/fr/language.types.float.php but for simple computing it should be nice if Php return value like other languages (Python & Java).

The computing (int)( 103.24 * 6 * 10**2) should return 61944 nor 61943.

I've done the same with Java & Python (converting float result to integer) and get the right result: 61944



Test script:
---------------
php -r 'echo (int)( 103.24 * 6 * 10**2),"\n";'


Expected result:
----------------
61944

Actual result:
--------------
61943

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-11-23 10:59 UTC] cyrille dot php at giquello dot fr
Another test script:

Test script:
---------------

php -r '$a=( 103.24*6*10**2); echo $a,"=",intval($a),"=",intval(61944.0),"\n";'

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

61944=61944=61944

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

61944=61943=61944
 [2017-11-23 10:59 UTC] spam2 at rhsoft dot net
that's indeed a joke

php > echo 103.24 * 6 * 10**2;
61944

php > echo round(( 103.24 * 6 * 10**2));
61944

php > echo (int)( 103.24 * 6 * 10**2);
61943
 [2017-11-23 11:03 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2017-11-23 11:03 UTC] nikic@php.net
I have no idea what you tried in Python and Java, but it was clearly something else, because these are standard floating point semantics that are consistent across all languages. I've just checked `(int)(103.24 * 6 * 10**2)` on Python 3.6 and it indeed gives 61943, as expected.

If you don't want truncating conversion ("round down" for positive numbers), don't use integer casts, or ensure that the floating point number represents an exact integer beforehand, by using round().
 [2017-11-23 11:06 UTC] spam2 at rhsoft dot net
> If you don't want truncating conversion

where are the decimal parts which could be truncated?

php > echo 103.24 * 6 * 10**2;
61944
 [2017-11-23 11:15 UTC] cyrille dot php at giquello dot fr
I do not want to round() but only truncate decimal part, if one exists.

You're example well show that integer casting make lost one unit on a number without decimal :

php > echo (int)( 103.24 * 6 * 10**2);
61943

Perhaps it's not a bug, I've just to learn the well use of float.

Thanks for your answer.
Cheers.
 [2017-11-23 11:17 UTC] cyrille dot php at giquello dot fr
In Java

    float a = (float) (103.24 * 6 * Math.pow(10,2)) ;
    System.out.println( a ); // print 61944.0
    System.out.println( (int) a ); // print 61944, not 61943
 [2017-11-23 11:28 UTC] nikic@php.net
@cyrille: The reason it "works" in Java is that you're using the float type instead of the double type. If you use the double type instead, you will receive 61943 as the result. Floating-point numbers in PHP are always double-precision, not single-precision.

@rhsoft: Floating-point to string conversion is controlled by the "precision" ini settings. If you would like to see all round-trip relevant digits, set precision=-1. In this case, this will display "61943.99999999999" rather than "61944".
 [2017-11-23 15:50 UTC] cyrille dot php at giquello dot fr
Thanks à lot @nikic

And sorry for noise.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 10 11:01:32 2024 UTC