File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Factorial of numbers

 
Ram Prasad
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Rob Spoor
Sheriff
Pie
Posts: 20399
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BigInteger is the way to go.
There is also a class called BigDecimal, but you shouldn't use that for whole numbers.
 
Pawel Nowacki
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also you may take a look at Fast Factorial Functions
Looks like its not so simple after all ... They discourage using "simple" recursing method
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic