php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74534 Floating point precision issue
Submitted: 2017-05-03 10:56 UTC Modified: 2017-05-03 11:00 UTC
From: uchihasv at yandex dot ru Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.1.4 OS: Windows 7 x64
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: uchihasv at yandex dot ru
New email:
PHP Version: OS:

 

 [2017-05-03 10:56 UTC] uchihasv at yandex dot ru
Description:
------------
I have array with numbers and i do simple math in loop and in results i see -6.9388939039072E-17 instead 0

Test script:
---------------
<?php
$h = 0.05;
$x = [-1,-0.5,0,0.5,1];

for($i=0;$i<4;$i++)
{
	for($j=$x[$i]+$h;$j<$x[$i+1];$j+=$h)
	{
		$x2[$i][] = $j;
	}
}

echo "<pre>".print_r($x2,1)."</pre>";

Expected result:
----------------
Array
(
    [0] => Array
        (
            [0] => -0.95
            [1] => -0.9
            [2] => -0.85
            [3] => -0.8
            [4] => -0.75
            [5] => -0.7
            [6] => -0.65
            [7] => -0.6
            [8] => -0.55
        )

    [1] => Array
        (
            [0] => -0.45
            [1] => -0.4
            [2] => -0.35
            [3] => -0.3
            [4] => -0.25
            [5] => -0.2
            [6] => -0.15
            [7] => -0.1
            [8] => -0.05
            [9] => 0
        )

    [2] => Array
        (
            [0] => 0.05
            [1] => 0.1
            [2] => 0.15
            [3] => 0.2
            [4] => 0.25
            [5] => 0.3
            [6] => 0.35
            [7] => 0.4
            [8] => 0.45
            [9] => 0.5
        )

    [3] => Array
        (
            [0] => 0.55
            [1] => 0.6
            [2] => 0.65
            [3] => 0.7
            [4] => 0.75
            [5] => 0.8
            [6] => 0.85
            [7] => 0.9
            [8] => 0.95
        )

)

Actual result:
--------------
Array
(
    [0] => Array
        (
            [0] => -0.95
            [1] => -0.9
            [2] => -0.85
            [3] => -0.8
            [4] => -0.75
            [5] => -0.7
            [6] => -0.65
            [7] => -0.6
            [8] => -0.55
        )

    [1] => Array
        (
            [0] => -0.45
            [1] => -0.4
            [2] => -0.35
            [3] => -0.3
            [4] => -0.25
            [5] => -0.2
            [6] => -0.15
            [7] => -0.1
            [8] => -0.05
            [9] => -6.9388939039072E-17
        )

    [2] => Array
        (
            [0] => 0.05
            [1] => 0.1
            [2] => 0.15
            [3] => 0.2
            [4] => 0.25
            [5] => 0.3
            [6] => 0.35
            [7] => 0.4
            [8] => 0.45
            [9] => 0.5
        )

    [3] => Array
        (
            [0] => 0.55
            [1] => 0.6
            [2] => 0.65
            [3] => 0.7
            [4] => 0.75
            [5] => 0.8
            [6] => 0.85
            [7] => 0.9
            [8] => 0.95
        )

)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-05-03 10:58 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2017-05-03 10:58 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.


 [2017-05-03 11:00 UTC] kelunik@php.net
-Summary: 123 +Summary: Floating point precision issue
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Oct 06 15:00:01 2026 UTC