home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / math / verylarg / vlncls.txt < prev   
Encoding:
Text File  |  1993-10-22  |  10.2 KB  |  281 lines

  1. Very Large Numbers - VLN
  2.  
  3. This Object Class allows the user to compute to very high
  4. precision.  The unique feature of this Object class is that
  5. numbers are supported  up to 1000 decimal digits or more,
  6. limited only by available memory.   While only integer data
  7. is supported, scaled arithmetic allows  numbers to be
  8. used as small as 1 x 10 ^ -1000 or smaller.
  9.  
  10. Several basic operations are provided such as add,
  11. subtract, multiply and divide.  Further useful
  12. operations include 2^n, 10^n, nth-power  and nth-Root.
  13. Write Hexadecimal and Write Decimal procedures are included.
  14.  
  15.     Object definition:
  16. ))))))))))))))))))))))))))))))))))))))))))
  17. ))                    ))
  18. ))    pWordArray = ^tWordArray             ))
  19. ))    tWordArray = object        ))
  20. ))       array[1..vlsize] of word;    ))
  21. ))      end;                ))
  22. ))                    ))
  23. ))      pVryLrgNo =^tVryLrgNo;        ))
  24. ))      tVryLrgNo = object        ))
  25. ))        count : integer;        ))
  26. ))        max : integer;                ))
  27. ))        sign : integer;        ))
  28. ))        tVLN : tWordArray;        ))
  29. ))                    ))
  30. ))        procedures and functions.    ))
  31. ))       end;                ))
  32. ))                    ))
  33. ))))))))))))))))))))))))))))))))))))))))))
  34.  
  35. One problem in defining this data type, that can contain
  36. more than a thousand bytes, is that there is no best size
  37. for 'vlsize' the maximum allowed size.  Some applications 
  38. will need a few numbers of immense size while others will 
  39. need many but not so big. 
  40.  
  41. The user, if he does nothing special  may use the default size 
  42. of 1000 words (which allows 5300 decimal digits) for all variables
  43.  in his program.
  44. A way is illustrated to use less memory  assuming he knows how large the numbers will actually be.  This method uses
  45.     GETMEM( name, size)
  46.  
  47. In the process of initializing a VLN variable, the programmer also
  48. specifies 'MAX' the maximum size expected and this is tested in
  49. all arithmetic procedures.
  50.  
  51. The main program is responsible for  OpenTempRegs at the start 
  52. and CloseTempRegs at the end as well as disposing of any dynamic 
  53. variables that are created.  This is to allow the user to specify maximum
  54.  size of the VLN before the arithmetic working registers are initialized.
  55.  
  56. {------------------------------------------------------------}
  57. VLN Fields
  58.  
  59. count    -    the number of tVLN words used, tVLN[count] is
  60. expected to be non-zero, while higher words  are - undefined
  61.  
  62. max      -     The number of tVLN words for which space is assigned
  63. Writing to a higher word may cause a crash.
  64.  
  65. sign         -      " +1" signifies positive
  66.          " -1" signifies negative
  67.  
  68. tVLN         -       A set of "1 . . max" words representing a positive
  69. integer.  tVLN[1] is least significant and   tVLN[count] is most
  70. significant.  If tVLN[count] is zero then the same VLN number can be 
  71. represented by the same data set except with count= count-1.
  72. This is a situation that is awkward but all algorithms included here
  73. should accept the redundant representations. 
  74.  
  75. -------------------------------------------------------------
  76. Description of procedures and functions
  77.  
  78. constructor Init( cnt, maxC, sgn : integer; pnew :pWordArray);
  79. The normal place to set "max" := maxC.  Most arithmetic routines 
  80. check the current "count" against "max"   This constructor calls 
  81. Set Value (below)
  82.  
  83. procedure SetVal( cnt, sgn : integer; pnew :pWordArray);
  84. Assumes that max is previously set.
  85.     Count is set to  cnt
  86.     Sign is set to  sgn
  87.     tVLN is set to contents of pnew
  88.  
  89. procedure SetSmall(n:integer); { set immediate to 16 bits, signed }
  90. same as SetVal, except operand limited to +/- integer.
  91.         Count is set to 1
  92.     Sign" set to sign of n
  93.     tVLN[1] is set to absv(n)
  94.  
  95. procedure Clear( n : integer);
  96.     Count  is set to 0
  97.     Sign is set to 1
  98.     max" and "tVLN" unchanged
  99.  
  100. procedure Copy( other : pVryLrgNo );   { copy other into self }
  101.     Previous values are discarded except Max" is retained.
  102.     Sign, Count and tVLN terms are taken from "other"
  103.     if other.count > self.max then an error is called.
  104.  
  105. procedure WriteHex;
  106.     Write to screen, Most significant data first
  107.  
  108. procedure WriteDecimal ( mode : integer);
  109.     Write to screen in decimal format.  
  110.         In mode=0  Most significant data is first
  111.     in mode=1  least significant data is first
  112.     in mode=2  only one or two most significant terms are shown
  113.     
  114. procedure AddBy(other : pVryLrgNo);
  115.     The present value is added to "other".
  116.     Count & Sign are set to calculated values.  
  117.     tVLN[i] will contain new calculated data.  
  118.  
  119. procedure AddN(n:integer);
  120.     Same as Addby except that the operand is a provided integer.
  121.  
  122. procedure SubBy(other : pVryLrgNo);
  123.     The value "other" is subtracted from the present value.
  124.     Count & Sign are set to calculated values.  
  125.     tVLN[i] will contain new calculated data.  
  126.  
  127. procedure SubN(n:integer);
  128.     Same as Subby except that the operand is a provided integer.
  129.  
  130. procedure TwosComplAbs( cnt : integer );
  131.     Sign and Count are unchanged.
  132.     Each term of tVLN from 1 to count is changed with terms converted
  133.     by the equation ---  t := 65536-t up to and including the 
  134.     first non-zero term.  Subsequent terms are converted by the 
  135.     equation  ---  t := 65535-t    up to and including tVLN[count]
  136.  
  137.  
  138. procedure MulBy(other : pVryLrgNo);
  139.     The present value is multiplied by "other".
  140.     Count & Sign are set to calculated values.  
  141.     tVLN[i] will contain new calculated data.  
  142.  
  143. procedure MultN(n:integer);
  144.     Same as Mulby except that the operand is a provided integer.
  145.  
  146. procedure DivBy( dvsr, remnd : pVryLrgNo);
  147.     The present value is divided by "other".  
  148.     Count & Sign are set to calculated values.  
  149.     tVLN[i] will contain new calculated data.  
  150.  
  151. procedure DivN(n:integer);
  152.     Same as Addby except that the operand is a provided integer.
  153.  
  154. procedure TwoNth(n:integer );
  155.     Discards present count, sign, and tVLN words and sets
  156.     Sign  is set to  1,
  157.     Count is set to (n div 16) + 1
  158.     all tVLN terms < count-1 = 0
  159.     tVLN[count] = 2^(n mod 16)
  160.  
  161. procedure TenNth(n:integer );
  162.     Discards present count, sign, and tVLN words
  163.     Sign is set to  1
  164.     If n=1 then count is set to 1 and tVLN[1] is set to  10
  165.     If n>1 then tVLN becomes integer 10 is multiplied by itself 
  166.     (n-1) times
  167.  
  168. procedure NthRoot(n:integer );
  169.     The present value is used to calculate it's Nth root
  170.     Sign is set to 1;
  171.     Count and tVLN are calculated
  172.     In general the number of significant bits is decreased and is 
  173.     inversely proportional to n.
  174.  
  175. procedure NthPower(n:integer );
  176.     The present value is used to calculate it's Nth power.
  177.     Sign is set to 1;
  178.     Count and tVLN are calculated
  179.     In general the number of significant bits is increased  and is
  180.     directly proportional to n.
  181.  
  182. function  FindnoBinDig : integer;        { how many binary digits }
  183.     Counts the number of significant digits, binary with each 
  184.     term less than count =     16 and using the actual number of 
  185.     bits in tVLN[count]
  186.  
  187. procedure BigSHL(cnt  : integer);         {shift left by words }
  188.     The entire VLN is multiplied by 2^16.  Count is incremented 
  189.     and tVln[1] becomes 0.
  190.  
  191. procedure MultiSHL(sf_cnt : integer);         {shift left by bits }
  192.     Will shift the entire VLN by 1 to 15 bits. If tVLN[count] 
  193.     overflows, then count is incrementd and the overflow bits  
  194.     are placed in the new tVLN[count].
  195.  
  196. procedure Shr1Bit;         {shift right one bit}
  197.     The entire VLN is divided by 2.  
  198.     If tVLN[count] was initially = 1 then count is decremented.
  199.  
  200. procedure ShL1Bit;         {shift left one bit}
  201.     The entire VLN is multiplied by 2.  
  202.     If the word tVLN[count] overflows then count is incremented 
  203.     and the new highest term is set to 1.
  204.  
  205. function  FindDivShift(other : pVryLrgNo) : integer;
  206.     Finds required shift in preparation for dividing.  This allows 
  207.     the divisor to  be initially positioned so that it is not larger
  208.      than the  dividend. Ideally the divisor     would be  
  209.     positioned so  that it at least half as large as the dividend.  
  210.     However this routine only looks at the most significant terms 
  211.     of dividend and divisor so it     may start smaller than optimum.
  212.  
  213. procedure GetRandom(binCnt  : integer);
  214.     Returns a random number with bincnt binary bits. The  tVLN terms
  215.     are defined using pascal's random function.  In addition the
  216.     top      term  is masked to give the number of significant bits 
  217.     in that word as calculated  by "bincnt" mod 16. 
  218.  
  219. procedure Recount;
  220.     Tests each tVLN term starting at "count" and working down until 
  221.     a non-zero     term is found.  Count is decremented to point 
  222.     to that non-zero word or is set to zero if no non-zero terms are 
  223.     found.
  224.  
  225.  
  226. Other routines for manipulating the Class tVryLrgNo;
  227.  
  228.   function MaxOfW         (a,b : word) : word;
  229.     Returns a or b whichever is larger;
  230.  
  231.   function IsGrEqAbs      (n1, n2  : pVryLrgNo): boolean;  { 
  232.     Returns true if variable pointed to by n1 is 
  233.     larger than variable pointed to by n2, without considering
  234.     sign, (treating both as though they were positive numbers) 
  235.     
  236.   function IsEqAbs      (n1, n2  : pVryLrgNo): boolean;
  237.     Returns true if variable pointed to by n1 is 
  238.     equal to variable pointed to by n2, without considering
  239.     sign, (treating both as though they were positive numbers) 
  240.  
  241.   procedure HexWord  ( w : word; var pS : pchar4);
  242.     Returns a 4-character array;
  243.     pchar4 is defined as --- array[0..4]of char;
  244.  
  245.  
  246.   procedure SetWkSize     (n:integer);
  247.     Sets the size that internal arithmetic registers will be
  248.     initialized to when calling ' OpenTempRegs '
  249.  
  250.   function  GetWkSize     : integer;
  251.     Retreives the current value of wksize
  252.  
  253.   procedure CloseTempRegs ;
  254.     Initializes several temporary arithmetic registers using
  255.     GetMem and with size = 'wksize'
  256.  
  257.   procedure OpenTempRegs ;
  258.     Disploses of several temporary arithmetic registers using
  259.     FreeMem and with size = 'wksize'
  260.   
  261. procedure CallError        (s:String);
  262.     Prints the string 's'
  263.  
  264. ----------------------------------------------------------------------------------
  265. Included files
  266.  
  267. VLNCLS.PAS   defines the Class - tVryLrgNo 
  268. FACTRL      calculates factorials
  269. FIBNACII      calculates the Fibonacii series
  270. MATRIX2      Creates an array of  reciprocals
  271. PI_4_96      calculates PI
  272. SQROOT      calculates square roots to variable precision
  273. ROOTS      calculates specified root to 36 decimal digits
  274.  
  275.  
  276. ----------------------------------------------------------------------------------
  277. This program is given to public without restriction
  278. ----------------------------------------------------------------------------------
  279.  
  280.  
  281.