PHP Floats - Doubles

Time to take a dip in the Pool. Where’s my PHP Float?

Floats and doubles are the same thing in PHP. The following numbers are considered floating point numbers.

 

<?php
$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
?>

 

PHP Floats have precision for up to 14 digits. That includes the cumulative digits, before and after the period. We’ll create two variables with 14 total digits and one with 15 digits. We’ll add them and see the results that we get.

 

<?php
// 14 digits
$a = 1.0000000000001;
$b = 0.0000000000001;// 15 digits
$c = 0.00000000000001;echo "A: " . $a . "<br>";
echo "A + B: " . $a + $b . "<br>";
echo "A + C: " . $a + $c . "<br>";?>

 

The results that we get are:

A: 1.0000000000001;
A + B: 1.0000000000002;
A + C: 1.0000000000001;

The first two results are correct, but the last one, A + C, is not. The result should be: 1.00000000000011, and yet the last 1 is clipped off. Since the maximum precision of a floating point number is 14, the 15th number is truncated.

If we modify the last number in variable $c to equal 6 instead of 1, the new result A + C will equal 1.0000000000002.

 

<?php
// 14 digits
$a = 1.0000000000001;
$b = 0.0000000000001;// 15 digits
$c = 0.00000000000006;echo "A: " . $a . "<br>";
echo "A + B: " . $a + $b . "<br>";
echo "A + C: " . $a + $c . "<br>";?>

 

A: 1.0000000000001;
A + B: 1.0000000000002;
A + C: 1.0000000000002;

There are also discrepancies when it comes to floating point comparisons. This is best illustrated with an example.

 

<?php$x = 8 - 6.4; // Should be 1.6
$y = 1.6;if ($x == $y) {
  echo "They equal";
} else {
  echo "They don't equal";
}?>

 

If we run the code, it displays “They don’t equal.” They should, since 8 minus 6.4 is 1.6. This is not a PHP problem; it’s a problem with the way that floating point numbers are stored in computers in general.

I know that we haven’t covered functions yet, but I feel obligated to discuss them briefly since this is an article on floating point numbers. There will be a series of articles dedicated to functions later on.

PHP 7 allows for type hinting. Inside your parameter declaration, you can specify what type of data type the function will accept through its parameter. Float is the acceptable type hint declaration that’s allowed in PHP; double is treated as a class name. If you do try to type hint with double, you’ll get an error. Here’s an example of type hinting using the floating point number.

 

<?phpfunction does_if_float( float $a ) {
  var_dump( $a );
}does_it_float(1.0001);?>

 

 

PHP Tutorial Series

Continue your learning with these articles

PHP Integers

You Know, Integers. Like Math.

PHP – P6: INTEGERS

The integer data type represents whole numbers, either positive or negative or zero, not fractions.

PHP Floats - Doubles

Time to take a dip in the Pool. Where’s my Float?

PHP – P7: Floats/Doubles

Floats and doubles are the same thing in PHP. Floating points in PHP have precision for up to 14 digits. That includes the cumulative digits, before and after the period.

PHP Strings

WHAT DO YOU USE YOUR STRINGS FOR?

PHP – P8: STRINGS

A string is just a sequence of characters surrounded by quotes; strings in PHP are placed inside either single or double quotes. In other programming languages, like Java, strings are surrounded by double quotes and individual characters are surrounded by single quotes.

Leave a Reply