views:

115

answers:

1

Dear All,

in my code i have two double values. Lets call them $a and $b. now I want to test which one of them is larger, so I wrote the following:

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

strangely the result is

larger
smaller

does anybody encountered a similar problem before? This problem only appears on our embedded linux system using php-cgi.

Thanks for your answers and advices.

Here is the whole code: I need to decode a hex value into a decimal value regarding the sign and eventually useng numbers larger than 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;
}
+1  A: 

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 for posting this as an answer, but my Stackoverflow Reputation does not allow for comments, yet)

artistoex