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

Add a Patch

Pull Requests

Add a Pull Request

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: Tue Apr 30 01:01:28 2024 UTC