PHP:测试两个双变量会在不更改变量的情况下给出不同的结果
在我的代码中我有两个双精度值。我们称它们为 $a
和 $b
。现在我想测试一下哪一个更大,所以我写了以下内容:
print ($a > $b ? "larger\n" : "smaller\n");
print ($a > $b ? "larger\n" : "smaller\n");
奇怪的结果是
larger
smaller
有人以前遇到过类似的问题吗?这个问题只出现在我们使用php-cgi的嵌入式linux系统上。
感谢您的回答和建议。
这是整个代码:我需要将十六进制值解码为关于符号的十进制值,并最终使用大于整数大小的数字
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请运行
并在此处发布输出。 (很抱歉将此作为答案发布,但我的 Stackoverflow 声誉还不允许发表评论)
Please run
and post the output here. (sorry for posting this as an answer, but my Stackoverflow Reputation does not allow for comments, yet)