Bug #80163 [Opn->Nab]: An error in operating a cast to an int
| From: | requinix@php.net | Date: | Wed, 30 Sep 2020 01:47:10 +0000 |
| Subject: | Bug #80163 [Opn->Nab]: An error in operating a cast to an int | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-229280@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80163&edit=1
ID: 80163
Updated by: requinix@php.net
Reported by: peter dot j dot burling at skymesh dot com dot au
Summary: An error in operating a cast to an int
-Status: Open
+Status: Not a bug
Type: Bug
Package: Math related
Operating System: Linux - Fedora fc30.x86_64
PHP Version: 7.3.22
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:
------------------------------------------------------------------------
[2020-09-30 01:44:15] peter dot j dot burling at skymesh dot com dot au
Description:
------------
Casting to an int where a subtraction equals 0.2, when multiplied by 10 and cast to an int returns a
value of 1. If the subtraction equals 0.3 then the result of the above cast will equal 3
Test script:
---------------
$kwatts = ((int)(($row[7]Ã - $row[4]) * 10))/10 ;
with $row[7] = 55.40
with $row[4] = 55.20
but returning a $kwatts value of 0.1
php > $a = 0.1;
php > $b = 0.3;
php > echo $b - $a ;
0.2
php > echo ($b - $a)* 10 ;
2
php > echo(int)($b - $a)* 10 ;
0
php > echo(int)(($b - $a)* 10) ;
1
php > echo(int)(10 * ($b - $a)) ;
1
php > echo(int)((10 * ($b - $a))) ;
1
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80163&edit=1