Solved

What is the problem with 'double'?

Posted on 2004-04-12
5
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Mayank S
Mayank S 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:Mayank S
ID: 10810991
Glad to help ;-)
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.

730 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