| Author |
Factorial of numbers
|
Ram Prasad
Greenhorn
Joined: Feb 02, 2008
Posts: 9
|
|
Hi Ranchers,
I am trying to write a function for factorial calculation. When i pass number more than 160 as input and trying to get the factorial, the return is Infinity
I am using a double literal to store the data and input is passed as integer n . I have taken the recursive approach
could someone suggest what data type i can use in order to get the factorial even for bigger numbers or correct me if i need to use other mechanism
My code looks like
Thank You
ARP
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16692
|
|
Take a look at the BigDecimal and BigInteger class.
An infinity value for double means that you exceeded the upper range for the type -- except for infinity, that is.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
BigInteger is the way to go.
There is also a class called BigDecimal, but you shouldn't use that for whole numbers.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Pawel Nowacki
Ranch Hand
Joined: Nov 14, 2008
Posts: 67
|
|
Also you may take a look at Fast Factorial Functions
Looks like its not so simple after all ... They discourage using "simple" recursing method
|
 |
 |
|
|
subject: Factorial of numbers
|
|
|