File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Factorial of numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Factorial of numbers" Watch "Factorial of numbers" New topic
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: 18135
    
  39


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: 19552
    
  16

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 - OCEEJBD 6
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Factorial of numbers
 
Similar Threads
Cannot properly get ints from String Array
Incorrect Output !
BigInteger Power/Exponent BigInteger
Nested Loops
Exception in thread "main" java.