php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80163 An error in operating a cast to an int
Submitted: 2020-09-30 01:44 UTC Modified: 2020-09-30 01:47 UTC
From: peter dot j dot burling at skymesh dot com dot au Assigned:
Status: Not a bug Package: Math related
PHP Version: 7.3.22 OS: Linux - Fedora fc30.x86_64
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: peter dot j dot burling at skymesh dot com dot au
New email:
PHP Version: OS:

 

 [2020-09-30 01:44 UTC] 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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-09-30 01:47 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2020-09-30 01:47 UTC] requinix@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-2025 The PHP Group
All rights reserved.
Last updated: Mon Mar 10 21:01:30 2025 UTC