Need help on calculating a number Factorial


I’m trying to calculate 100! on a MacBook Pro and the answer came out to be different than others. I’m wondering if i used wrong primitive data type. Below is my code:

long double total = 1;

for (int i = 100; i>= 1; i--){

  total = total * i;

 NSLog(@"The total sum is %Lf", total);

Any reason you used a double and not an int?

Some languages have trouble with doubles — they add tiny bits here and there (PHP for one is not always easy with doubles – depending on platform, ect… I have to round out my doubles in order to keep them tidy – e.g. I would do 2.5*6 and get 14.9999999999967 or something like that). Perhaps that is what is happening here. Even if a tiny bit of variance was introduced here, it could result in huge differences.

Not sure if that’s what’s happening here though.