在php中了解浮点数 [英] Understanding floating point numbers in php
问题描述
我知道这些问题可能会被问到很多,但是从我的阅读和测试来看,这让我感到困惑了一些,而且我所做的很多阅读让我更加困惑,因为它非常复杂。
$ b
有些人似乎有简单比较的问题,但是我自己没有问题。
例如...
$ num1 = 27.64;
$ num2 = 27.64;
if($ num1 == $ num2){
echo'Good!';
} else {
echo'Bad!';
}
// Echo的好!
...和
$ num1 = 27.60;
$ num2 = 27.6;
if($ num1 == $ num2){
echo'Good!';
} else {
echo'Bad!';
}
// Echo's Good
...和
$ num1 = 27.60;
$ num2 = 57.60;
if($ num1 <= $ num2){
echo'Good!';
} else {
echo'Bad!';
}
// Echo's Good
...和
$ num1 = 25.00;
$ num2 = 12.50 + 12.5;
if($ num1 == $ num2){
echo'Good!';
} else {
echo'Bad!';
}
// Echo's Good
然后我看到页面如 http://patchlog.com/php/comparing-float-values-in -php / ,似乎有简单的问题,我不明白。
我只是想了解他是如何得到他的简单代码的问题但是我不是和我一起的。 那些值将是相同的 - 你分配相同的十进制文字到每个变量。比较这个代码:
$ num1 = 27.64;
$ num2 = 10.0 + 2.88 + 2.88 + 2.88 + 9.0;如果($ num1 == $ num2){
echo'Good!'; //在十进制算术中加27.64
;
} else {
echo'Bad!';
}
// Echo的Bad!
$ num2看起来应该是27.64,但它确实增加了类似于27.639999999999997015720509807579219341278076171875(这就是我当我在我的机器上的Visual C ++中进行计算时)。 $ num1 = 27.6400000000000005684341886080801486968994140625(在我的机器上),所以它们不同。
示例2
没有区别。
示例3
这些数字当然不在浮点容差内将会有所不同。
示例4
12.5正好可以浮点表示,所以12.5 + 12.5也是0.5是2 ^ -1)。
I know these questions may get asked a lot but from my reading and testing it had me confused a bit and a lot of the reading I have done has just confused me more as it is quite complex.
Some people seem to have issues with simple comparisons, however I have had no issues myself.
For example...
$num1 = 27.64;
$num2 = 27.64;
if ($num1 == $num2) {
echo 'Good!';
} else {
echo 'Bad!';
}
// Echo's "Good!"
...and
$num1 = 27.60;
$num2 = 27.6;
if ($num1 == $num2) {
echo 'Good!';
} else {
echo 'Bad!';
}
// Echo's Good
...and
$num1 = 27.60;
$num2 = 57.60;
if ($num1 <= $num2) {
echo 'Good!';
} else {
echo 'Bad!';
}
// Echo's Good
...and
$num1 = 25.00;
$num2 = 12.50 + 12.5;
if ($num1 == $num2) {
echo 'Good!';
} else {
echo 'Bad!';
}
// Echo's Good
Then I see pages like http://patchlog.com/php/comparing-float-values-in-php/ that seem to have simple issues and I don't get it.
I just want to understand how he is getting problems with his simple code but I am not with mine.
Example 1
Those values will be the same -- you assign the same decimal literal to each variable. Compare that to this code:
$num1 = 27.64;
$num2 = 10.0 + 2.88 + 2.88 + 2.88 + 9.0; //In decimal arithmetic adds to 27.64
if ($num1 == $num2) {
echo 'Good!';
} else {
echo 'Bad!';
}
// Echo's "Bad!"
$num2 looks like it should be 27.64, but it really adds to something like 27.639999999999997015720509807579219341278076171875 (that's what I get when I do that calculation in Visual C++ on my machine). $num1 = 27.6400000000000005684341886080801486968994140625 (on my machine), so they differ.
Example 2
The trailing 0 makes no difference.
Example 3
The numbers are not within the floating-point "tolerance" so of course will differ.
Example 4
12.5 is exactly representable in floating point, so 12.5 + 12.5 is too (0.5 is 2^-1).
这篇关于在php中了解浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!