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
 [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

Pull Requests

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-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 10 00:01:28 2024 UTC