umouse

umouse.git
git clone git://git.lenczewski.org/umouse.git
Log | Files | Refs | Submodules | README

DPANSA12.HTM (11473B)


      1 <HTML><HEAD>
      2 <TITLE>DPANS94</TITLE>
      3 <link disabled rel="stylesheet" href="mpexc6.css">
      4 <style>@import url(mpexc6.css);</style>
      5 </head>
      6 
      7 <BODY>
      8 <table width=100%>
      9 <tr>
     10 <td align=left>
     11 <a href=dpansa11.htm><img src=left.gif
     12  width=26 height=26 align=ALIGN border=0></a>
     13 <a href=dpansa13.htm><img src=right.gif
     14  width=26 height=26 align=ALIGN border=0></a>
     15 </td>
     16 <td align=right>
     17 <a href=dpans.htm#toc><img src=up.gif 
     18  width=26 height=26 align=ALIGN border=0></a>
     19 <a name=A.12>Table of Contents</a>
     20 </td>
     21 </tr>
     22 </table>
     23 <p>
     24 <hr size=4>
     25  
     26 <H2>A.12 The optional Floating-Point word set</H2>
     27 
     28 The Technical Committee has considered many proposals dealing with the
     29 inclusion and makeup of the Floating-Point Word Sets in ANS Forth.
     30 Although it has been argued that ANS Forth should not address
     31 floating-point arithmetic and numerous Forth applications do not need
     32 floating-point, there are a growing number of important Forth
     33 applications from spreadsheets to scientific computations that require
     34 the use of floating-point arithmetic.  Initially the Technical Committee
     35 adopted proposals that made the Forth Vendors Group Floating-Point
     36 Standard, first published in 1984, the framework for inclusion of
     37 Floating-Point in ANS Forth.  There is substantial common practice and
     38 experience with the Forth Vendors Group Floating-Point Standard.
     39 Subsequently the Technical Committee adopted proposals that placed the
     40 basic floating-point arithmetic, stack and support words in the
     41 Floating-Point word set and the floating-point transcendental functions
     42 in the Floating-Point Extensions word set.  The Technical Committee also
     43 adopted proposals that:
     44 
     45 <UL>
     46 <LI>changed names for clarity and consistency; e.g., REALS to 
     47 <a href=dpans12.htm#12.6.1.1556>FLOATS</a>, and
     48 REAL+ to 
     49 <a href=dpans12.htm#12.6.1.1555>FLOAT+</a> .
     50 <LI>removed words; e.g., FPICK .
     51 <LI>added words for completeness and increased functionality; e.g.,
     52 <a href=dpans12.htm#12.6.2.1616>FSINCOS</a>, 
     53 <a href=dpans12.htm#12.6.2.1640>F~</a>, 
     54 <a href=dpans12.htm#12.6.2.1204>DF@</a>, 
     55 <a href=dpans12.htm#12.6.2.1203>DF!</a>, 
     56 <a href=dpans12.htm#12.6.2.2203>SF@</a> and 
     57 <a href=dpans12.htm#12.6.2.2202>SF!</a>
     58 </UL>
     59 <P>
     60 
     61 Several issues concerning the Floating-Point word set were resolved by
     62 consensus in the Technical Committee:
     63 
     64 <P>
     65 
     66 Floating-point stack: By default the floating-point stack is separate
     67 from the data and return stacks; however, an implementation may keep
     68 floating-point numbers on the data stack.  A program can determine
     69 whether floating-point numbers are kept on the data stack by passing the
     70 string 
     71 <a href=dpans12.htm#12.3.4>FLOATING-STACK</a> to 
     72 <a href=dpans6.htm#6.1.1345>ENVIRONMENT?</a> 
     73 It is the experience of several
     74 members of the Technical Committee that with proper coding practices it
     75 is possible to write floating-point code that will run identically on
     76 systems with a separate floating-point stack and with floating-point
     77 numbers kept on the data stack.
     78 
     79 <P>
     80 
     81 Floating-point input: The current base must be 
     82 <a href=dpans6.htm#6.1.1170>DECIMAL</a>.  Floating-point
     83 input is not allowed in an arbitrary base.  All floating-point numbers
     84 to be interpreted by an ANS Forth system must contain the exponent
     85 indicator <B>E</B> (see
     86 <a href=dpans12.htm#12.3.7>12.3.7</a> Text interpreter input number
     87 conversion).
     88 Consensus in the Technical Committee deemed this form of
     89 floating-point input to be in more common use than the alternative that
     90 would have a floating-point input mode that would allow numbers with
     91 embedded decimal points to be treated as floating-point numbers.
     92 
     93 <P>
     94 
     95 Floating-point representation: Although the format and precision of the
     96 significand and the format and range of the exponent of a floating-point
     97 number are implementation defined in ANS Forth, the Floating-Point
     98 Extensions word set contains the words DF@, SF@, DF!, and SF! for
     99 fetching and storing double- and single-precision IEEE
    100 floating-point-format numbers to memory.  The IEEE floating-point format
    101 is commonly used by numeric math co-processors and for exchange of
    102 floating-point data between programs and systems.
    103 
    104 <P>
    105 
    106 
    107 <hr>
    108 <a name=A.12.3>
    109 <H3>A.12.3 Additional usage requirements</H3>
    110 </a>
    111 
    112 
    113 
    114 <hr>
    115 <a name=A.12.3.5>
    116 <H4>A.12.3.5 Address alignment</H4>
    117 </a>
    118 
    119 In defining custom floating-point data structures, be aware that 
    120 <a href=dpans6.htm#6.1.1000>CREATE</a>
    121 doesn't necessarily leave the data space pointer aligned for various
    122 floating-point data types.  Programs may comply with the requirement for
    123 the various kinds of floating-point alignment by specifying the
    124 appropriate alignment both at compile-time and execution time.  For
    125 example:
    126 
    127 <PRE>
    128 : FCONSTANT ( F:  r -- )
    129     CREATE FALIGN  HERE  1 FLOATS ALLOT  F!
    130     DOES> ( F:  -- r )  FALIGNED F@ ;
    131 </PRE>
    132 
    133 <P>
    134 
    135 
    136 <hr>
    137 <a name=A.12.3.7>
    138 <H4>A.12.3.7 Text interpreter input number conversion</H4>
    139 </a>
    140 
    141 The Technical Committee has more than once received the suggestion that
    142 the text interpreter in Standard Forth systems should treat numbers that
    143 have an embedded decimal point, but no exponent, as floating-point
    144 numbers rather than double cell numbers.  This suggestion, although it
    145 has merit, has always been voted down because it would break too much
    146 existing code; many existing implementations put the full digit string
    147 on the stack as a double number and use other means to inform the
    148 application of the location of the decimal point.
    149 
    150 <p>
    151 <code>
    152 See:
    153 <a href=a0004.htm>RFI 0004</a> Number Conversion
    154 </code>
    155 <P>
    156 
    157 <hr>
    158 <a name=A.12.6>
    159 <H3>A.12.6 Glossary</H3>
    160 </a>
    161 
    162 
    163 
    164 <hr>
    165 <a name=A.12.6.1.0558>A.12.6.1.0558 &gt;FLOAT</A>
    166 <P>
    167 
    168 &gt;FLOAT enables programs to read floating-point data in legible ASCII
    169 format.  It accepts a much broader syntax than does the text interpreter
    170 since the latter defines rules for composing source programs whereas
    171 &gt;FLOAT defines rules for accepting data.  &gt;FLOAT is defined as broadly
    172 as is feasible to permit input of data from ANS Forth systems as well as
    173 other widely used standard programming environments.
    174 
    175 <P>
    176 
    177 This is a synthesis of common FORTRAN practice.  Embedded spaces are
    178 explicitly forbidden in much scientific usage, as are other field
    179 separators such as comma or slash.
    180 
    181 <P>
    182 
    183 While &gt;FLOAT is not required to treat a string of blanks as zero, this
    184 behavior is strongly encouraged, since a future version of ANS Forth may
    185 include such a requirement.
    186 
    187 <P>
    188 
    189 <hr>
    190 <a name=A.12.6.1.1427>A.12.6.1.1427 F.</A>
    191 <P>
    192 
    193 For example, 1E3 F. displays 1000. .
    194 
    195 <P>
    196 
    197 <hr>
    198 <a name=A.12.6.1.1492>A.12.6.1.1492 FCONSTANT</A>
    199 <P>
    200 
    201 Typical use:    <code>r FCONSTANT name</code>
    202 
    203 <P>
    204 
    205 <hr>
    206 <a name=A.12.6.1.1552>A.12.6.1.1552 FLITERAL</A>
    207 <P>
    208 
    209 Typical use:    <code>: X ... [ ... ( r ) ] FLITERAL ... ;</code>
    210 
    211 <P>
    212 
    213 <hr>
    214 <a name=A.12.6.1.1630>A.12.6.1.1630 FVARIABLE</A>
    215 <P>
    216 
    217 Typical use:    <code>FVARIABLE name</code>
    218 
    219 <P>
    220 
    221 <hr>
    222 <a name=A.12.6.1.2143>A.12.6.1.2143 REPRESENT</A>
    223 <P>
    224 
    225 This word provides a primitive for floating-point display.  Some
    226 floating-point formats, including those specified by IEEE-754, allow
    227 representations of numbers outside of an implementation-defined range.
    228 These include plus and minus infinities, denormalized numbers, and
    229 others.  In these cases we expect that REPRESENT will usually be
    230 implemented to return appropriate character strings, such as
    231 <B>+infinity</B> or <B>nan</B>, possibly truncated.
    232 
    233 <P>
    234 
    235 <hr>
    236 <a name=A.12.6.2.1489>A.12.6.2.1489 FATAN2</A>
    237 <P>
    238 
    239 <a href=dpans12.htm#12.6.2.1616>FSINCOS</a> 
    240 and 
    241 <a href=dpans12.htm#12.6.2.1489>FATAN2</a> 
    242 are a complementary pair of operators which convert
    243 angles to 2-vectors and vice-versa.  They are essential to most
    244 geometric and physical applications since they correctly and
    245 unambiguously handle this conversion in all cases except null vectors,
    246 even when the tangent of the angle would be infinite.
    247 
    248 <P>
    249 
    250 FSINCOS returns a Cartesian unit vector in the direction of the given
    251 angle, measured counter-clockwise from the positive X-axis.  The order
    252 of results on the stack, namely y underneath x, permits the 2-vector
    253 data type to be additionally viewed and used as a ratio approximating
    254 the tangent of the angle.  Thus the phrase FSINCOS 
    255 <a href=dpans12.htm#12.6.1.1430>F/</a> is functionally
    256 equivalent to 
    257 <a href=dpans12.htm#12.6.2.1625>FTAN</a>, 
    258 but is useful over only a limited and discontinuous
    259 range of angles, whereas FSINCOS and FATAN2 are useful for all angles.
    260 This ordering has been found convenient for nearly two decades, and has
    261 the added benefit of being easy to remember.  A corollary to this
    262 observation is that vectors in general should appear on the stack in
    263 this order.
    264 
    265 <P>
    266 
    267 The argument order for FATAN2 is the same, converting a vector in the
    268 conventional representation to a scalar angle.  Thus, for all angles,
    269 FSINCOS FATAN2 is an identity within the accuracy of the arithmetic and
    270 the argument range of FSINCOS.  Note that while FSINCOS always returns a
    271 valid unit vector, FATAN2 will accept any non-null vector.  An ambiguous
    272 condition exists if the vector argument to FATAN2 has zero magnitude.
    273 
    274 <P>
    275 
    276 <hr>
    277 <a name=A.12.6.2.1516>A.12.6.2.1516 FEXPM1</A>
    278 <P>
    279 
    280 This function allows accurate computation when its arguments are close
    281 to zero, and provides a useful base for the standard exponential
    282 functions.  Hyperbolic functions such as cosh(x) can be efficiently and
    283 accurately implemented by using FEXPM1; accuracy is lost in this
    284 function for small values of x if the word 
    285 <a href=dpans12.htm#12.6.2.1515>FEXP</a> is used.
    286 
    287 <P>
    288 
    289 An important application of this word is in finance; say a loan is
    290 repaid at 15% per year; what is the daily rate? On a computer with
    291 single precision (six decimal digit) accuracy:
    292 
    293 <P>
    294 
    295 1.  Using
    296 <a href=dpans12.htm#12.6.2.1553>FLN</a> and FEXP:
    297 <P>
    298 
    299 FLN of 1.15 = 0.139762,
    300 divide by 365 = 3.82910E-4,
    301 form the exponent using
    302 FEXP = 1.00038, and
    303 subtract one (1) and convert to percentage = 0.038%.
    304 
    305 <P>
    306 
    307 Thus we only have two digit accuracy.
    308 <P>
    309 
    310 
    311 2.  Using 
    312 <a href=dpans12.htm#12.6.2.1554>FLNP1</a> and FEXPM1:
    313 
    314 <P>
    315 
    316 FLNP1 of 0.15 = 0.139762, (this is the same value as in the first example,
    317 although with the argument closer to zero it may not be so)
    318 divide by 365 = 3.82910E-4,
    319 form the exponent and subtract one (1) using FEXPM1 = 3.82983E-4,
    320 and convert to percentage = 0.0382983%.
    321 
    322 <P>
    323 
    324 This is full six digit accuracy.
    325 <P>
    326 
    327 
    328 The presence of this word allows the hyperbolic functions to be computed with
    329 usable accuracy.  For example, the hyperbolic sine can be defined as:
    330 
    331 <PRE>
    332 : FSINH  ( r1 -- r2 )
    333     FEXPM1  FDUP  FDUP 1.0E0 F+  F/  F+  2.0E0 F/ ;
    334 </PRE>
    335 
    336 <P>
    337 
    338 <hr>
    339 <a name=A.12.6.2.1554>A.12.6.2.1554</a> FLNP1
    340 <P>
    341 
    342 This function allows accurate compilation when its arguments are close
    343 to zero, and provides a useful base for the standard logarithmic
    344 functions.  For example, 
    345 <a href=dpans12.htm#12.6.2.1553>FLN</a> can be implemented as:
    346 
    347 
    348 <PRE>
    349 	: FLN   1.0E0 F-  FLNP1 ;
    350 </PRE>
    351 <P>
    352 <code>
    353 See: 
    354 <a href=dpansa12.htm#A.12.6.2.1516>A.12.6.2.1516 FEXPM1</a>
    355 </code>
    356 <P>
    357 
    358 <hr>
    359 <a name=A.12.6.2.1616>A.12.6.2.1616</a> FSINCOS
    360 <P>
    361 <code>
    362 See:  
    363 <a href=dpansa12.htm#A.12.6.2.1489>A.12.6.2.1489 FATAN2</a>
    364 </code>
    365 <P>
    366 
    367 <hr>
    368 <a name=A.12.6.2.1640>A.12.6.2.1640</a> F~
    369 <P>
    370 
    371 This provides the three types of <B>floating point equality</B> in
    372 common use -- <B>close</B> in absolute terms, exact equality as
    373 represented, and <B>relatively close</B>.
    374 
    375 <P>
    376 
    377 <hr>
    378 <a href=dpans.htm#toc><IMG   src="up.gif" ></A>    Table of Contents 
    379 <BR>
    380 <a href=dpansa13.htm><IMG   src="right.gif" ></A>
    381 Next Section
    382 <P>
    383 </BODY>
    384 </HTML>