PHP: testing two double variables gives different results without changing the variables

there are two double values ​​in my code. Lets call them $a and $b . now I want to check which one is bigger, so I wrote the following:

 print ($a > $b ? "larger\n" : "smaller\n"); print ($a > $b ? "larger\n" : "smaller\n"); 

strange result

 larger smaller 

Has anyone encountered a similar problem before? This problem only occurs in our embedded Linux system using php-cgi.

Thanks for your answers and advice.

Here is the whole code: I need to decode a hexadecimal value to a decimal value with respect to the sign and, ultimately, useng numbers are larger than the size of the integer size

 function decodeInteger($datahex) { // ignore non hex characters $hex = preg_replace('/[^0-9A-Fa-f]/', '', $datahex); // converted decimal value as double: $dec = hexdec($hex) * 1.0; // maximum decimal value based on length of hex + 1: // number of bits in hex number is 8 bits for each 2 hex-characters -> max = 2^n // use 'pow(2.0,n)' since '1 << n' and 'pow(2,n)' is only for integers and therefore limited to integer size. $max = pow(2.0, 4 * (strlen($hex) + (strlen($hex) % 2))); // complement = maximum - converted hex: $_dec = $max - $dec; print ($dec > $_dec ? "larger\n" : "smaller\n"); print ($dec > $_dec ? "larger\n" : "smaller\n"); // if dec value is larger than its complement we have a negative value (first bit is set) return $dec > $_dec ? -$_dec : $dec; } 
+4
source share
1 answer

Please run

 var_dump($dec); var_dump($_dec); var_dump($dec); var_dump($_dec); print ($dec > $_dec ? "larger\n" : "smaller\n"); var_dump($dec); var_dump($_dec); var_dump($dec); var_dump($_dec); print ($dec > $_dec ? "larger\n" : "smaller\n"); 

and post the output here. (sorry to report this as an answer, but my Stackoverflow reputation still doesn't allow comments)

+1
source

Source: https://habr.com/ru/post/1310204/


All Articles