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 array_diff_key Function: How it Works with Examples

PHP array_diff_key compares arrays and removes elements that share the same key. It checks only keys and keeps the values…

How to Delete Data in PHP MySQL

In this tutorials, we are going to explain the PHP MySQL Delete Data process with a simple, unique guide. By…

PHP $_SERVER: Basics and Examples

PHP $_SERVER is a superglobal. It’s a predefined variable containing information about your server, client, and request environment. As a…

PHP array_filter: How to Filter Array Values with Examples

You can use array_filter in PHP to remove unwanted data from arrays. It works with a custom callback or default…

PHP is_dir Function: Check if a Directory Exists

Sometimes, when you run a script, you may encounter an error because the directory you're trying to access doesn't exist.…

PHP OOP Programming: A Complete Tutorial

A complex web application needs proper organization and maintenance to keep your code structured. This is where PHP Object-Oriented Programming…

PHP AND (&&) Operator: Usage & Examples

As you are entering into PHP, it would be essential to understand how logical operators work. The PHP AND operator…

PHP Hello World: Write Your First Program with Code Example

You always start with “Hello World” when you learn a new language. It keeps things simple. You see right away…

PHP $_GET: How to Create Dynamic URLs in PHP?

The PHP $_GET— is a tiny part, but strong in the data processing using the URL. There is a site that…

PHP abs Function: How to Get Absolute Values

The abs() function in PHP gives you a number without its sign — it always returns a positive value. That…

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.