flt_arith.3 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. .TH FLT_ARITH 3 "$Revision$"
  2. .ad
  3. .SH NAME
  4. flt_arith \- high precision floating point arithmetic
  5. .SH SYNOPSIS
  6. .nf
  7. .B #include <flt_arith.h>
  8. .PP
  9. .if t .ta 3m 13m 22m
  10. .if n .ta 5m 25m 40m
  11. struct flt_mantissa {
  12. long flt_h_32; /* high order 32 bits of mantissa */
  13. long flt_l_32; /* low order 32 bits of mantissa */
  14. };
  15. typedef struct {
  16. short flt_sign; /* 0 for positive, 1 for negative */
  17. short flt_exp; /* between -16384 and 16384 */
  18. struct flt_mantissa flt_mantissa; /* normalized, in [1,2). */
  19. } flt_arith;
  20. extern int flt_status;
  21. #define FLT_OVFL 001
  22. #define FLT_UNFL 002
  23. #define FLT_DIV0 004
  24. #define FLT_NOFLT 010
  25. #define FLT_BTSM 020
  26. #define FLT_STRLEN 32
  27. .PP
  28. .B void flt_add(e1, e2, e3)
  29. .B flt_arith *e1, *e2, *e3;
  30. .PP
  31. .B void flt_mul(e1, e2, e3)
  32. .B flt_arith *e1, *e2, *e3;
  33. .PP
  34. .B void flt_sub(e1, e2, e3)
  35. .B flt_arith *e1, *e2, *e3;
  36. .PP
  37. .B void flt_div(e1, e2, e3)
  38. .B flt_arith *e1, *e2, *e3;
  39. .PP
  40. .B void flt_umin(e)
  41. .B flt_arith *e;
  42. .PP
  43. .B void flt_modf(e1, intpart, fractpart)
  44. .B flt_arith *e1, *intpart, *fractpart;
  45. .PP
  46. .B int flt_cmp(e1, e2)
  47. .B flt_arith *e1, *e2;
  48. .PP
  49. .B void flt_str2flt(s, e)
  50. .B char *s;
  51. .B flt_arith *e;
  52. .PP
  53. .B void flt_flt2str(e, buf, bufsize)
  54. .B flt_arith *e;
  55. .B char *buf;
  56. .B int bufsize;
  57. .PP
  58. .B int flt_status;
  59. .PP
  60. .B #include <em_arith.h>
  61. .B void flt_arith2flt(n, e, uns)
  62. .B arith n;
  63. .B flt_arith *e;
  64. .B int uns;
  65. .PP
  66. .B arith flt_flt2arith(e, uns)
  67. .B flt_arith *e;
  68. .B int uns;
  69. .PP
  70. .B void flt_b64_sft(m, n)
  71. .B struct flt_mantissa *m;
  72. .B int n;
  73. .SH DESCRIPTION
  74. This set of routines emulates floating point arithmetic, in a high
  75. precision. It is intended primarily for compilers that need to evaluate
  76. floating point expressions at compile-time. It could be argued that this
  77. should be done in the floating point arithmetic of the target machine,
  78. but EM does not define its floating point arithmetic.
  79. .PP
  80. .B flt_add
  81. adds the numbers indicated by
  82. .I e1
  83. and
  84. .I e2
  85. and stores the result indirectly through
  86. .IR e3 .
  87. .PP
  88. .B flt_mul
  89. multiplies the numbers indicated by
  90. .I e1
  91. and
  92. .I e2
  93. and stores the result indirectly through
  94. .IR e3 .
  95. .PP
  96. .B flt_sub
  97. subtracts the number indicated by
  98. .I e2
  99. from the one indicated by
  100. .I e1
  101. and stores the result indirectly through
  102. .IR e3 .
  103. .PP
  104. .B flt_div
  105. divides the number indicated by
  106. .I e1
  107. by the one indicated by
  108. .I e2
  109. and stores the result indirectly through
  110. .IR e3 .
  111. .PP
  112. .B flt_umin
  113. negates the number indicated by
  114. .I e
  115. and stores the result indirectly through
  116. .IR e .
  117. .PP
  118. .B flt_modf
  119. splits the number indicated by
  120. .I e
  121. in an integer and a fraction part, and stores the integer part through
  122. .I intpart
  123. and the fraction part through
  124. .IR fractpart .
  125. So, adding the numbers indicated by
  126. .I intpart
  127. and
  128. .I fractpart
  129. results (in the absence of rounding error) in the number
  130. indicated by
  131. .IR e .
  132. Also, the absolute value of the number indicated by
  133. .I intpart
  134. is less than or equal to the absolute value of the number indicated by
  135. .IR e .
  136. The absolute value of the number indicated by
  137. .I fractpart
  138. is less than 1.
  139. .PP
  140. .B flt_cmp
  141. compares the numbers indicated by
  142. .I e1
  143. and
  144. .I e2
  145. and returns -1 if
  146. .I e1
  147. <
  148. .IR e2 ,
  149. 0 if
  150. .I e1
  151. =
  152. .IR e2 ,
  153. and 1 if
  154. .I e1
  155. >
  156. .IR e2 .
  157. .PP
  158. .B flt_str2flt
  159. converts the string indicated by
  160. .I s
  161. to a floating point number, and stores this number through
  162. .IR e.
  163. The string should contain a floating point constant, which consists of
  164. an integer part, a decimal point, a fraction part, an \f(CWe\fP or an
  165. \f(CWE\fP, and an optionally signed integer exponent. The integer and
  166. fraction parts both consist of a sequence of digits. They may not both be
  167. missing. The decimal point, the \f(CWe\fP and the exponent may be
  168. missing.
  169. .PP
  170. .B flt_flt2str
  171. converts the number indicated by
  172. .I e
  173. into a string, in a scientific notation acceptable for EM. The result is
  174. stored in
  175. .IR buf .
  176. At most
  177. .I bufsize
  178. characters are stored.
  179. The maximum length needed is available in the constant FLT_STRLEN.
  180. .PP
  181. .B flt_arith2flt
  182. converts the number
  183. .I n
  184. to the floating point format used in this package and returns the result
  185. in
  186. .IR e . If the
  187. .I uns
  188. flag is set, the number
  189. .I n
  190. is regarded as an unsigned.
  191. .PP
  192. .B flt_flt2arith
  193. truncates the number indicated by
  194. .I e
  195. to the largest integer value smaller than or equal to the number indicated by
  196. .IR e .
  197. It returns this value. If the
  198. .I uns
  199. flag is set, the result is to be regarded as unsigned.
  200. .PP
  201. Before each operation, the
  202. .I flt_status
  203. variable is reset to 0. After an operation, it can be checked for one
  204. of the following values:
  205. .IP FLT_OVFL
  206. .br
  207. an overflow occurred. The result is a large value with the correct sign.
  208. This can occur with the routines
  209. .IR flt_add ,
  210. .IR flt_sub ,
  211. .IR flt_div ,
  212. .IR flt_mul ,
  213. .IR flt_flt2arith ,
  214. and
  215. .IR flt_str2flt .
  216. .IP FLT_UNFL
  217. .br
  218. an underflow occurred. The result is 0.
  219. This can occur with the routines
  220. .IR flt_div ,
  221. .IR flt_mul ,
  222. .IR flt_sub ,
  223. .IR flt_add ,
  224. and
  225. .IR flt_str2flt .
  226. .IP FLT_DIV0
  227. .br
  228. divide by 0. The result is a large value with the sign of the dividend.
  229. This can only occur with the routine
  230. .IR flt_div .
  231. .IP FLT_NOFLT
  232. .br
  233. indicates that the string did not represent a floating point number. The
  234. result is 0.
  235. This can only occur with the routine
  236. .IR flt_str2flt .
  237. .IP FLT_BTSM
  238. .br
  239. indicates that the buffer is too small. The contents of the buffer is
  240. undefined. This can only occur with the routine
  241. .IR flt_flt2str .
  242. .PP
  243. The routine
  244. .I flt_b64_sft
  245. shifts the mantissa
  246. .I m
  247. .I |n|
  248. bits left or right, depending on the sign of
  249. .IR n .
  250. If
  251. .I n
  252. is negative, it is a left-shift; If
  253. .I n
  254. is positive, it is a right shift.
  255. .SH FILES
  256. ~em/modules/h/flt_arith.h
  257. .br
  258. ~em/modules/h/em_arith.h
  259. .br
  260. ~em/modules/lib/libflt.a