Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between float and double in php?

Tags:

php

People also ask

What is difference between float and double?

A float has 7 decimal digits of precision and occupies 32 bits . A double is a 64-bit IEEE 754 double-precision floating-point number. 1 bit for the sign, 11 bits for the exponent, and 52 bits for the value. A double has 15 decimal digits of precision and occupies a total of 64 bits .

What is a double in PHP?

Variables can store data of different types, and different data types can do different things. PHP supports the following data types: String. Integer. Float (floating point numbers - also called double)

What is the difference between float decimal and double?

Float represent data with single precision. Double represent data with double precision. Decimal has higher precision than float and Double.

What is the difference between float and double in SQL?

A FLOAT is for single-precision, while a DOUBLE is for double-precision numbers. MySQL uses four bytes for single-precision values and eight bytes for double-precision values. There is a big difference from floating point numbers and decimal (numeric) numbers, which you can use with the DECIMAL data type.


There is no difference in PHP. float, double or real are the same datatype.

At the C level, everything is stored as a double.
The real size is still platform-dependent.

See the manual for more details:
http://www.php.net/manual/en/language.types.float.php


For PHP, they are the same. 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:

" double " (for historical reasons "double" is returned in case of a float , and not simply "float")


As of PHP 7.0.6 on Windows, comparing this command without xdebug:

$ php -r 'var_dump(28.4);'
float(28.4)

and with 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.

This may address some concerns why you see double instead of float shown in var_dump in some other machines.

Also, with or without xdebug, gettype still returns string(6) "double".


In 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".

function test(float $a) {
    var_dump($a);
}
test(2.2111);

Prints 2.2111 to the screen.