php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81285 Division of floats results strange values
Submitted: 2021-07-23 13:51 UTC Modified: 2021-07-23 13:55 UTC
From: rico dot humme at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: Math related
PHP Version: 8.0.8 OS: Ubuntu
Private report: No CVE-ID: None
 [2021-07-23 13:51 UTC] rico dot humme at gmail dot com
Description:
------------
I've come across this as trying to find a workaround for fmod()

Calculations: 
9.6 / 3.2
9.9 / 3.3

Results:
Before PHP 8:
float(3)
float(3)

PHP 8+:
float(2.9999999999999996)
float(3.0000000000000004)

Test script:
---------------
Test script for testing on multiple versions:
https://3v4l.org/9bRl5

Expected result:
----------------
Result should return float(3) on all versions


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-07-23 13:55 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2021-07-23 13:55 UTC] cmb@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.

var_dump() uses serialize_precision instead of precision as of PHP
8.0.0, see <https://3v4l.org/s82d8>.
 
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sun Feb 05 02:05:48 2023 UTC