We already have one challenge on this concept. Whoever follows the publication might be able to solve this challenge. This challenge is based on CachedInteger. Here is the old challenge.
I will just mark down the answer, please go to the challenge for an explanation.
Challenge Your Java Skill
Thanks.
Please follow the publication.
1. 1
2. 0.9999999999999999
3. 1.0000000000000001
4. 0.9999999999999999D
Try to solve the challenge by yourself first.
The answer should be 1
but that’s not the case the answer is 0.9999999999999999
. Let’s see the bytecode —
public class JavaChallenge38 {
public JavaChallenge38() {
}
public static void main(String[] args) {
System.out.println(0.9999999999999999D);
}
}
So, at compile time only all 0.1
will be added, but it’s weird; so many of you will get a question —
Is floating point math broken???
Please go through the following links, it will help you to understand the entire concept —
Thanks. Don’t forget to upvote.