Link to home
Start Free TrialLog in
Avatar of b612_forever
b612_forever

asked on

What is the problem with 'double'?

Here I attach the code segment and it output:

Code:

public static void main(String args[]){
    for(double d = 1; d >= 0; d -= 0.2){
        System.out.println(d);
    }
}

Output:

1.0
0.8
0.6000000000000001
0.4000000000000001
0.20000000000000007
5.551115123125783E-17

Why is this happened? and How to solve this problem if I am expecting the output of:

1.0
0.8
0.6
0.4
0.2
0.0

Thank You.
SOLUTION
Avatar of Javatm
Javatm
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of b612_forever
b612_forever

ASKER

Since Javatm provide the solution and mayankeagle,scuriware provide the explanation, I add 10 point for this question and split it to three.
Glad to help ;-)