home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / mathwiz.zip / MATHWIZ.DOC < prev    next >
Text File  |  1991-01-08  |  17KB  |  383 lines

  1.                      The Math Wizard's Library for BASIC                page 1
  2.                    =-------------------------------------=
  3.                                  Version 1.0
  4.  
  5.               MATHWIZ  Copyright (c) 1991  Thomas G. Hanlin III
  6.  
  7.  
  8.  
  9. This is MATHWIZ, a library of assembly language and BASIC routines for use
  10. with QuickBASIC version 4.5.  Support for other recent versions of the BASIC
  11. compiler is provided with registration.  The MATHWIZ collection is
  12. copyrighted and may be distributed only under the following conditions:
  13.  
  14.    1) No fee of over $10.00 may be charged for distribution.  This
  15.       restriction applies only to physical copies and is not meant to
  16.       prevent distribution by telecommunication services.
  17.  
  18.    2) All MATHWIZ files must be distributed together in original, unaltered
  19.       form.  This includes MATHWIZ.BI, MATHWIZ.DOC, MATHWIZ.LIB, MATHWIZ.NEW,
  20.       MATHWIZ.QLB, MATHWIZ.REF, BIBLIO.TXT, CATALOG.TXT, FILES.LST,
  21.       LIBRARY.TXT, QUESTION.TXT, and REGISTER.TXT.
  22.  
  23. You use this library at your own risk.  It has been tested by me on my own
  24. computer, but I will not assume any responsibility for any problems which
  25. MATHWIZ may cause you.  If you do encounter a problem, please let me know
  26. about it, and I will do my best to verify and repair the error.
  27.  
  28. It is expected that if you find MATHWIZ useful, you will register your copy.
  29. You may not use MATHWIZ routines in programs intended for sale unless you
  30. have registered.  Registration entitles you to receive the latest version of
  31. MATHWIZ, complete with full source code in assembly language and BASIC.  The
  32. assembly code is designed for the OPTASM assembler by SLR Systems and will
  33. require modifications if you wish to use it with MASM or TASM.  You will be
  34. able to compile the BASIC code with whatever version of the compiler you
  35. have, allowing you to use MATHWIZ with QuickBASIC versions 4.0 - 4.5 and
  36. BASCOM versions 6.0 - 7.1 (QBX and BASCOM 7.x far strings aren't supported).
  37.  
  38. Warning: Use of MATHWIZ for more than 30 days without registering has been
  39. determined to cause the Surgeon General to issue warning labels!  If you use
  40. this product, please do register.
  41.  
  42. For an example of how to set up your program to access the MATHWIZ library,
  43. how to LINK the routines, and so forth, see the LIBRARY.TXT file.
  44.  
  45. So who's the Math Wizard?  With this library, you will be!  Read this
  46. tome well, for invoking these routines without proper preparation may bring
  47. unexpected results.  Cape and hat (optional) not included.  No assembly
  48. required.
  49.  
  50. Note that MATHWIZ may be considered an extension of my main BASIC library,
  51. BASWIZ.  See CATALOG.TXT for additional information.
  52.  
  53.                               Table of Contents                         page 2
  54.  
  55.  
  56.  
  57.  Overview and Legal Info ................................................ 1
  58.  
  59.  Extensions to BASIC's floating point ................................... 3
  60.  
  61.  BCD Math ............................................................... 4
  62.  
  63.  Fractions .............................................................. 7
  64.  
  65.  Miscellaneous Notes .................................................... 8
  66.  
  67.  Troubleshooting ........................................................ 9
  68.  
  69.  Using MATHWIZ with PDQ ................................................ 10
  70.  
  71.                      Extensions to BASIC's floating point               page 3
  72.  
  73.  
  74.  
  75. For the most part, this library is designed to provide alternatives to the
  76. math routines that are built into BASIC.  Still, BASIC's floating point is
  77. quite adequate for many purposes, so there's no sense in ignoring it.  Here
  78. are a few functions which make BASIC math a bit more convenient.
  79.  
  80.    Result! = FactS!(Nr%)       ' factorial
  81.    Result! = CotS!(Nr!)        ' cotangent
  82.    Result! = CscS!(Nr!)        ' cosecant
  83.    Result! = SecS!(Nr!)        ' secant
  84.    Result! = Deg2RadS!(Nr!)    ' convert degrees to radians
  85.    Result! = Rad2DegS!(Nr!)    ' convert radians to degrees
  86.    Result! = Cent2Fahr!(Nr!)   ' convert centigrade to Fahrenheit
  87.    Result! = Fahr2Cent!(Nr!)   ' convert Fahrenheit to centigrade
  88.    Result! = Kg2Pound!(Nr!)    ' convert kilograms to pounds
  89.    Result! = Pound2Kg!(Nr!)    ' convert pounds to kilograms
  90.    Pi! = PiS!                  ' the constant "pi"
  91.    e! = eS!                    ' the constant "e"
  92.  
  93.    Result# = FactD#(Nr%)       ' factorial
  94.    Result# = CotD#(Nr#)        ' cotangent
  95.    Result# = CscD#(Nr#)        ' cosecant
  96.    Result# = SecD#(Nr#)        ' secant
  97.    Result# = Deg2RadD#(Nr#)    ' convert degrees to radians
  98.    Result# = Rad2DegD#(Nr#)    ' convert radians to degrees
  99.    Pi# = PiD#                  ' the constant "pi"
  100.    e# = eD#                    ' the constant "e"
  101.  
  102. Like BASIC, the trig functions expect the angle to be in radians.  Constants
  103. are expressed to the maximum precision available.  If you are not familiar
  104. with variable postfix symbols, "!" indicates single precision and "#"
  105. indicates double precision.  See your BASIC manual for further details.
  106.  
  107.                                    BCD Math                             page 4
  108.  
  109.  
  110.  
  111. Some of you may not have heard of BCD math, or at least not have more than a
  112. passing acquaintance with the subject.  BCD (short for Binary-Coded Decimal)
  113. is a way of encoding numbers.  It differs from the normal method of handling
  114. numbers in several respects.  On the down side, BCD math is much slower than
  115. normal math and the numbers take up more memory.  However, the benefits may
  116. far outweigh these disadvantages, depending on your application: BCD math is
  117. absolutely precise within your desired specifications, and you can make a BCD
  118. number as large as you need.  If your applications don't require great range
  119. or precision out of numbers, normal BASIC math is probably the best choice.
  120. For scientific applications, accounting, engineering and other demanding
  121. tasks, though, BCD may be just the thing you need.
  122.  
  123. The BCD math routines provided by MATHWIZ allow numbers of up to 255 digits
  124. long (the sign counts as a digit, but the decimal point doesn't).  You may
  125. set the decimal point to any position you like, as long as there is at least
  126. one digit position to the left of the decimal.
  127.  
  128. Since QuickBASIC doesn't support BCD numbers directly, we store the BCD
  129. numbers in strings.  The results are not in text format and won't mean much
  130. if displayed.  A conversion routine allows you to change a BCD number to a
  131. text string in any of a variety of formats.  The BCD numbers can also be
  132. compressed to allow more efficient storage.
  133.  
  134. Note that the BCD math handler doesn't yet track overflow/underflow error
  135. conditions.  If you anticipate that this may be a problem, it would be a good
  136. idea to screen your input or to make the BCD range large enough to avoid
  137. these errors.
  138.  
  139. Let's start off by examining the routine which allows you to set the BCD
  140. range:
  141.  
  142.    BCDSetSize LeftDigits%, RightDigits%
  143.  
  144. The parameters specify the maximum number of digits to the left and to the
  145. right of the decimal point.  There must be at least one digit on the left,
  146. and the total number of digits must be less than 255.  The BCD strings will
  147. have a length that's one larger than the total number of digits, to account
  148. for the sign of the number.  The decimal point is implicit and doesn't take
  149. up any extra space.
  150.  
  151. It is assumed that you will only use one size of BCD number in your program--
  152. there are no provisions for handling mixed-length BCD numbers.  Of course,
  153. you could manage that yourself with a little extra work, if it seems like a
  154. useful capability.  If you don't use BCDSetSize, the default size of the BCD
  155. numbers will be 32 (20 to the left, 11 to the right, 1 for the sign).
  156.  
  157. You can get the current size settings in your program, too:
  158.  
  159.    BCDGetSize LeftDigits%, RightDigits%
  160.  
  161.                                    BCD Math                             page 5
  162.  
  163.  
  164.  
  165. Before doing any BCD calculations, you must have some BCD numbers!  The
  166. BCDSet routine takes a number in text string form and converts it to BCD:
  167.  
  168.    TextSt$ = "1234567890.50"
  169.    Nr$ = BCDSet$(TextSt$)
  170.  
  171. If your numbers are stored as actual numbers, you can convert them to a text
  172. string with BASIC's STR$ function, then to BCD.  Leading spaces are ignored:
  173.  
  174.    Nr$ = BCDSet$(STR$(AnyNum#))
  175.  
  176. BCD numbers can also be converted back to text strings, of course.  You may
  177. specify how many digits to the right of the decimal to keep (the number will
  178. be truncated, not rounded).  If the RightDigits% is positive, trailing zeros
  179. will be kept; if negative, trailing zeros will be removed.  There are also
  180. various formatting options which may be used.  Here's how it works:
  181.  
  182.    TextSt$ = BCDFormat$(Nr$, HowToFormat%, RightDigits%)
  183.  
  184. The HowToFormat% value may be any combination of the following (just add the
  185. numbers of the desired formats together):
  186.  
  187.    0   plain number
  188.    1   use commas to separate thousands, etc
  189.    2   start number with a dollar sign
  190.    4   put the sign on the right side instead of the left side
  191.    8   use a plus sign instead of a space if number is not negative
  192.  
  193. The BCD math functions are pretty much self-explanatory, so I'll keep the
  194. descriptions brief.  Here are the single-parameter functions:
  195.  
  196.    Result$ = BCDAbs$(Nr$)              ' take the absolute value of a number
  197.    Result$ = BCDCos$(Nr$)              ' cosine function
  198.    Result$ = BCDCot$(Nr$)              ' cotangent function
  199.    Result$ = BCDCsc$(Nr$)              ' cosecant function
  200.    Result$ = BCDDeg2Rad$(Nr$)          ' convert degrees to radians
  201.    e$ = BCDe$                          ' get the value of the constant "e"
  202.    Result$ = BCDFact$(N%)              ' calculate the factorial of integer N
  203.    Result$ = BCDNeg$(Nr$)              ' negate a number
  204.    pi$ = BCDpi$                        ' get the value of the constant "pi"
  205.    Result$ = BCDRad2Deg$(Nr$)          ' convert radians to degrees
  206.    Result$ = BCDSec$(Nr$)              ' secant function
  207.    Result% = BCDSgn%(Nr$)              ' signum function
  208.    Result$ = BCDSin$(Nr$)              ' sine function
  209.    Result$ = BCDSqr$(Nr$)              ' get the square root of a number
  210.    Result$ = BCDTan$(Nr$)              ' tangent function
  211.  
  212.                                    BCD Math                             page 6
  213.  
  214.  
  215.  
  216. Notes on the single-parameter functions:
  217.  
  218.   The signum function returns an integer based on the sign of the BCD number:
  219.      -1   the BCD number is negative
  220.       0   the BCD number is zero
  221.       1   the BCD number is positive
  222.  
  223.   BCDpi is currently accurate to as many as 200 decimal positions.  BCDe is
  224.   accurate to as many as 115 decimal places.  The actual accuracy, of course,
  225.   depends on the size of BCD numbers you've chosen.
  226.  
  227.   The trigonometric functions (cos, sin, tan, sec, csc, cot) expect angles in
  228.   radians.  BCDDeg2Rad and BCDRad2Deg will allow you to convert back and
  229.   forth between radians and degrees, although some accuracy may be lost
  230.   inasmuch as they rely on BCDpi.
  231.  
  232.  
  233.  
  234. Here is a list of the two-parameter functions:
  235.  
  236.    Result$ = BCDAdd$(Nr1$, Nr2$)      ' add two numbers together
  237.  
  238.    Result$ = BCDSub$(Nr1$, Nr2$)      ' subtract the second nr from the first
  239.  
  240.    Result$ = BCDMul$(Nr1$, Nr2$)      ' multiply one number by another
  241.  
  242.    Result$ = BCDDiv$(Nr1$, Nr2$)      ' divide the first number by the second
  243.  
  244.    Result$ = BCDPower$(Nr$, Power%)   ' raise a number to a power
  245.  
  246.    Result% = BCDCompare%(Nr1$, Nr2$)  ' compare two numbers
  247.  
  248. The comparison function returns an integer which reflects how the two numbers
  249. compare to eachother:
  250.    -1   Nr1 < Nr2
  251.     0   Nr1 = Nr2
  252.     1   Nr1 > Nr2
  253.  
  254.                                   Fractions                             page 7
  255.  
  256.  
  257.  
  258. Using BCD allows you to represent numbers with excellent precision, but at a
  259. fairly large cost in speed.  Another way to represent numbers with good
  260. precision is to use fractions.  Fractions can represent numbers far more
  261. accurately than BCD, but can be handled much more quickly.  There are some
  262. limitations, of course, but by now you've guessed that's always true!
  263.  
  264. Each fraction is represented by MATHWIZ as a string of 8 characters.  The
  265. numerator (top part of the fraction) may be anywhere from -999,999,999 to
  266. 999,999,999.  The denominator (the bottom part) may be from 0 to 999,999,999.
  267. This allows handling a fairly wide range of numbers quite exactly.
  268.  
  269. Fractions can be converted to or from numeric text strings in any of three
  270. formats: real number (e.g., "1.5"), plain fraction (e.g., "3/2"), or whole
  271. number and fraction (e.g., "1 1/2").  Internally, the numbers are stored as a
  272. plain fraction, reduced to the smallest fraction possible which means the
  273. same thing (for instance, "5/10" will be reduced to "1/2").
  274.  
  275. To convert a numeric text string into a fraction, do this:
  276.  
  277.    Nr$ = FracSet$(NumSt$)
  278.  
  279. To convert a fraction into a numeric text string, try this:
  280.  
  281.    NumSt$ = FracFormat$(Nr$, HowToFormat%)
  282.  
  283. The formatting options are:
  284.  
  285.    0   convert to plain fraction
  286.    1   convert to whole number and fraction
  287.    2   convert to decimal number
  288.  
  289. Here is a list of the other functions available:
  290.  
  291.    Result$ = FracAbs$(Nr$)            ' take the absolute value of a fraction
  292.    Result$ = FracAdd$(Nr1$, Nr2$)     ' add two fractions
  293.    Result% = FracCompare%(Nr1$, Nr2$) ' compare two fractions
  294.    Result$ = FracDiv$(Nr1$, Nr2$)     ' divide the first fraction by the second
  295.    Result$ = FracMul$(Nr1$, Nr2$)     ' multiply two fractions
  296.    Result$ = FracNeg$(Nr$)            ' negate a fraction
  297.    Result% = FracSgn%(Nr$)            ' signum function for a fraction
  298.    Result$ = FracSub$(Nr1$, Nr2$)     ' subtract the 2nd fraction from the 1st
  299.  
  300. Fractions are automatically reduced to allow the greatest possible range.
  301. Note that little range-checking is done at this point, so you may wish to
  302. screen any input to keep it reasonable.
  303.  
  304.    Result     FracSgn      FracCompare
  305.      -1       negative #    1st < 2nd
  306.       0       # is zero     1st = 2nd
  307.       1       positive #    1st > 2nd
  308.  
  309.                              Miscellaneous Notes                        page 8
  310.  
  311.  
  312.  
  313. A certain lack of speed is inherent in BCD math, especially if you require
  314. high precision.  The division, root, and trig routines in particular are
  315. quite slow.  I'll attempt to improve this in the future, but the routines are
  316. already fairly well optimized, so don't expect miracles.  Precision costs!
  317.  
  318. The fraction routines are much faster, but they have a much smaller range.
  319. I'll have to do some experimenting on that.  It may prove practical to use a
  320. subset of the BCD routines to provide an extended range for fractions without
  321. an unreasonable loss in speed.
  322.  
  323. I am hoping to add Bessel functions, matrix math, logarithms and other
  324. goodies in the future as I am able to obtain information on how to handle
  325. these things.  Your suggestions as to good reference books and other routines
  326. that might be useful will be most welcome.
  327.  
  328.                                Troubleshooting                          page 9
  329.  
  330.  
  331.  
  332. Problem:
  333.    QB says "subprogram not defined".
  334.  
  335. Solution:
  336.    The definition file was not included.  Your program must contain the line
  337.       REM $INCLUDE: 'MATHWIZ.BI'
  338.    before any executable code in your program.  You should also start
  339.    QuickBASIC with
  340.       QB /L MATHWIZ
  341.    so it knows to use the MATHWIZ library.
  342.  
  343.  
  344. Problem:
  345.    LINK says "unresolved external reference".
  346.  
  347. Solution:
  348.    Did you specify MATHWIZ as the library when you used LINK?  You should!
  349.    The MATHWIZ.LIB file must be in the current directory or along a path
  350.    specified by the LIB environment variable (like PATH, but for LIB files).
  351.  
  352.  
  353. Problem:
  354.    The BCDSIN$ function returns strange results.
  355.  
  356. Solution:
  357.    Make sure you have left some room on the left side of the decimal as well
  358.    as the right!  The sine calculations can involve fairly large numbers.
  359.  
  360.                            Using MATHWIZ with PDQ                      page 10
  361.  
  362.  
  363.  
  364. Unfortunately, most MATHWIZ routines are not compatible with Crescent
  365. Software's PDQ library.  Some of the routines require floating point math,
  366. which is not supported by PDQ.  Many of the routines require dynamic string
  367. functions, which aren't supported by PDQ either.  So it goes.
  368.  
  369. Crescent thoughtfully provided me with a free copy of PDQ in order that I
  370. might resolve any incompatibilities between it and my BASIC libraries.  In
  371. this case, it didn't prove practical to do so.
  372.  
  373. If you are not familiar with PDQ, it is a replacement library for BASIC's own
  374. runtime libraries.  While not providing every capability of plain QuickBASIC,
  375. it allows you to create substantially smaller EXE files for those programs
  376. that qualify.  Support is currently lacking for floating point (single/double
  377. precision) numbers, music, and graphics, among other things.  I understand
  378. that these will be added to a future version of the library.  Communications
  379. support is available as an add-on package.  PDQ also adds new capabilities
  380. which are quite impressive, such as being able to write small TSRs in BASIC.
  381. Check with Crescent for more recent details.
  382.  
  383.