PHP Float: Understanding Float Precision in PHP

A PHP float is a kind of number that has a decimal point, like 2.45 or 0.11. It’s also called a ‘double’ or ‘real number’.

It may be used in positive numbers or negative numbers, such as 3.14159 or -0.005.

In the following section, we will explain the precision of PHP floats and their limitations. Let’s move forward.

php float

PHP Float Precision and Limitations

PHP float is used to represent numbers with fractional components.

Moreover, it is represented as double-precision floating-point numbers, which can store numbers with up to 14 digits of precision.

Usually, the range of float values in PHP depends on the platform, which spans from approximately -2.2250738585072E-308 to 1.7976931348623E+308.

So to show the minimum and maximum values of floats, use the following constants:

  • PHP_FLOAT_MIN
  • PHP_FLOAT_MAX

Let’s put them in a PHP example:

echo PHP_FLOAT_MIN // 2.2250738585072E-308
echo PHP_FLOAT_MAX // 1.7976931348623E+308

Anyway, here are some examples of floating numbers:

5.5
12.25
-10.17

Additionally, PHP converts very large floating numbers or very small floating numbers to scientific notation.

For example, a very small number like 0.000000000000000157 is written in scientific notation as 1.57E-16. And the very big number, like 1,797,693,134,862,315.7, is written as 1.7976931348623157E+15.

In PHP 7.4, you use underscores in long numbers to make them more readable. For example, you can write a number like 2_419.279_17.

Anyway, there are some considerations when you use comparisons in a floating number. Let’s see why it doesn’t work.

Why Close Isn’t Always Equal in Floating-Point Comparisons?

When you perform an operation between a decimal number and a floating number, you can use the result in a comparison, but be mindful of potential precision issues with floating-point numbers.

Check out the following example:

$x = 12.6;
$y= 10;
$z = $x - $y; // 2.6
Var_dump( $z == 2.6 ); // false

When you perform the operation $x - $y in PHP, with $x has 12.6 and $y has 10, one would logically expect the result to be exactly 2.6.

However, a surprising result occurs when this result is compared to the literal value of 2.6 using the == operator. The comparison is false.

The root cause of this discrepancy is the way the numbers are stored in memory, especially when the two numbers are floating or decimal. Basically, operating on a binary system struggles to represent certain decimal numbers with perfect accuracy.

So, the main number may be 2.5999999999999999 and not 2.6.

To solve this problem, just use the PHP built-in function round to refine floats with perfect accuracy.

$x = 12.6;
$y= 10;
$z = $x - $y; // 2.6
Var_dump( round($z, 2) == 2.6 ); // true

Let’s see more examples of floating numbers.

Examples of PHP Floats

PHP lets you do math with floats, which are numbers with decimals. You can add, multiply, and divide them. Like this:

$a = 3.14;
$b = 2.71;

// Addition
$c = $a + $b; // 5.85

// Subtraction
$d = $a - $b; // 0.43

// Multiplication
$e = $a * $b; // 8.5094

// Division
$f = $a / $b; // 1.1590517223072

You can also perform more advanced operations on floats in PHP, such as exponentiation, modulus, and rounding. For example:

// Exponentiation
$g = pow($a, $b); // 36.977291374682

// Modulus
$h = $a % $b; // 0.43

// Rounding
$i = round($a, 1); // 3.1
$j = round($b, 1); // 2.7

Anyway, let’s summarize it in a few points

Wrapping Up

In this tutorial, you learned what a float is. You also understood some tricks for comparing floats and decimal numbers.

Anyway, here is a summary of what we covered:

In the first section, we basically defined the PHP float as numbers that have a decimal point in a specific position, like (3.14) or (-0.005).

They can be positive or negative. Additionally, it is precise and enables you to put up 14 digits after the decimal point.

You also understood how to get the minimum and maximum number in PHP float using these two constants: PHP_FLOAT_MIN and PHP_FLOAT_MAX.

You can also represent very large or small float numbers in scientific notation, such as by converting (0.000000000000000157) into (1.57E-16).

Similar Reads

PHP Spread Operator: Understand How (…) Syntax Works

Before the spread operator, PHP developers had to pass array items to a function using extra steps. They often had…

PHP Singleton Pattern: How to Use with Examples

The PHP singleton design pattern makes sure that the class has only one instance and provides a global access point…

PHP MySQL Create Database: MySQLi & PDO Examples

If you are working with PHP and MySQL, one of the first tasks you may need is to create a…

PHP Null Coalescing Operator: Handling Null Values

The PHP null coalescing operator, written as ??, is a helpful feature added in PHP 7. It makes it easier…

PHP file_exists: Check File and Directory Existence

Whether you are just trying to keep your app from crashing or making sure your users’ uploads don’t accidentally overwrite…

PHP array_chunk(): Split Arrays into Manageable Parts

When working with big arrays in PHP, things can get messy. That is where php array_chunk() steps in by providing…

PHP if-elseif Statement: ‘elseif’ versus ‘else if’

PHP’s if-elseif statement is a fundamental construct that allows developers to control the flow of their code based on different conditions. In…

PHP array_unshift: How to Add Values to the Start of an Array

PHP array_unshift helps you add new elements to appear before the existing ones. Understand the array_unshift function in PHP The…

How to Install PHP on a Raspberry Pi

You can use your Raspberry Pi as a personal web server or tool that runs in your home. You do…

PHP Type Hints: Ensuring Data Integrity

PHP-type hinting is something that keeps your code in check and helps your functions receive just the right types of…

Previous Article

PHP Integers Guide: Types, Limits, and Conversions

Next Article

Master PHP Iterables: Arrays, Objects, Traversable Interface

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.