home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / os / vms / 21633 < prev    next >
Encoding:
Internet Message Format  |  1993-01-21  |  2.3 KB

  1. Path: sparky!uunet!think.com!ames!agate!ucbvax!kopc.hhs.dk!ARNE
  2. From: ARNE@kopc.hhs.dk (Arne Vajhxj)
  3. Newsgroups: comp.os.vms
  4. Subject: Re: Vax C Int size problems?
  5. Message-ID: <01GTQQSHA1NM8WW3FQ@kopc.hhs.dk>
  6. Date: 20 Jan 93 12:20:24 GMT
  7. Sender: daemon@ucbvax.BERKELEY.EDU
  8. Distribution: world
  9. Organization: The Internet
  10. Lines: 60
  11.  
  12. >         I am in the process of writing what seemed to be an easy program to
  13. > calculate sin x's ans cos x's utilizing the Maclaurin series. The program works
  14. > rather well until I get to a point such that I need to find a factorial of a
  15. > large number as 17. I use the nice little recursive function listed here:
  16. > factorial(n)
  17. >   int n;
  18. > {
  19. >   if (n == 1)
  20. >     return (1);
  21. >   else
  22. >     return (n * factorial(n - 1));
  23. > }
  24. > I have problems with the number (n) when it comes out of the function because a
  25. > regular int definition cannot hold the value. I thought perhaps a "long int n"
  26. > or something like that would remedy the situation, but to no avail. Is there a
  27. > good definition for a variable which is going to handle such a large number? Or
  28. > is it perhaps worse and (gasp) I'll need to find another handy-dandy algorythm
  29. > for finding the factorial of such a large number?
  30.  
  31. No - there is nothing bigger than int (=long=long int) in VAX C. But that should
  32. not be a problem for you !
  33.  
  34. 1) Change factorial from int to float or double !
  35.  
  36.    Then you will be able to go higher (33 instead of 15). If you use double
  37.    and G_FLOATING you will be even better off.
  38.  
  39.    You will loose a little in precision, but it should not mean anything for
  40.    the calculations.
  41.  
  42.    So:
  43.  
  44.       double factorial(int n)
  45.       {
  46.         if (n == 1)
  47.           return (1);
  48.         else
  49.           return (n * factorial(n - 1));
  50.       }
  51.  
  52. 2) If speed means anything for you, then it will be much faster to have
  53.    an initialized array containing the values.
  54.  
  55.    So:
  56.  
  57.       double factorial[33];
  58.  
  59. 3) If you are interested in big integer arithmetic, then GNU C++ comes
  60.    with an Integer class, which implements extremely large integers. It
  61.    can easily be used for factorial calculation too !
  62.  
  63.                                                           Arne
  64.  
  65. Arne Vajhxj                             local DECNET:  KO::ARNE
  66. Computer Department                     PSI:           PSI%23831001304030::ARNE
  67. Business School of Southern Denmark     Internet:      ARNE@KO.HHS.DK
  68.  
  69.  
  70.