Bug #74534 [Opn->Nab]: 123

From: Date: Wed, 03 May 2017 10:58:28 +0000
Subject: Bug #74534 [Opn->Nab]: 123
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-208934@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=74534&edit=1

 ID:                 74534
 Updated by:         requinix@php.net
 Reported by:        uchihasv at yandex dot ru
 Summary:            123
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            *General Issues
 Operating System:   Windows 7 x64
 PHP Version:        7.1.4
 Block user comment: N
 Private report:     N

 New Comment:

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.




Previous Comments:
------------------------------------------------------------------------
[2017-05-03 10:56:50] 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
        )

)


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



--
Edit this bug report at https://bugs.php.net/bug.php?id=74534&edit=1


Thread (2 messages)

« previous php.bugs (#208934) next »