Results 1 to 5 of 5
Thread: big double division
- 10-11-2009, 07:50 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 2
- Rep Power
- 0
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);
}
}
-
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).
-
For instance, here are two ways of displaying a prettier output:
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.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); } }
Oh, and welcome!
- 10-11-2009, 09:37 PM #4
Member
- Join Date
- Oct 2009
- Posts
- 2
- Rep Power
- 0
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
-
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
-
Problem with division using doubles
By chrismanahan in forum New To JavaReplies: 3Last Post: 10-10-2009, 09:26 PM -
java division and decimal error
By heartysnowy in forum New To JavaReplies: 5Last Post: 10-07-2009, 04:57 PM -
non-static method add(double,double) cannot be referenced from a static context
By cravi85 in forum Java SoftwareReplies: 5Last Post: 03-21-2009, 09:32 PM -
how to discard remainder on division?
By RobertF in forum New To JavaReplies: 9Last Post: 03-13-2009, 12:20 PM -
Double.valueOf() vs Double.parseDouble()
By greenbean in forum New To JavaReplies: 10Last Post: 01-12-2009, 08:39 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks