php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #16036 in_array() with pow()
Submitted: 2002-03-13 06:15 UTC Modified: 2002-03-13 06:55 UTC
From: goodman888 at hongkong dot com Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: 4.1.0 OS: Solaris
Private report: No CVE-ID: None
 [2002-03-13 06:15 UTC] goodman888 at hongkong dot com
$a = pow(2,31);
$b = array($a);
var_dump( in_array(2147483648, $b));


$b = array(2147483648);
var_dump( in_array(2147483648, $b));


// this is the result. how come there are different?
bool(false) bool(true) 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-03-13 06:20 UTC] mfischer@php.net
var_dump() _ONLY_ works on variables.
 [2002-03-13 06:23 UTC] rasmus@php.net
This is a variation of the commonly asked floating point number comparison question.  At numbers beyond 2^31-1 pow() returns a float.  Doing exact comparisons of an integer against a floating point value in any computer language is tricky because of the way floating point numbers are represented.  You could make sure your precision is set the way you want and convert to a string, or apply a fuzz factor.
 [2002-03-13 06:47 UTC] goodman888 at hongkong dot com
Can anyone give me a "workable" version?

I've tried even to convert all numbers into integers, floats or whatever. It still does not work.
 [2002-03-13 06:55 UTC] rasmus@php.net
The bug database is not the place to ask questions like this.  And like I mentioned, just convert to a string:

    $a = pow(2,31);
    $b = array("$a");
    echo in_array(2147483648, $b);

That prints 1

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 23:01:28 2024 UTC