Bug #72938 [Opn->Nab]: Wrong output for intval of floats

From: Date: Thu, 25 Aug 2016 09:35:26 +0000
Subject: Bug #72938 [Opn->Nab]: Wrong output for intval of floats
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-203533@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72938&edit=1

 ID:                 72938
 Updated by:         cmb@php.net
 Reported by:        inggo dot espinosa at gmail dot com
 Summary:            Wrong output for intval of floats
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            Math related
 Operating System:   Ubuntu 14.04
 PHP Version:        7.0.10
-Assigned To:        
+Assigned To:        cmb
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2016-08-25 08:10:41] 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


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



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


Thread (2 messages)

« previous php.bugs (#203533) next »