Bug #74133 [Com]: PHP function in_array() return wrong.

From: Date: Mon, 20 Feb 2017 11:33:08 +0000
Subject: Bug #74133 [Com]: PHP function in_array() return wrong.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-207461@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=74133&edit=1

 ID:                 74133
 Comment by:         long dot it dot stu at gmail dot com
 Reported by:        long dot it dot stu at gmail dot com
 Summary:            PHP function in_array() return wrong.
 Status:             Not a bug
 Type:               Bug
 Package:            Arrays related
 Operating System:   Centos
 PHP Version:        5.6.30
 Block user comment: N
 Private report:     N

 New Comment:

Hi team!. it's clearly. thanks for your feedback!.


Previous Comments:
------------------------------------------------------------------------
[2017-02-20 09:57:46] 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.

Oh, sorry, I mistook how the range() worked.

https://3v4l.org/Nb2iK
The values in the arrays aren't what you think because floating-point numbers aren't
always accurate. You could round each value in the array, or use count+array_filter instead, or do
something else.
https://3v4l.org/JK901
https://3v4l.org/r8Pq8
https://3v4l.org/dZq7U

------------------------------------------------------------------------
[2017-02-20 09:09:29] long dot it dot stu at gmail dot com

with function range(1, 1.9, 0.1) it return result under:
Array
(
    [0] => 1
    [1] => 1.1
    [2] => 1.2
    [3] => 1.3
    [4] => 1.4
    [5] => 1.5
    [6] => 1.6
    [7] => 1.7
    [8] => 1.8
)
when i check in_array(1.7, range(1, 1.9, 0.1));
it return False;
But all item in array except 1.7 it return False.
in range(0, 0.9, 0.1) it resut too.

------------------------------------------------------------------------
[2017-02-20 08:20:13] requinix@php.net

None of those four values exist in either of those two arrays.

------------------------------------------------------------------------
[2017-02-20 06:29:15] long dot it dot stu at gmail dot com

Description:
------------
---
From manual page: http://www.php.net/security.globals
---
PHP function in_array() return False although it existing.


Test script:
---------------
<?php
$array_1 = range(1, 1.9, 0.1);
var_dump(in_array(1.7, $array_1));
array_2 = range(0, 0.9, 0.1);
var_dump(in_array(0.3, $array_2));
var_dump(in_array(0.6, $array_2));
var_dump(in_array(0.7, $array_2));

//=>it return false;



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



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


Thread (5 messages)

« previous php.bugs (#207461) next »