views:

130

answers:

3

Hello all,

I have a problem concerned with losing of precision

my task is to print numbers as strings

int exponent = ...
int[] Mantissas = { 1, 2, 5 };
double dataStep = java.lang.Math.pow(10.0, exponent) * Mantissas[mantissaIndex];
...
for (int i = 0; i < NSteps; i++)
                    steps[i] = firstStep + i * dataStep;
draw(steps);

for example, 0.2*7=1.4000000000000001; 0.0000014/10=1.3999999999999998E-7

how to figure out this problem?

UPD: The main problem is string output formating. i don't bother about losting of about 0.00000001 value. Now I solved it as String.format("%f", value), but I think it's not good approach

+1  A: 

Search for "floating point numbers" on SO and you'll get a slew of answers as to why this happens. It has to do with how floating point numbers are represented in computers.

http://stackoverflow.com/questions/56947/how-is-floating-point-stored-when-does-it-matter

Another article on the matter - Floating Point Approximation

JasCav
+2  A: 

The double type does not have infinite precision and cannot represent decimals exactly. You are observing normal rounding errors. For arbitrary precision arithmetic, you will need to use java.math.BigDecimal instead.

Sean Owen
ok,java.math.BigDecimal m=new BigDecimal(1.4).setScale(1000); m=m.divide(new BigDecimal(10).setScale(1000)); System.out.println(m);the output is 0.1399999999999999911182158029987476766109466552734375;I want to print "0.14" on my chart control
2xMax
Don't use new BigDecimal(double) use either new BigDecimal(String) or BigDecimal.valueOf(double). new BigDecimal(double) will act exactly like a double.
ILMTitan
+1  A: 

As mentioned by others you have to use java.math.BigDecimal instead of float/double. This however comes with its own set of problems.

For instance when you call BigDecimal(double) the value you pass in will be expanded to its full representation:

BigDecimal oneTenth = new BigDecimal(0.1);
BigDecimal oneMillion = new BigDecimal(1000000);
oneTenth.multiply(oneMillion)
out> 100000.0000000000055511151231257827021181583404541015625000000

But when you use the BigDecimal(String) constructor the eact value is represented and you get

BigDecimal oneTenth = new BigDecimal("0.1");
BigDecimalr oneMillion = new BigDecimal(1000000);
oneTenth.multiply(oneMillion)
out> 100000.0

You can read more on BigDecimal's limitations in Joshua Bloch and Neal Gafter's splendid Java puzzlers book and in this informative article. Finally note that toString on BigDecimal's will print in scientific notation so you will properly have to use toPlainString instead.

Lars Tackmann