Bug #75558 [Nab]: Float to integer conversion

From: Date: Thu, 23 Nov 2017 15:50:47 +0000
Subject: Bug #75558 [Nab]: Float to integer conversion
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-212698@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75558&edit=1

 ID:                 75558
 User updated by:    cyrille dot php at giquello dot fr
 Reported by:        cyrille dot php at giquello dot fr
 Summary:            Float to integer conversion
 Status:             Not a bug
 Type:               Bug
 Package:            *General Issues
 Operating System:   Ubuntu 16.04
 PHP Version:        7.1.11
 Block user comment: N
 Private report:     N

 New Comment:

Thanks à lot @nikic

And sorry for noise.


Previous Comments:
------------------------------------------------------------------------
[2017-11-23 11:28:35] 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 11:17:43] 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:15:03] 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:06:20] 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:03:21] 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().

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=75558


--
Edit this bug report at https://bugs.php.net/bug.php?id=75558&edit=1


Thread (9 messages)

« previous php.bugs (#212698) next »