By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,645 Members | 1,328 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,645 IT Pros & Developers. It's quick & easy.

java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representa

P: 20
here is my code.but there is something wrong when I using BigDecimal class.
Expand|Select|Wrap|Line Numbers
  1. package javaapplication30;
  2.  
  3. import java.math.BigDecimal;
  4.  
  5. public class Main {
  6.  
  7.   public static void main(String[] args) {
  8.    Main a=new Main();
  9.      BigDecimal sum=new BigDecimal(0);
  10.     for(int i=3;i<=100;i++){
  11.  
  12.             sum=a.R(i).add(sum);
  13.  
  14.     }System.out.println(sum);
  15.  
  16. }
  17.  
  18. BigDecimal R(int m){
  19.      int p,q;
  20.         BigDecimal sum=new BigDecimal(0);
  21.     for(p=1;p<=m;p++){
  22.         for(q=p+1;q<=m;q++){
  23.             if(p+q>=m){
  24.                 if(isCoprime(p,q)){
  25.                                     BigDecimal x = new BigDecimal(1);
  26.                                 BigDecimal y = new BigDecimal((p*q));
  27.                                 BigDecimal result=x.divide(y); 
  28.                 sum=sum.add(result);
  29.                                 }    
  30.             }
  31.         }
  32.     }
  33. return sum;    
  34. }
  35. boolean isCoprime(int p,int q){
  36.     int i;
  37.     if(p==1)return true;
  38.     if(p==q-1)return true;
  39.     if(q%p==0)return false;
  40.     if(p==3)return true;
  41.     for(i=2;i*i<=p;i++){
  42.      if(p%i==0){
  43.          if(q%i==0||q%(p/i)==0)break;
  44.      }    
  45.     }if(i*i<=p+1)return false;
  46.     return true;
  47. }
  48. }
here is output.please help me...
Expand|Select|Wrap|Line Numbers
  1. init:
  2. deps-jar:
  3. Compiling 1 source file to C:\Users\seeker-PC\Documents\NetBeansProjects\JavaApplication30\build\classes
  4. warning: [options] bootstrap class path not set in conjunction with -source 1.5
  5. 1 warning
  6. compile:
  7. run:
  8. 0.5
  9. Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
  10.         at java.math.BigDecimal.divide(BigDecimal.java:1616)
  11.         at javaapplication30.Main.R(Main.java:27)
  12.         at javaapplication30.Main.main(Main.java:12)
  13. Java Result: 1
  14. BUILD SUCCESSFUL (total time: 0 seconds
Oct 20 '13 #1

✓ answered by Nepomuk

The problem is when you divide 1 by 3; the resulting expression cannot be represented by a finite number of decimal places (as it is 0.333333333...). To solve this you have to pass a scale to which the result should be rounded and a rounding mode. So this would mean using this divide method, which would look something like this:
Expand|Select|Wrap|Line Numbers
  1. BigDecimal result = x.divide(y, 3, BigDecimal.ROUND_HALF_UP);

Share this Question
Share on Google+
2 Replies


Expert 100+
P: 785
Expand|Select|Wrap|Line Numbers
  1. BigDecimal result=x.divide(y);
Let's say for example x=10, y=3
The result would be 3.33333333333333333333333.....
The error message says, this cannot be represented as a decimal number.
Oct 21 '13 #2

Nepomuk
Expert 2.5K+
P: 3,112
The problem is when you divide 1 by 3; the resulting expression cannot be represented by a finite number of decimal places (as it is 0.333333333...). To solve this you have to pass a scale to which the result should be rounded and a rounding mode. So this would mean using this divide method, which would look something like this:
Expand|Select|Wrap|Line Numbers
  1. BigDecimal result = x.divide(y, 3, BigDecimal.ROUND_HALF_UP);
Oct 21 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.