Results 1 to 5 of 5
  1. #1
    grilo is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default big double division

    The output of the following program is 0.8402000002094746 and it should be 0.8402.

    Anyone knows why? Is there any way to solve the problem?

    public class Main {

    public static void main(String[] args){

    double p = 0;
    double l = 10000000;
    double x = 0.8402;

    for(int i = 0; i < l; i++){
    p += x;
    }

    System.out.println(p / l);
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You're running into a general issue of how digital computers handle floating point numbers. I don't have any links handy, but you might want to google this topic to learn more about it as it is not a Java issue per se, but instead is a general digital computing issue (for instance, how do you precisely represent 3/7 in only binary? Think about this).

    There are two issues here, one is one of numeric accuracy, and the other regarding display of the number. The result that the program is outputting is accurate to at least 9 significant digits, and so it is reasonably accurate. To display it better, you may wish to look into formatting the String that displays the number either via NumberFormat (for example DeicmalFormat) or String.format (printf is one of its variants).

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    For instance, here are two ways of displaying a prettier output:
    Java Code:
    import java.text.DecimalFormat;
    
    public class Main {
    
      public static void main(String[] args) {
    
        double sum = 0;
        double bigNumber = 10000000;
        double littleNumber = 0.8402;
    
        for (int i = 0; i < bigNumber; i++) {
          sum += littleNumber;
        }
    
        DecimalFormat df = new DecimalFormat("0.####");
        System.out.println(df.format(sum / bigNumber));
        System.out.printf("%6.4f %n", sum/bigNumber);
      }
    }
    Also, sorry to be blunt, but your variable naming is poor at best since the variable names aren't self-describing in that they don't describe what the values represent, and dangerous at worst, since it is very easy to confuse the letter "l" with the number 1. Finally, please use code tags when posting here.

    Oh, and welcome!

  4. #4
    grilo is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default Thanks

    Thanks for the answer. I was concerned with accuracy since after many many such operations we can have a deviation that in some situations (critical systems, scientific calculations) may have some meaning.

    I'm sorry about the tags and the variable naming.

    Grilo

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    For critical floating point numbers such as currency, you should not use double but instead use BigDecimal. While it gives a significant performance hit, sometimes accuracy trumps speed.

Similar Threads

  1. Problem with division using doubles
    By chrismanahan in forum New To Java
    Replies: 3
    Last Post: 10-10-2009, 10:26 PM
  2. java division and decimal error
    By heartysnowy in forum New To Java
    Replies: 5
    Last Post: 10-07-2009, 05:57 PM
  3. Replies: 5
    Last Post: 03-21-2009, 10:32 PM
  4. how to discard remainder on division?
    By RobertF in forum New To Java
    Replies: 9
    Last Post: 03-13-2009, 01:20 PM
  5. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 09:39 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •