php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64682 failing to add 0.001 multiple times
Submitted: 2013-04-20 07:42 UTC Modified: 2013-04-20 07:50 UTC
From: easteregg at verfriemelt dot org Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.4.14 OS: Linux and Windows
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: easteregg at verfriemelt dot org
New email:
PHP Version: OS:

 

 [2013-04-20 07:42 UTC] easteregg at verfriemelt dot org
Description:
------------
Hi,

first some informations: vanilla php 5.4.14 without any changes from your 
website, and a php 5.4.13 on a linux host.

C:\Users\Administrator>php -v
PHP 5.4.12 (cli) (built: Feb 19 2013 21:26:17)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

root@verfriemelt:~# php -v
PHP 5.4.13-1~dotdeb.1 (cli) (built: Mar 21 2013 08:29:56)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies


------------


i have a number like 8 and add 0.001 while this number reaches 11. i suspected 
the numbers in between should look like 8.999 and 10.743 but instead i got some 
like this:

8.9899999999999

and i noticed a gap between some numbers.

eg:

8.09 + 0.001 equals 8.090999999999

i suspected a problem with my linux box and tested it with my windows 
workstation, same result. so i guess its a php internal error.

this doest not occur, when i simple add 0.001 to 8.09 so i guess it has 
something to do with the for()

Test script:
---------------
<?php
    for ($i = 8; $i< 11; $i += 0.001) {
	echo $i . "\n";
    }

Expected result:
----------------
[...]

8.08
8.081
8.082
8.083
8.084
8.085
8.086
8.087
8.088
8.089
8.09
8.091
8.092
8.093
8.094
8.095
8.096
8.097
8.098
8.099
8.1

[...]

Actual result:
--------------
8.08
8.081
8.082
8.083
8.084
8.085
8.086
[...]

8.087
8.088
8.089
8.09
8.0909999999999
8.0919999999999
8.0929999999999
8.0939999999999
8.0949999999999
8.0959999999999
8.0969999999999
8.0979999999999
8.0989999999999
8.0999999999999
8.1009999999999

[...]

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-04-20 07:50 UTC] johannes@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.

.
 [2013-04-20 07:50 UTC] johannes@php.net
-Status: Open +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 14:01:30 2024 UTC