0

Help please. I have a problem with the wrong calculations when using like double. For example, we use a code below:

double a = 2.01;
double b = 1.01;
double c = 100*(a - (int)a);
double d = 100*(b-(int)b);

In my log I see the following: c = 1.0000001 and d = 0.99999987. How to make so that calculations were more exact result, namely, 0.01? Thanks in advance for the help.

user
  • 86,916
  • 18
  • 197
  • 190
zharik86
  • 51
  • 1
  • 3

4 Answers4

0

Use Math.round(num) instead of int casting.

Ori Seri
  • 917
  • 1
  • 6
  • 14
  • That will not fix the underlying problem. Furthermore, if `a` i `2.51`, this will yield a negative number instead of a positive one, which doesn't seem to be be what the OP wants. – jerry Apr 20 '13 at 14:56
0

You could do your maths with BigDecimal.

alex
  • 6,359
  • 1
  • 23
  • 21
0

The problem is that doubles are binary-based and have limited precision. They can not exactly represent 2.01 or 1.01 (or almost all real numbers). This does not mean they're useless, often we don't care about small differences like this. If you do, you will have to use something else (like fixed point or BigDecimal)

jerry
  • 2,581
  • 1
  • 21
  • 32
0

Well, a computer doesn't think or store numbers like us. There are several ways of storing and accessing floating point numbers in a computer - its called numerical analysis; basically how floating point numbers are approximated.

https://en.wikipedia.org/wiki/Numerical_analysis

shiladitya
  • 2,290
  • 1
  • 23
  • 36