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
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: tripollite at gmail dot com
New email:
PHP Version: OS:

 

 [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: Tue Nov 29 18:05:52 2022 UTC