php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67978 Bug with floating number
Submitted: 2014-09-08 12:47 UTC Modified: 2014-09-08 15:08 UTC
From: tripollite at gmail dot com Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: Irrelevant OS: Debian 7
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
26 - 3 = ?
Subscribe to this entry?

 
 [2014-09-08 12:47 UTC] tripollite at gmail dot com
Description:
------------
Var dump fail to show right number but this number is used in memory to make calcul.


it should respect precision by cutting floating numbers
or showing the right number

Test script:
---------------
http://3v4l.org/AFQF6

<?php
$b = 0.27*28;
$a = round($b,2);
var_dump($a-$b);

$b = 0.270000000000001;
$a = round($b,2);
var_dump($b);var_dump($a-$b);

Expected result:
----------------
float(0)
float(0.27)
float(0)

Actual result:
--------------
float(-8.8817841970013E-16)
float(0.27)
float(-9.9920072216264E-16)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-09-08 15:08 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2014-09-08 15:08 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.

If you need numbers rounded to a certain precision then you need to do that yourself.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Fri Dec 02 03:05:53 2022 UTC