0

The given values are

double double1 = 1.0814449990040142E7; double double2 = 7.0164302885665664E17;

result is double1 + double2 = 7.016430288674711E17 in Java

Manually calculated value is 701643028867471089

Can anyone resolve my issue.

3 Answers3

0

Double values are not accurate in Java, you should use the BigDecimal class, which has a lot better precision.

For more information refer to the documentation: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html

Tomaz Fernandes
  • 964
  • 1
  • 7
  • 12
  • Hi..we already use BigDecimal, facing Performance issue due to that choose alternate. is there any alternative to calculate accurate result. – Anil Kumar Karre Oct 23 '18 at 05:31
  • Floating point numbers are inherently imprecise in Java, as you can see in this link: https://wiki.sei.cmu.edu/confluence/plugins/servlet/mobile?contentId=88487676#content/view/88487676 Can’t you make up for the performance hit with any other optimization? – Tomaz Fernandes Oct 23 '18 at 05:40
  • This question has a lot of good insights on what you’re trying to do: https://stackoverflow.com/questions/611732/what-to-do-with-java-bigdecimal-performance – Tomaz Fernandes Oct 23 '18 at 05:45
0

just format it how you want

double double1 = 1.0814449990040142E7; 
double double2 = 7.0164302885665664E17;

System.out.printf("%.2f", double1 + double2);

output 701643028867471100.00

or

System.out.printf("%.0f", double1 + double2);
Scary Wombat
  • 41,782
  • 5
  • 32
  • 62
0

Unfortunately, this is a case of There Ain't No Such Thing As A Free Lunch.

Double gets its performance by using a compact format. Every double occupies exactly 64 bits. That allows very efficient storage and transfers between processor and memory, the use of registers to hold double values, and hardware implementation of arithmetic. A BigDecimal is a data structure whose size depends on the value being represented, but can be much bigger than 64 bits.

The price you pay for double's efficiency is that it can exactly represent only a subset of the numbers that BigDecimal can represent exactly. The closest doubles to your literals are 10814449.9900401420891284942626953125 and 701643028856656640. Their exact sum is 701643028867471089.9900401420891284942626953125. It is not itself the exact value of any double. It is bracketed by the doubles 701643028867470976 and 701643028867471104, the closer of which is the result you got.

For many purposes, doubles are close enough. For example, if your variable represents a measured physical quantity, the measurement error will dwarf the rounding error on converting to double. The numbers that can be expressed as terminating decimal fractions are no more important than other rationals and irrationals. In those cases, use double and round outputs to the number of digits justified by the input precision and accumulated rounding error.

Other cases, such as some financial calculations, require exact arithmetic on decimal fractions. Less often, decimal fractions have no special significance, but you need more precision than double can give you. In both those cases, BigDecimal is the right data type despite the performance cost.

Patricia Shanahan
  • 24,883
  • 2
  • 34
  • 68
  • We are trying to calculate accurate value as per C++ Code, but in JAVA this not able resolve. In C++ code they are using double, In Java we are using double is that get correct values ?, we are not get it out of this even though using BigDecimal. how can i solve this in JAVA . – Anil Kumar Karre Oct 23 '18 at 10:39
  • C++ allows the use of extra precision in intermediate values. Java does not. That makes C++ result less reproducible, but in some cases, depending on compiler and optimization choices, can get more precise answers. Do not count on it. The extra precision is always optional, and compiler dependent, not required by the standard. – Patricia Shanahan Oct 23 '18 at 12:54