• Post Reply Bookmark Topic Watch Topic
  • New Topic

Factorial of numbers  RSS feed

 
Greenhorn
Posts: 9
  • 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
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 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
 
Sheriff
Posts: 21137
87
Chrome Eclipse IDE Java Windows
  • 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.
 
Ranch Hand
Posts: 67
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!