Solved

What is the problem with 'double'?

Posted on 2004-04-12
5
257 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

624 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