This content originally appeared on Level Up Coding - Medium and was authored by Serhii Riabokon
Recently I went through a code review and was asked for inconsistency in using double and integers values interchangeably in unit tests. “You should have stick to one type. There is no reason for using integers here!” — said the reviewer.

Well, it is not that obvious, but when you are using data types with a floating point you might get into peculiar errors in comparing numbers due to an internal representation of its decimal parts. Let me illustrate by an example.
let a = 0.5;
let b = 0.5;
console.log(a == b); // true
Looks good with this one. Results to true as expected. However, an internal representation of floats is not 0.5 as it’s been written in the code, but rather somewhere 0.5000000000034. In other words, an actual number is nearly 0.5 with some small extra value near the last digits.
With an explicit value initialisation as in the case above, you might not catch the extra digits at first. But let’s rewrite 0.5 as a series of 0.01:
let a = 0.0;
for (let i = 0; i < 50; i++) {
a += 0.01;
}
let b = 0.5;
console.log(a);
console.log(b);
console.log(a == b);
Now if you run the code the output would be somewhere like:
0.5000000000000002
0.5
false
So, instead of expected 0.5 the value is 0.5000000000000002. And that is the case for all programming languages, not only JavaScript. Consider the following Java snippet:
class T {
public static void main(String[] argsc) {
float a = 0.25f + 0.25f;
float b = 0.5f;
System.out.println(a == b); // true
}
}That would result to true (at least in my case), but again if 0.5 is represented as a longer series of small values:
class T {
public static void main(String[] argsc) {
float a = 0.0f;
for (int i = 0; i < 50; i++) {
a += 0.01;
}
float b = 0.5f;
System.out.println(a == b);
}
}That would lead to false as a result of comparison! Hence, direct comparing of floats (doubles) for equality should have to be avoided as an error-prone.
The simplest solution is to use an explicit conversion to integers (long) with multiplication to power of 10:
class T {
public static void main(String[] argsc) {
float a = 0.0f;
for (int i = 0; i < 50; i++) {
a += 0.01;
}
float b = 0.5f;
int timesA = (int) a * 10;
int timesB = (int) b * 10;
System.out.println(timesA == timesB);
}
}In this case the result would true as expected.
Unexpected pitfalls of comparing floats was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Serhii Riabokon
Serhii Riabokon | Sciencx (2022-12-01T03:05:01+00:00) Unexpected pitfalls of comparing floats. Retrieved from https://www.scien.cx/2022/12/01/unexpected-pitfalls-of-comparing-floats/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.