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
Solved

What is the problem with 'double'?

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unhandled exception type Exception 18 42
Android development question 2 52
ejb entity bean example issue 2 16
java example issue 3 20
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

808 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