I have very complicated calculation involving trigonometry that assign result to $x. When I print $x it will say it is float(-1):
var_dump($x); // this will print float(-1)
When I check if it is lower than -1, it will evaluate to true!
if ($x < -1) {
echo $x.' is lower than -1'; // it will print "-1 is lower than -1"
}
if (floatval($x) < floatval(-1)) {
echo floatval($x).' is lower than '.floatval(-1); // it will print "-1 is lower than -1"
}
if (strval($x) === strval(-1)) {
echo strval($x).' is equal to '.strval(-1); // it will print "-1 is equal to -1"
}
How is this possible? How can -1 be lower than -1? I'm using PHP 7.4.3 (built: Aug 13 2021 05:39:12) (NTS) from Ubuntu. But same thing happened in hosting.
This is offending function in it's entirety. It dies with saying "-1 is lower than -1":
function Qacos($aAngle) {
if ($aAngle < -1) {
die($aAngle.' is lower than -1');
}
return 180 * acos($aAngle) / M_PI;
}
function Qsin($aAngle) {
return sin(M_PI * $aAngle / 180);
}
function Qcos($aAngle) {
return cos(M_PI * $aAngle / 180);
}
$c = Qsin(7.5937478568555);
$d = Qsin(33.2207);
$e = Qsin(64.373047856856);
$f = Qcos(33.2207);
$g = Qcos(64.373047856856);
$x = ($c - $d * $e) / ($f * $g);
var_dump($x);
if ($x < -1) {
die('x lower than -1');
}