php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54248 disappearing array key [987]
Submitted: 2011-03-14 15:48 UTC Modified: 2011-03-14 17:16 UTC
From: rodoeted at yahoo dot com Assigned:
Status: Not a bug Package: Output Control
PHP Version: 5.3.5 OS: centos 5.5
Private report: No CVE-ID: None
 [2011-03-14 15:48 UTC] rodoeted at yahoo dot com
Description:
------------
the key 987 disappears

Test script:
---------------
$ceva=array(
	array('amount'=>9.89),
	array('amount'=>9.88),
	array('amount'=>9.87),
	array('amount'=>9.86),
	array('amount'=>9.85),
	array('amount'=>9.84),
	array('amount'=>9.83),
	array('amount'=>9.82),
	array('amount'=>8.86),
	array('amount'=>8.85),
	array('amount'=>8.84),
);

function print_a($array) {
	echo "<pre>";
	print_r($array);
	echo "</pre>";
}

print_a($ceva);


$usable=array();

foreach($ceva as $b){
	$x=$b['amount']*100;
	$usable[$x]=$b['amount'];
	var_dump($usable[$x]);
}

var_dump($usable);

Expected result:
----------------
<pre>Array
(
    [0] => Array
        (
            [amount] => 9.89
        )

    [1] => Array
        (
            [amount] => 9.88
        )

    [2] => Array
        (
            [amount] => 9.87
        )

    [3] => Array
        (
            [amount] => 9.86
        )

    [4] => Array
        (
            [amount] => 9.85
        )

    [5] => Array
        (
            [amount] => 9.84
        )

    [6] => Array
        (
            [amount] => 9.83
        )

    [7] => Array
        (
            [amount] => 9.82
        )

    [8] => Array
        (
            [amount] => 8.86
        )

    [9] => Array
        (
            [amount] => 8.85
        )

    [10] => Array
        (
            [amount] => 8.84
        )

)
</pre>float(9.89)
float(9.88)
float(9.87)
float(9.86)
float(9.85)
float(9.84)
float(9.83)
float(9.82)
float(8.86)
float(8.85)
float(8.84)
array(10) {
  [989]=>
  float(9.89)
  [988]=>
  float(9.88)
  [987]=>
  float(9.87)
  [986]=>
  float(9.86)
  [985]=>
  float(9.85)
  [984]=>
  float(9.84)
  [983]=>
  float(9.83)
  [982]=>
  float(9.82)
  [886]=>
  float(8.86)
  [885]=>
  float(8.85)
  [884]=>
  float(8.84)
}


Actual result:
--------------
<pre>Array
(
    [0] => Array
        (
            [amount] => 9.89
        )

    [1] => Array
        (
            [amount] => 9.88
        )

    [2] => Array
        (
            [amount] => 9.87
        )

    [3] => Array
        (
            [amount] => 9.86
        )

    [4] => Array
        (
            [amount] => 9.85
        )

    [5] => Array
        (
            [amount] => 9.84
        )

    [6] => Array
        (
            [amount] => 9.83
        )

    [7] => Array
        (
            [amount] => 9.82
        )

    [8] => Array
        (
            [amount] => 8.86
        )

    [9] => Array
        (
            [amount] => 8.85
        )

    [10] => Array
        (
            [amount] => 8.84
        )

)
</pre>float(9.89)
float(9.88)
float(9.87)
float(9.86)
float(9.85)
float(9.84)
float(9.83)
float(9.82)
float(8.86)
float(8.85)
float(8.84)
array(10) {
  [989]=>
  float(9.89)
  [988]=>
  float(9.88)
  [986]=>
  float(9.86)
  [985]=>
  float(9.85)
  [984]=>
  float(9.84)
  [983]=>
  float(9.83)
  [982]=>
  float(9.82)
  [886]=>
  float(8.86)
  [885]=>
  float(8.85)
  [884]=>
  float(8.84)
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-14 17:16 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2011-03-14 17:16 UTC] rasmus@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.

.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 22:01:28 2024 UTC