80.txt~ 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. Operations with real values in Rigal
  2. =======================================
  3. NOTE:
  4. This description presents implementation of
  5. operations with real values in Rigal in
  6. MS-DOS (Turbo-Pascal based) and UNIX (C - based)
  7. variants, starting from Rigal V.2.34 and UNIX Rigal V.4.45.
  8. Read carefully parts regarding UNIX and MS-DOS.
  9. Old such description was for MS-DOS version
  10. only. Unix description exists only starting from version
  11. 4.45.
  12. Limitations of Turbo Pascal
  13. ===========================
  14. MS-DOS Rigal is implemented via Pascal real run time library. The
  15. following sequence of characters is accepted by Pascal VAL
  16. procedure as a real number:
  17. (* space *) [ (+!-) ] (* digits1 *) [ '.' (* digit2 *) ]
  18. [ (e!E) [ (+!-) ] (* digit3 *) ]
  19. The context constraints are that:
  20. digit1+digit2<37 ( limited number of digits )
  21. The absolute value of digits1.digits2E[-]digits3 must be
  22. in 2.9e-39 ... 1.7e38
  23. We assume that UNIX C has no such constraints.
  24. Input real number from lexical analyzer
  25. =======================================
  26. We call real numbers stored in Rigal memory as #FATOMs. Use
  27. built-in Rigal predicate #FATOM to recognize them. They
  28. can be read by the lexical analyzer from its input as
  29. sequences of characters that must match following grammar
  30. rule
  31. (+ digits1 +)
  32. ( '.' (* digit2 *) [ (e!E) [ (+!-) ] (* digit3 *) ]
  33. ! (e!E) [ (+!-) ] (* digit3 *) )
  34. and it must be acceptable by procedure VAL (see above).
  35. IN MS-DOS:
  36. =========
  37. If #FATOM is read then it is stored in 8-byte-length atom, 6
  38. bytes are occupied by standard representation of REAL type
  39. in Pascal. In other two bytes number of digits before dot
  40. and number of digits after the dot are stored. If the input
  41. number was in exponential form then these bytes contain
  42. zeros.
  43. IN UNIX:
  44. =======
  45. If #FATOM is read then it is stored in 8-byte-length atom,
  46. assuming that sizeof(double)=8. The information about the
  47. number of digits before the dot and number of digits after
  48. the dot are _NOT_ stored.
  49. Since standard 6 (or 8)-byte representation is stored, only 10 (13)
  50. digits are valid.
  51. Non-matching in MS-DOS scaner
  52. =============================
  53. If the input value matches grammar and does not match "VAL
  54. rules" then atom with type #KEYWORD is produced (it is
  55. used for diagnostic purposes). Normally #FATOM is produced.
  56. Output of real numbers
  57. ======================
  58. Statements PRINT, <<, ,<], built-in rules #IMPLODE and
  59. #EXPLODE output real numbers in "exponential" form,
  60. like the %E format in UNIX, or simple WRITE() in Pascal.
  61. Sd.ddddddddddEZdd
  62. S is space or '-'
  63. d are digits (exactly 10 digits after dot)
  64. E is character 'E'
  65. Z is '+' or '-'
  66. Real numbers in traditional operators
  67. =====================================
  68. If you use real numbers in traditional arithmetical
  69. operators and comparisons, all #FATOMs are accepted the
  70. same way as NULL. It corresponds to 0 value. If you compare
  71. #FATOMs with '=', their internal representation (with 6
  72. or 8 bytes long code) will be compared. Therefore, there
  73. is no sense in such operations.
  74. Ways to create #FATOM
  75. =====================
  76. #CALL_PAS(80 S any_string)
  77. returns #FATOM if the string value matches "VAL rule".
  78. Otherwise NULL is returned.
  79. #CALL_PAS(80 I integer)
  80. returns #FATOM. Integer value can be arbitrary.
  81. Ways to get information from #FATOM
  82. ===================================
  83. ( THIS APPLIES TO MS_DOS VARIANT ONLY !)
  84. ======================================
  85. #CALL_PAS(80 D #FATOM) returns pair
  86. (. digits_before digits_after .)
  87. for FATOM values that were entered through the
  88. lexical analyzer. If the number was entered in exponential
  89. style (with character, 'E'), value (. 0 0 .) is returned.
  90. If the value was obtained by #CALL_PAS(80..), NULL is
  91. returned. There is no other ways to create #FATOM.
  92. NOTE:
  93. If you have entered #FATOM to variable $X via lexical
  94. analyzer, you can output it with the same number of digits
  95. before and after the point (if it was not exponential
  96. style) as it was read by lexical analyser:
  97. $D:=#CALL_PAS(80 D $X);
  98. IF $D AND ($D[1]+$D[2]>0) ->
  99. $REZ:=#CALL_PAS(80 Z $X ($D[1]+$D[2]+1)*100+$D[2])
  100. ELSIF T->
  101. $REZ:=#CALL_PAS(80 V $X)
  102. FI;
  103. Conversions (MS-DOS VARIANT ONLY)
  104. =================================
  105. #CALL_PAS(80 T #FATOM)
  106. returns #NUMBER - whole part of real value. If the
  107. number is not in -2147483648..2147483647 then NULL is
  108. returned.
  109. #CALL_PAS(80 Z #FATOM D*100+A)
  110. returns formatted string with the value of the real
  111. number. "A" can be positive or negative.
  112. If "A" is arbitrary NEGATIVE number then exponential form
  113. is produced in following form:
  114. Sd.dEZdd (e.g. -7.7E01)
  115. S is space or '-' Z is '-' or '+'
  116. If D>=9 then additional digits are added after the point,
  117. but not more than 10 digits.
  118. If "A" is 0 then integer value is produced, possible with
  119. '-'.
  120. If "A" is positive, it specifies number of digits after
  121. the point. Non-exponential form is produced.
  122. Zeros are appended after point if necessary. The
  123. number of digits after the point cannot be larger than 11.
  124. After all cases discussed above, if "D" is larger than the
  125. new string, RIGAL adds spaces from the left side of the
  126. string. The result string length is not less than "D".
  127. #CALL_PAS(80 R #FATOM p)
  128. - returns rounded value of real number. The number is
  129. rounded such way that digits after p-th position after the
  130. dot must be set to zeros. E.g. 23.1415 rounded at 2 nd
  131. position must be 23.140000.
  132. If p is not positive, it is accepted as 0. If absolute
  133. value of real*(10 in p-th degree) is larger than 1e37 then
  134. NULL is returned.
  135. #CALL_PAS(80 V #FATOM)
  136. - creates string from real number in form
  137. [-]d.ddddddddddESdd
  138. S is '+' or '-'
  139. Conversions (UNIX VARIANT ONLY)
  140. =================================
  141. #CALL_PAS(80 F #FATOM FORMAT_STRING)
  142. returns formatted string with UNIX C the _arbitrary_ format
  143. (allowed in UNIX C sprintf function) you specified,
  144. for example #CALL_PAS(80 F $A 'A=%.9e');
  145. Note, however, that this option is _not_ ported to MS-DOS.
  146. #CALL_PAS(80 Z #FATOM D*100+A)
  147. returns formatted string with the value of the real
  148. number. "A" _must_ be positive.
  149. If "A" is 0 then integer value is produced, possible with
  150. '-'.
  151. If "A" is positive, it specifies number of digits after
  152. the point. Non-exponential form is produced.
  153. Zeros are appended after point if necessary. The
  154. number of digits after the point cannot be larger than 11.
  155. After all cases discussed above, if "D" is larger than the
  156. new string, RIGAL adds spaces from the left side of the
  157. string. The result string length is not less than "D".
  158. #CALL_PAS(80 V #FATOM)
  159. returns formatted string with UNIX C %E format.
  160. Mathematical operations
  161. =======================
  162. #CALL_PAS(80 op #FATOM #FATOM)
  163. Operations "op" can be '+','-', '*','/', '<','>',
  164. '<=','>=', '<>','='
  165. In MS-DOS if '+' or '-' applied, absolute value of the arguments
  166. must be less than 6.2e37 otherwise NULL is returned.
  167. In MS-DOS if '*' or '-' applied, absolute value of the result must
  168. be between 1.6e-38..6.2e37 and the second argument of '/' is
  169. not 0, otherwise NULL is returned.
  170. If '<','>','<=','>=','<>','=' are applied, one of the
  171. arguments is returned to indicate 'true', NULL is returned
  172. to indicate 'false' Note that it is BAD and DANGEROUS STYLE to check the
  173. results of real arithmetic using '=' or '<>' operations,
  174. because the computer never calculates precise results. You
  175. can compare two real numbers only after some conversion,
  176. e.g. using #CALL_PAS(80 V ...).
  177. #CALL_PAS(80 Q #FATOM) returns square root if argument is
  178. not negative, NULL otherwise.
  179. #CALL_PAS(80 X #FATOM) returns exponential ( e in r-th
  180. degree) of the argument.
  181. #CALL_PAS(80 L #FATOM) returns natural logarithm if the
  182. argument is positive, otherwise NULL.
  183. Mathematical operations for UNIX only
  184. =====================================
  185. #CALL_PAS(80 op #FATOM)
  186. where op = tSIN, tCOS, tTAN, tASIN, tACOS, tATAN.
  187. call corresponding functions from UNIX libm library.
  188. Common note about #CALL_PAS(80..).
  189. =================================
  190. It has no run time diagnostic.
  191. If wrong types of arguments of wrong number of arguments
  192. or wrong operation name is given, NULL is returned.
  193. If #CALL_PAS(80...) produces a #FATOM, it is 6 (or 8) bytes long
  194. and it has _no_ information about the digits before and after
  195. the dot.
  196. You can learn more details about #CALL_PAS(80) from
  197. source files USE80.PAS and SERVICES.PAS in MS-DOS,
  198. usemod.c, sevice.c in UNIX C.