Difference between float and double in php?
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
MySQL: What's the difference between float and double?
They both represent floating point numbers. 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. This is used to store exact numeric data values, unlike floating point numbers, where it is important to preserve exact precision, for example with monetary data.
What is the difference between double() and float() methods in migraions in Yii2?
It doesn't do exactly the same.
Let's take a look at implementation of these methods:
public function float($precision = null)
{
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_FLOAT, $precision);
}
public function double($precision = null)
{
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_DOUBLE, $precision);
}
As you can see both methods uses different constant for creation of ColumnSchemaBuilder
instance. The value of constants is also different.
const TYPE_FLOAT = 'float';
const TYPE_DOUBLE = 'double';
So why is the result same for Postgres? The reason is in yii\db\pgsql\QueryBuilder
which maps both abstract types to same final type.
public $typeMap = [
// ...
Schema::TYPE_FLOAT => 'double precision',
Schema::TYPE_DOUBLE => 'double precision',
// ...
];
It's done this way because of abstraction. It allows you to write the migrations that will work no matter if the application runs on Postgres, MySQL or MSSQL.
But, I don't know why the authors of framework decided to map both abstract types to double precision
.
PHP 7.4 Typed property with floats / doubles
Why is PHP correctly enforcing the int type but not the float type?
Why is it that when I check with is_float() it returns false but the
function accepts an integer?
Because float range can contain integers without losing any data. In other words, float is like a superset of integers, so you can't say I only want floats i don't want integers even in strongly typed languages and even if your code is in strict_types
mode.
Why is the integer type perfectly enforced but not the float?
PHP type declaration comes with coercive mode as default mode, so it's possible to change the declared type and no TypeError will be thrown only if it's possible for PHP to coerce values of the wrong type into the expected ones.
In your case you are passing 8
(integer) to a method that expects a float, this is totally fine and PHP will not complain about it because coercing 8
to a float will not change anything/lose anything.
So what does strict_types
do then ?
It will change the behavior of PHP from coercive to strict. That means PHP will not be tolerant when an involved operation could lead to data loss.
By taking your example when we set declare(strict_types=1)
the following line will be a problem
$ft->setInt(8.2);//Works as expected, great!
Because we are trying to pass a float
number to an int
parameter which means a data loss (8.2 becomes 8 ) PHP will throw an exception
Fatal error: Uncaught TypeError: Argument 1 passed to
FloatTest::setInt() must be of the type int, float given
Even though PHP has a valid double data type, why does it assume
double is an instance? double is definitely a primitive data type
within PHP, as the is_double() function works perfectly
PHP has no double
data type and is_double
is just an alias of is_float()
Basically what would be the best, cleanest work around to enforcing
decimal numbers in PHP?
What you did is the best and cleanest work :)
PHP / difference between 2 float returns a very big result
1.7763568394003E-15
isn't a big number, it's actually very very small. It's being shown in scientific notation; it's 0.0000000000000017763568394003 in normal form. This is the error due to floating point representations.
The PHP manual states you should compare floats as such:
To test floating point values for equality, an upper bound on the
relative error due to rounding is used. This value is known as the
machine epsilon, or unit roundoff, and is the smallest acceptable
difference in calculations.
$a
and$b
are equal to 5 digits of precision.
<?php
$a = 1.23456789;
$b = 1.23456780;
$epsilon = 0.00001;
if(abs($a-$b) < $epsilon) {
echo "true";
}
?>
Perhaps the assertEquals
does something similar
Compare floats in php
If you do it like this they should be the same. But note that a characteristic of floating-point values is that calculations which seem to result in the same value do not need to actually be identical. So if $a
is a literal .17
and $b
arrives there through a calculation it can well be that they are different, albeit both display the same value.
Usually you never compare floating-point values for equality like this, you need to use a smallest acceptable difference:
if (abs(($a-$b)/$b) < 0.00001) {
echo "same";
}
Something like that.
Using double/float in php
You can use number_format :
$b = $del['Antal'];
$y = $info['Pris'];
$z = number_format($y * $b ,2);
The number_format
function :
string number_format ( float $number [, int $decimals = 0 ] )
And if you don't want the default format :
string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
Why float behaves differently between Python/PHP/Javascript and Java/C#
There is no difference between scripting languages and Java/C#. However, there is a difference between float
and double
. A small confusion comes from the fact that in the scripting languages (at least in Python) the underlying type of the float
normally has 64 bits precision (i.e., a double
in Java). The reason then, for the different behavior is that the closest value after rounding will not be the same, as can be seen from the following:
fl64(.1) == 0.10000000000000001
fl64(.2) == 0.20000000000000001
fl64(.3) == 0.29999999999999999
fl64(.1) + fl64(.2) == 0.30000000000000004
fl32(.1) == 0.1
fl32(.2) == 0.2
fl32(.3) == 0.30000001
fl32(.1) + fl32(.2) == 0.30000001
Thus, with the lower precision (32 bits) it just happens to be that 0.1 + 0.2 == 0.3
. This however is not a general result and for many other numbers this will not hold. Thus floats are still unreliable for exact precision.
Related Topics
PHP Script to Execute at Certain Times
Forbidden Error When Submitting Simple PHP Form
How to Send an HTML Email with an Inline Attached Image with PHP
File Download Script Doesn't Work When Called from Ajax
How to I Send Data from JavaScript to PHP and Vice Versa
How to Extract HTML Comments and All HTML Contained by Node
Replace Values in a Uri Query String
Azure Active Directory Sso with Laravel
Random Float Between 0 and 1 in PHP
Starting with Laravel on Ubuntu
Gmail Like File Upload with Jquery
How to Make Form_Rest() Not Display a Field with Symfony2
Difference Between MySQL_Fetch_Array and MySQL_Fetch_Row
Inspect Xml Created by PHP Soapclient Call Before/Without Sending the Request