Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP数学奇怪的行为_Php - Fatal编程技术网

PHP数学奇怪的行为

PHP数学奇怪的行为,php,Php,我的代码中有幻影bug。在10次尝试中有一次出现。当我试着调试它时,我发现了这种奇怪的行为: $a = floor(1385968017.8665 * 10000); // or (int) (1385968017.8665 * 10000) // here $a equals to 13859680178664 $b = (1385968017.8665 * 10000); // here $b equals to 13859680178665 我有这样的PHP版本/配置: PHP 5.3.

我的代码中有幻影bug。在10次尝试中有一次出现。当我试着调试它时,我发现了这种奇怪的行为:

$a = floor(1385968017.8665 * 10000); // or (int) (1385968017.8665 * 10000)
// here $a equals to 13859680178664
$b = (1385968017.8665 * 10000);
// here $b equals to 13859680178665
我有这样的PHP版本/配置:

PHP 5.3.10-1ubuntu3.8 with Suhosin-Patch (cli) (built: Sep  4 2013 20:00:51) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
更新: 我知道浮动操作进动。C++中的相同代码:

#include <iostream>

int main()
{
    std::cout << std::fixed << (1385968017.8665 * 10000) << std::endl;
    std::cout << (unsigned long long) (1385968017.8665 * 10000) << std::endl;

    return 0;
}
这是我期待的。但为什么我的PHP代码显示(138596817.8665*10000)精确地等于1385968178665

更新2: 正如用户@core1024在评论中提到的,PHP可能是计算结果的四舍五入(138596817.8665*10000)。如果尝试
php-r'printf(“%f\n”,138596817.8665*10000);'结果将在大约
13859680178664.998047
的某个地方。谁能解释PHP何时以及为什么这样做?

它按预期运行

您在第一条语句中使用了
FLOOR()
函数,该函数将一个值向下舍入为最接近的整数

查看更多信息


至于为什么它会这样舍入,这是因为你的值
138596817.8665
并不是那么精确。Jon Skeet解释说..和小马Tony一起。

那是因为138596817.8665是二进制的无限双精度, 和(文字138596817.8665)~=(计算机中为138596817.8664998)


您需要的是圆形(138596817.8665*10000)吗?

我找到了这个问题的解释。我的php.ini精度设置等于14。如果将精度设置增加1,则不表示此stragne行为:

$a = floor(1385968017.8665 * 10000); // or (int) (1385968017.8665 * 10000)
// here $a equals to 1385968017866(!)5

在这两种情况下我都得到1385968178700。。。Apache/2.2.8(Win32)PHP/5.2.6看看这里,无论哪种情况,PHP都在舍入它。请尝试
php-r'printf(“%f\n”,138596817.8665*10000);'检查我的答案,特别是通过链接(链接到Jon Skeet的博客)。希望你能明白为什么这些值会这样输出。不幸的是,我不知道为什么。如果我知道的话,我会把它作为一个答案贴在这里:)在这种情况下,这一轮给了我正确的结果。但我总是想在不进行浮点运算的情况下,得到小于或等于“理想除法结果”的最近整数值。使用round不会将我从代码中的幻影bug中解救出来。
$a = floor(1385968017.8665 * 10000); // or (int) (1385968017.8665 * 10000)
// here $a equals to 1385968017866(!)5