
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
Comparison of floating point values in PHP.n
In PHP, testing floating point values for equality is problematic, because PHP is failing when checking if one floating point number is equal to another. Despite the fact floating point numbers seems to have the same value do not need to actually be identical. So in this article will demonstrate the problem we are facing in comparison of Floating-Point Numbers and different procedures to avoid this problem.
Example
Let's test this with a simple example:
<?php $a = 0.14; $b = 1 - 0.86; //0.14 if($a == $b ){ echo 'a and b are same'; } else { echo 'a and b are not same'; } ?>
output:
a and b are not same.
Explanation:
In this code, the else condition is executed instead of the if condition, even though $a and $b are the same. It is due to the characteristic of floating-point values that the way they were represented internally. Let's discuss different cases in PHP to avoid this problem in real time.
Case1:
Demonstrate a comparison of floating point values by using the smallest difference in the calculation in computer systems.
Example:
<?php $val1 = 2.183459; $val2 = 2.183450; $epsilon = 0.00001; if(abs($val1 - $val2) < $epsilon) { echo "True"; } else { echo "False"; } ?>
Output:
True
Explanation:
Here we are using two floating point variables val1 and val2 alongside epsilon. Then we take the absolute difference of variables (val1 and val2) by utilizing the native function named as abs(). This code will give us the absolute value but the question is why we are taking the absolute values? The answer is we can see that both the values having the same digits after decimal up to precision value 5, Which is hard for PHP to analyze the comparison.
Case 2:
Use of round() function.
<?php $val1 = 9 - 6.2; $val2 = 1.8; var_dump(round($val1, 2) == round($val2, 2)); ?>
Output:
bool(true)
Explanation:
Here we are using two floating point variables val1 and val2. Then we are using the predefined function named as round () which is round off floating values up to two decimal places and then compare it. Here We are using the predefined round() function to get our expected result in the correct way.
- Related Articles
- Floating point comparison in C++
- PHP Floating Point Data Type
- Apply modulus operator to floating-point values in Java
- Comparison of dates in PHP
- Floating-point hexadecimal in Java
- Signed floating point numbers
- C++ Floating Point Manipulation
- Fixed Point and Floating Point Number Representations
- Floating-point conversion characters in Java
- Format floating point number in Java
- Decimal fixed point and floating point arithmetic in Python
- MySQL to get only the floating-point numbers from a list of values in a column
- What is the precision of floating point in C++?
- Floating point operators and associativity in Java
- What are floating point literals in C#?
