home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!think.com!ames!agate!ucbvax!kopc.hhs.dk!ARNE
- From: ARNE@kopc.hhs.dk (Arne Vajhxj)
- Newsgroups: comp.os.vms
- Subject: Re: Vax C Int size problems?
- Message-ID: <01GTQQSHA1NM8WW3FQ@kopc.hhs.dk>
- Date: 20 Jan 93 12:20:24 GMT
- Sender: daemon@ucbvax.BERKELEY.EDU
- Distribution: world
- Organization: The Internet
- Lines: 60
-
- > I am in the process of writing what seemed to be an easy program to
- > calculate sin x's ans cos x's utilizing the Maclaurin series. The program works
- > rather well until I get to a point such that I need to find a factorial of a
- > large number as 17. I use the nice little recursive function listed here:
- >
- > factorial(n)
- > int n;
- > {
- > if (n == 1)
- > return (1);
- > else
- > return (n * factorial(n - 1));
- > }
- >
- > I have problems with the number (n) when it comes out of the function because a
- > regular int definition cannot hold the value. I thought perhaps a "long int n"
- > or something like that would remedy the situation, but to no avail. Is there a
- > good definition for a variable which is going to handle such a large number? Or
- > is it perhaps worse and (gasp) I'll need to find another handy-dandy algorythm
- > for finding the factorial of such a large number?
-
- No - there is nothing bigger than int (=long=long int) in VAX C. But that should
- not be a problem for you !
-
- 1) Change factorial from int to float or double !
-
- Then you will be able to go higher (33 instead of 15). If you use double
- and G_FLOATING you will be even better off.
-
- You will loose a little in precision, but it should not mean anything for
- the calculations.
-
- So:
-
- double factorial(int n)
- {
- if (n == 1)
- return (1);
- else
- return (n * factorial(n - 1));
- }
-
- 2) If speed means anything for you, then it will be much faster to have
- an initialized array containing the values.
-
- So:
-
- double factorial[33];
-
- 3) If you are interested in big integer arithmetic, then GNU C++ comes
- with an Integer class, which implements extremely large integers. It
- can easily be used for factorial calculation too !
-
- Arne
-
- Arne Vajhxj local DECNET: KO::ARNE
- Computer Department PSI: PSI%23831001304030::ARNE
- Business School of Southern Denmark Internet: ARNE@KO.HHS.DK
-
-
-