php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79443 round with precision is not rounding
Submitted: 2020-04-01 15:41 UTC Modified: 2020-04-01 18:09 UTC
From: info at globules dot io Assigned: cmb (profile)
Status: Not a bug Package: *General Issues
PHP Version: 7.4.4 OS: Windows 10 x64
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: info at globules dot io
New email:
PHP Version: OS:

 

 [2020-04-01 15:41 UTC] info at globules dot io
Description:
------------
round with precision is not rounding

Test script:
---------------
echo round(-80.0035326999999938, 6);


Expected result:
----------------
-80.003533

Actual result:
--------------
-80.0035330000000044


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-04-01 15:44 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2020-04-01 15:44 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.
 [2020-04-01 15:48 UTC] danack@php.net
Mr Info 

Are you sure that code you posted is actually the code that's producing that output?

https://3v4l.org/v9k54
 [2020-04-01 16:06 UTC] info at globules dot io
Yes I have posted the exact code.
 [2020-04-01 16:10 UTC] info at globules dot io
How is this not a bug?
This is the exact code. I'm rounding with precision, it works on Linux, doesn't work on Windows.
 [2020-04-01 16:40 UTC] tiffany@php.net
I am on Windows 10 Pro x64 and ran this from command prompt.

C:\Users\Tylae>php -r "echo round(-80.0035326999999938, 6);"
-80.003533
 [2020-04-01 16:51 UTC] bugreports at gmail dot com
> I am on Windows 10 Pro x64 and ran this from command prompt

yeah, but you didn't mangle the "precision" config (look at the subejct)

garbage in, garbage out and so we are at: 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

php -n -d precision=100  -r "echo round(-80.0035326999999938, 6);"
-80.00353300000000444924808107316493988037109375
 [2020-04-01 16:57 UTC] cmb@php.net
Also note that round() returns a float.
 [2020-04-01 17:02 UTC] info at globules dot io
Oh I see, our precision on the win box in the php.ini is different... I guess that's the difference we see. Not sure how to fix it now but thanks.
 [2020-04-01 17:32 UTC] tiffany@php.net
If it's any help at all, the most I've changed in my php.ini file is adding the file path for logging, otherwise everything has been left default. Maybe do a diff with a default php.ini file against your current php.ini file.
 [2020-04-01 17:53 UTC] info at globules dot io
tiffany, we actually need a greater precision for scientific calculations. The Linux box is in fact the one that is not properly configured. 
The issue arises when some other values that are not scientific need to be rounded and validated before storage, and this values can't exceed 8 decimals. But round doesn't supersede the precision, which creates the issue.
 [2020-04-01 18:04 UTC] info at globules dot io
Any workaround/idea welcome. Some values cannot be stored as string and need to be trimmed (8 decimals max). Those are not the scientific values that require high precision.
 [2020-04-01 18:09 UTC] nikic@php.net
@info at globules dot io: Set precision=-1 to display the value with full accuracy but without unnecessary digits.

For fine-grained control use sprintf().
 [2020-04-01 18:11 UTC] info at globules dot io
Thanks Nikic :)
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sun Oct 02 14:03:45 2022 UTC