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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Glad to help ;-)
ASKER