for(int i=n+1;i<=m;i++){
double r = (lm/mu);
// double res = (Math.pow(r, i))*(fact(m)/(fact(m-i)*fact(i)))*((Math.pow(n, n-i)*fact(i))/fact(n)) formulae
BigDecimal lr= new BigDecimal(Math.pow(r, i));
BigDecimal fnum = new BigDecimal(factorial(m));
BigDecimal fden1 = new BigDecimal(factorial(m-i));
BigDecimal fden2 = new BigDecimal(factorial(i));
fden1=fden1.multiply(fden2);
fnum=fnum.divide(fden1);
lr=lr.multiply(fnum);
BigDecimal nden = new BigDecimal(factorial(n));
BigDecimal inum = new BigDecimal(factorial(i));
BigDecimal mnum = new BigDecimal(Math.pow(n, n-i));
mnum = mnum.multiply(inum);
mnum = mnum.divide(nden);
lr = lr.multiply(mnum);
ssum2 = ssum2.add(lr);
sum2=Double.parseDouble(format(ssum2,40));
}
System.out.println("Total 2 is"+sum2);
Asked
Active
Viewed 89 times
-3

Madhawa Priyashantha
- 9,633
- 7
- 33
- 60

shravan
- 11
- 4
-
Can what be solved? What do you want it to do? – Neilos Apr 09 '15 at 12:54
-
Code only questions are frowned upon. provide a description of the expected behavior, and the error you are getting. – Davis Broda Apr 09 '15 at 12:56
-
1You should add a lot more information. Like what you want to do. – Nitram Apr 09 '15 at 12:56
-
Well `120!` would overflow, it's 6.6x10^198. How about including your `factorial` method? – weston Apr 09 '15 at 13:20
-
When you divide one factorial by another, you don't need to fully evaluate them both http://stackoverflow.com/questions/26311984/permutation-and-combination-in-c-sharp/26312275#26312275 – weston Apr 09 '15 at 13:28
1 Answers
0
You are parsing the result from a BigDecimal back into a double. If the BigDecimal is too big, the value is just Infinite because the double can' hold that much data.
Edit: The Infinity error could also occure at some other point in code and the error just gets propagated. Just check for unwanted cast from BigDecimal to Double.

Rene8888
- 199
- 2
- 12
-
double res = (Math.pow(r, i))*(fact(m)/(fact(m-i)*fact(i)))*((Math.pow(n, n-i)*fact(i))/fact(n)) this is the formuale i have implemented as u can see but it goes to infinity above 120.. i have tried it without parsing it gives me values but for the final computation its not working.. any idea on how to go about this other than avoiding parsing?. – shravan Apr 10 '15 at 05:15
-
You are using Math.pow which "only" calculates with double precision. If you want to calculate with bigger numbers you use BigDecimal. You have to convert your code to use BigDecimals. For example you can use `BigDecimal powedNumber = new BigDecimal(20).pow(1000) ;` to pow 20 by 1000. The result of this calculation would be too big to fit into a double. You can perform other caluclations with BigDecimal, for example multiply, divide, plus... – Rene8888 Apr 10 '15 at 07:23