#41714 [Bgs]: Problem with intval()

From: Date: Sun, 17 Jun 2007 21:19:08 +0000
Subject: #41714 [Bgs]: Problem with intval()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-113880@lists.php.net to get a copy of this message
ID: 41714 User updated by: bartlomiejbak at gmail dot com Reported By: bartlomiejbak at gmail dot com Status: Bogus Bug Type: *Math Functions Operating System: ubuntu PHP Version: 5.2.3 New Comment: so. if you are right look at results of this code function isInt($value) { return preg_match("/^-?[0-9]+$/",$value); } for ($y = 0; $y < 100; $y++) { $x = (1/20) - (27/20) * $y; if (isInt($x)) { echo "x = $x; intval(x) = ".intval($x)."; var_dump(x == intval(x)) = "; var_dump($x == intval($x)); echo "\n"; } } first result // x = -4; intval(x) = -4; var_dump(x == intval(x)) = bool(false) why? Previous Comments: ------------------------------------------------------------------------ [2007-06-17 09:34:19] johannes@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://docs.sun.com/source/806-3568/ncg_goldberg.html Thank you for your interest in PHP. . ------------------------------------------------------------------------ [2007-06-16 21:18:01] bartlomiejbak at gmail dot com Description: ------------ Hi. I've got a little bit problem this code should show me result of equation 20x + 27y = 1 for ($y = 0; $y < 1000; $y++) { $x = (1/20) - (27/20) * $y; if ($x == intval($x)) $out .= "x: $x,y: $y\n"; } I need only int values of X and Y but intval() sometimes return wrong value. Result should be (-4,3),(-31,23),(-58,43) etc. but php is starting showing from (-31,23)... ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=41714&edit=1

« previous php.bugs (#113880) next »