In PHP, the size of a floating values is platform-dependent. Due to internal representation of floating point numbers, there might be unexpected output while performing or testing floating point values for equality. For example, see the following program.
Explanation: The output of this code is False which is very unpredictable but in PHP the value1 is not exactly 1.6 i.e it is coming from the difference between 8 and 6.4 which actually turned out to be 1.599999 that is why this statement turned out to be false.
How to resolve above problem?
Method 1 : For equality testing in floating point values, use the machine epsilon or we can call it smallest difference in calculation in computer systems.
Explanation: In this code use two floating point numbers value1 and value2 along with epsilon. Now take the absolute difference of values (value1 and value2) by using the predefined function named as abs(). This code will give the absolute value but the question is why we are taking the absolute values. You can see that both the values having same digits after decimal upto precision value 7. Which is very difficult for the system to analyse the comparison.
Method 2 : We can use round function in PHP.
- Comparing two dates in PHP
- Percent Change & Discounts - Comparing Quantities | Class 8 Maths
- Class 8 NCERT Solutions - Chapter 8 Comparing Quantities - Exercise 8.2
- Comparing two ArrayList In Java
- How to push a value based on matched value in PHP ?
- CSS Float
- Converting a string variable into Boolean, Integer or Float type in Golang
- Bootstrap | Float utilities with Examples
- Formatting float column of Dataframe in Pandas
- How to float three div side by side using CSS?
- How to make elements float to center?
- p5.js | float() function
- How to move H2 Beneath H1 by using only float property ?
- How to convert Int data type to Float in Golang?
- How to Convert string to float type in Golang?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.