php php中float和double的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3280892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between float and double in php?
提问by Centurion
I have this code
我有这个代码
$vad = 1.1;
print gettype($vad);
var_dump($vad);
this will output:
这将输出:
double
float(1.1)
So it is double or float in php?
所以它在php中是double还是float?
回答by Macmade
There is no difference in PHP. float, doubleor realare the same datatype.
PHP 没有区别。float,double或者real是相同的数据类型。
At the C level, everything is stored as a double.
The real size is still platform-dependent.
在 C 级别,所有内容都存储为double.
实际大小仍然取决于平台。
See the manual for more details:
http://www.php.net/manual/en/language.types.float.php
有关更多详细信息,请参阅手册:http:
//www.php.net/manual/en/language.types.float.php
回答by Victor Nicollet
For PHP, they are the same. http://www.php.net/manual/en/language.types.float.php:
对于 PHP,它们是相同的。http://www.php.net/manual/en/language.types.float.php:
Floating point numbers (also known as "floats", "doubles", or "real numbers") can be specified using any of the following syntaxes: [...]
可以使用以下任何语法指定浮点数(也称为“浮点数”、“双精度数”或“实数”):[...]
The confusing part is why gettype (which you shouldn't use, anyway) returns "double" instead of "float". The answer is http://de2.php.net/manual/en/function.gettype.php:
令人困惑的部分是为什么 gettype(无论如何你都不应该使用它)返回“double”而不是“float”。答案是http://de2.php.net/manual/en/function.gettype.php:
" double " (for historical reasons "double" is returned in case of a float , and not simply "float")
“ double ”(由于历史原因,在 float 的情况下返回“double”,而不仅仅是“float”)
回答by SOFe
As of PHP 7.0.6 on Windows, comparing this command withoutxdebug:
从 Windows 上的 PHP 7.0.6 开始,在没有xdebug 的情况下比较此命令:
$ php -r 'var_dump(28.4);'
float(28.4)
and withxdebug:
并使用xdebug:
$ php -r 'var_dump(28.4);'
Command line code:1:
double(28.4)
Note that this only changes var_dump() output, but not the actual memory management.
请注意,这只会更改 var_dump() 输出,而不会更改实际的内存管理。
This may address some concerns why you see doubleinstead of floatshown in var_dump in some other machines.
这可能会解决一些问题,为什么您会在其他机器的 var_dump 中看到double而不是float显示。
Also, with or without xdebug, gettypestill returns string(6) "double".
此外,无论有没有 xdebug,gettype仍然返回string(6) "double".
回答by Oboroten
In PHP 7.0.14
在 PHP 7.0.14 中
function test(double $a) {
var_dump($a);
}
test(2.2111);
Returns "Fatal error: Uncaught TypeError: Argument 1 passed to test() must be an instance of double, float given".
返回“致命错误:未捕获的类型错误:传递给 test() 的参数 1 必须是 double 的实例,给定的浮点数”。
function test(float $a) {
var_dump($a);
}
test(2.2111);
Prints 2.2111 to the screen.
在屏幕上打印 2.2111。

