PHP 7.x — P7: Floats/Doubles

Dino Cajic
Jun 21, 2020 · 3 min read
Image for post
Image for post

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;
?>

Floating points in PHP 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);?>

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Sign up for Best Stories

By Dev Genius

The best stories sent monthly to your email. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Dino Cajic

Written by

Author of An Illustrative Introduction to Algorithms. A Software Engineer with a B.S. in Computer Science, a minor in Biology, and a passion for learning.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Dino Cajic

Written by

Author of An Illustrative Introduction to Algorithms. A Software Engineer with a B.S. in Computer Science, a minor in Biology, and a passion for learning.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store