Solved

What is the problem with 'double'?

Posted on 2004-04-12
5
218 Views
Last Modified: 2010-03-31
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.
0
Comment
Question by:b612_forever
5 Comments
 
LVL 15

Assisted Solution

by:Javatm
Javatm earned 20 total points
ID: 10810092
This should solve your problem :

public static void main(String args[]) {

    DecimalFormat d1 = new DecimalFormat("0.0");

    for(double d = 1; d >= 0; d -= 0.2) {
    System.out.println(d1.format(d));
    }
}

Hope that helps . . .
Javatm
0
 
LVL 30

Assisted Solution

by:mayankeagle
mayankeagle earned 20 total points
ID: 10810540
Well, precision-related matters always come into picture while dealing with floats and sometimes also with doubles. You should never look for or compare exact values but keep a margin for error-tolerance while dealing with them.
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 20 total points
ID: 10810805
Remember that a computer is binary organised: many floating numbers can't be represented in float or double
like PI in the decimal system.
;JOOP!
0
 
LVL 1

Author Comment

by:b612_forever
ID: 10810958
Since Javatm provide the solution and mayankeagle,scuriware provide the explanation, I add 10 point for this question and split it to three.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10810991
Glad to help ;-)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now