Jump to content
  • Advertisement

Archived

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

silvermace

factorials, int limits and my scripting language ...

This topic is 5811 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
Advertisement
Guest Anonymous Poster
13! > 2^32 > 12!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!