Archived

This topic is now archived and is closed to further replies.

factorials, int limits and my scripting language ...

This topic is 5506 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi im creating a scripting language and to test the stack and efficiency of the VM, i wrote the factorial function .. but, the maximum number i can find the factorial of is 12! is this normal, is this acceptable? i thought it was an error, but it seems that its just the limit of the type "long", i even tried using __int64 in msvc, still, no go .. heres the C code:
  
#include <stdio.h>

typedef unsigned long ulong;

ulong factorial(ulong number) 
{
	ulong temp = 0;
	ulong next = 0;

	if(number <= 1) 
	   return 1;

	next = number - 1;
	temp = factorial(next);
	temp = number * temp;

	return temp;
}

int main(int argc, char **argv) 
{
	ulong number = 14;
	ulong ans = 0;

	ans = factorial(number);

	printf("%li\n",ans);
}
  
and heres the scripted version:
  
function: init() {

	var<long> number = 12;
	var<long> ans = 0;

	ans = factorial(number);

	print(ans);
}

function: factorial(var<long> number;) 
{
	var<long> temp = 0;
	var<long> next = 0;

	if(number <= 1) 
	   return 1;

	next = number - 1;
	temp = factorial(next);
	temp = number * temp;

	return temp;
}
  
advice, answers, tips .. anyone? thanx

Share this post


Link to post
Share on other sites