Bug #74133 [Fbk->Nab]: PHP function in_array() return wrong.

From: Date: Mon, 20 Feb 2017 09:57:48 +0000
Subject: Bug #74133 [Fbk->Nab]: PHP function in_array() return wrong.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-207455@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 Updated by: requinix@php.net Reported by: long dot it dot stu at gmail dot com Summary: PHP function in_array() return wrong. -Status: Feedback +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: 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 Previous Comments: ------------------------------------------------------------------------ [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

« previous php.bugs (#207455) next »