1

in some cases I need to do really precise calculations in Java, but it always have some unexpected errors. How can I avoid them or keep the error in a acceptable range?

e.g.

public static void main(String[] args) throws Exception {
    double x = 0.0;
    while (x <= 1.0){
        System.out.println(x);
        x += 0.1;
        System.out.println("add 0.1");
    }
}



- the result will be 

   0.0 add 0.1
   0.1 add 0.1
   0.2 add 0.1
   0.30000000000000004 add 0.1
   0.4 add 0.1
   0.5 add 0.1
   0.6 add 0.1
   0.7 add 0.1
   0.7999999999999999 add 0.1
   0.8999999999999999 add 0.1
   0.9999999999999999 add 0.1

which is not as expected.

Thanks in advance

Danielson
  • 2,605
  • 2
  • 28
  • 51

2 Answers2

5

You should use BigDecimal in these cases. It saves you from various deviations you will find working with float or double.

For more information, Double vs. BigDecimal?

http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial

Community
  • 1
  • 1
Aakash
  • 2,029
  • 14
  • 22
  • BigDecimal is usually a bit overkill when people are just confused by the unformatted output of doubles. – Kayaman Jun 24 '15 at 09:29
  • In first line, OP says, "in some cases I need to do really precise calculations in Java". – Aakash Jun 24 '15 at 09:36
  • I doubt he really does. He's not working in banking, that's for sure. – Kayaman Jun 24 '15 at 09:37
  • We should stick to answering the question, rather than assuming. I have answered as per the question. I have also provided with some links where OP can find more information. One of them clearly says that using `BigDecimal` is slow and is trickier to use. But who are we to decide what should be used where? OP needs some help, we provided it. That's it. – Aakash Jun 24 '15 at 09:40
  • Well, based on the way the OP phrased his question, it's obvious that he has trouble understanding the formatting, not the accuracy. You develop an eye for these things after you've seen this exact type of question a dozen times. – Kayaman Jun 24 '15 at 09:43
  • My answer was based on my understanding of question. I will not accept any more comments on that. You also have provided your answer based on your understanding. OP will use whatever he likes. – Aakash Jun 24 '15 at 09:49
  • I know it was based on your understanding. I commented to show you that the first understanding is not always the correct one. Often you need to dig the real question out of what has been posted, just because the OP doesn't exactly know what he should be asking for. – Kayaman Jun 24 '15 at 09:54
  • Thank you guys, I am just a student now. Maybe my question was not that professional which make you have some misunderstanding. My task now is a small project regarding financing, it should be really precise,so I think BigDecimal is exactly what I need now. Hopefully it will still be precise after a long chain of calculations. – Oliver Caffrey Jun 24 '15 at 10:44
  • Thanks @OliverCaffrey to clarify the confusion. Yes, `BigDecimal` is guaranteed to be accurate, irrespective of number of calculations since it is immutable. Please mark this answer correct if it solved your question. – Aakash Jun 24 '15 at 11:03
1

Rather than using BigDecimal you should probably just format your double when you're printing it out.

Replace your println with printf("%.1f\n", x); and you'll get the output you're looking for.

BigDecimal is needed when you absolutely need to have precise accuracy during complex calculations (such as when money is involved).

Kayaman
  • 72,141
  • 5
  • 83
  • 121