\ @(#) floats.fth 98/02/26 1.4 17:51:40
\ High Level Forth support for Floating Point
\ Author: Phil Burk and Darren Gibbs
\ Copyright 1994 3DO, Phil Burk, Larry Polansky, Devid Rosenboom
\ The pForth software code is dedicated to the public domain,
\ and any third party may reproduce, distribute and modify
\ the pForth software code or any derivative works thereof
\ without any compensation or license. The pForth software
\ code is provided on an "as is" basis without any warranty
\ of any kind, including, without limitation, the implied
\ warranties of merchantability and fitness for a particular
\ purpose and their equivalents under the laws of any jurisdiction.
\ 19970702 PLB Drop 0.0 in REPRESENT to fix 0.0 F.
\ 19980220 PLB Added FG. , fixed up large and small formatting
\ 19980812 PLB Now don't drop 0.0 in REPRESENT to fix 0.0 F. (!!!)
\ Fixed F~ by using (F.EXACTLY)
: FALIGNED ( addr -- a-addr )
: FALIGN ( -- , align DP )
\ account for size of create when aligning floats
fp-create-size swap - constant CREATE_SIZE
: FALIGN.CREATE ( -- , align DP for float after CREATE )
: FCREATE ( <name> -- , create with float aligned data )
: FVARIABLE ( <name> -- ) ( F: -- )
FCREATE here 1 floats allot f!
: F0SP ( -- ) ( F: ? -- )
fdepth 0 max 0 ?DO fdrop LOOP
\ Convert between single precision and floating point
: S>F ( s -- ) ( F: -- r )
: F>S ( -- s ) ( F: r -- )
: (F.EXACTLY) ( r1 r2 -f- flag , return true if encoded equally ) { | caddr1 caddr2 fsize fcells }
fsize cell 1- + cell 1- invert and \ round up to nearest multiple of stack size
cell / -> fcells ( number of cells per float )
\ make room on data stack for floats data
\ compare bit representation
caddr1 fsize caddr2 fsize compare 0=
>r fcells 2* 0 ?DO drop LOOP r> \ drop float bits
: F~ ( -0- flag ) ( r1 r2 r3 -f- )
frot frot ( -- r3 r1 r2 )
fover fover ( -- r3 r1 r2 r1 r2 )
f- fabs ( -- r3 r1 r2 |r1-r2| )
frot frot ( -- r3 |r1-r2| r1 r2 )
fabs fswap fabs f+ ( -- r3 |r1-r2| |r1|+|r2| )
frot fabs f* ( -- |r1-r2| |r1|+|r2|*|r3| )
(f.exactly) \ f- f0= \ 19980812 Used to cheat. Now actually compares bit patterns.
frot frot ( -- r3 r1 r2 )
f- fabs ( -- r3 |r1-r2| )
\ FP Output --------------------------------------------------------
fvariable FVAR-REP \ scratch var for represent
: REPRESENT { c-addr u | n flag1 flag2 -- n flag1 flag2 , FLOATING } ( F: r -- )
TRUE -> flag2 \ FIXME - need to check range
fvar-rep f@ fabs fvar-rep f! \ absolute value
\ fdrop \ 19970702 \ 19980812 Remove FDROP to fix "0.0 F."
1 s>f f+ \ round up exponent
\ normalize r to u digits
1 s>f 2 s>f f/ f+ \ round result
\ convert float to double_int then convert to text
\ ." REP - d = " over . dup . cr
<# u 1- 0 ?DO # loop #s #> \ ( -- addr cnt )
\ Adjust exponent if rounding caused number of digits to increase.
\ For example from 9999 to 10000.
\ Set maximum digits that are meaningful for the precision that we use.
1 FLOATS 4 / 7 * constant FP_PRECISION_MAX
32 constant FP_REPRESENT_SIZE
64 constant FP_OUTPUT_SIZE
create FP-REPRESENT-PAD FP_REPRESENT_SIZE allot \ used with REPRESENT
create FP-OUTPUT-PAD FP_OUTPUT_SIZE allot \ used to assemble final output
variable FP-OUTPUT-PTR \ points into FP-OUTPUT-PAD
: FP.HOLD ( char -- , add char to output )
fp-output-ptr @ fp-output-pad 64 + <
: FP.APPEND { addr cnt -- , add string to output }
: FP.STRIP.TRAILING.ZEROS ( -- , remove trailing zeros from fp output )
fp-output-ptr @ fp-output-pad u>
fp-output-ptr @ 1- c@ [char] 0 =
: FP.APPEND.ZEROS ( numZeros -- )
: FP.MOVE.DECIMAL { n prec -- , append with decimal point shifted }
fp-represent-pad n prec min fp.append
: (EXP.) ( n -- addr cnt , convert exponent to two digit value )
: FP.REPRESENT ( -- n flag1 flag2 ) ( r -f- )
: (FS.) ( -- addr cnt ) ( F: r -- , scientific notation )
fp-output-pad fp-output-ptr ! \ setup pointer
fp-represent-pad precision represent
\ ." (FS.) - represent " fp-represent-pad precision type cr
1 precision fp.move.decimal
s" <FP-OUT-OF-RANGE>" fp.append
fp-output-pad fp-output-ptr @ over -
: FS. ( F: r -- , scientific notation )
: (FE.) ( -- addr cnt ) ( F: r -- , engineering notation ) { | n n3 -- }
fp-output-pad fp-output-ptr ! \ setup pointer
fp-represent-pad precision represent
\ convert exponent to multiple of three
n 1- s>d 3 fm/mod \ use floored divide
1+ precision fp.move.decimal \ amount to move decimal point
s" <FP-OUT-OF-RANGE>" fp.append
fp-output-pad fp-output-ptr @ over -
: FE. ( F: r -- , engineering notation )
: (FG.) ( F: r -- , normal or scientific ) { | n n3 ndiff -- }
fp-output-pad fp-output-ptr ! \ setup pointer
fp-represent-pad precision represent
\ compare n with precision to see whether we do scientific display
IF \ use exponential notation
1 precision fp.move.decimal
\ POSITIVE EXPONENT - place decimal point in middle
precision fp.move.decimal
\ NEGATIVE EXPONENT - use 0.000????
fp-represent-pad precision fp.append
s" <FP-OUT-OF-RANGE>" fp.append
fp-output-pad fp-output-ptr @ over -
: (F.) ( F: r -- , normal or scientific ) { | n n3 ndiff prec' -- }
fp-output-pad fp-output-ptr ! \ setup pointer
fp-represent-pad \ place to put number
fdup flog 1 s>f f+ f>s precision max
fp_precision_max min dup -> prec'
\ add '-' sign if negative
\ compare n with precision to see whether we must do scientific display
IF \ use exponential notation
1 precision fp.move.decimal
\ POSITIVE EXPONENT - place decimal point in middle
\ NEGATIVE EXPONENT - use 0.000????
fp-represent-pad precision rot + fp.append
s" <FP-OUT-OF-RANGE>" fp.append
fp-output-pad fp-output-ptr @ over -
: F.S ( -- , print FP stack )
fdepth i - 1- \ index of next float
\ FP Input ----------------------------------------------------------
variable FP-REQUIRE-E \ must we put an E in FP numbers?
false fp-require-e ! \ violate ANSI !!
: >FLOAT { c-addr u | dlo dhi u' fsign flag nshift -- flag }
c-addr c@ [char] - = dup -> fsign
IF 1 +-> c-addr -1 +-> u \ skip char
\ convert first set of digits
0 0 c-addr u >number -> u' -> c-addr -> dhi -> dlo
\ convert optional second set of digits
dlo dhi c-addr 1+ u' 1- dup -> nshift >number
-> u' -> c-addr -> dhi -> dlo
1 +-> c-addr -1 +-> u' \ skip E char
c-addr c@ [char] + = \ ignore + on exponent
1 +-> c-addr -1 +-> u' \ skip char
true -> flag \ allow "1E"
\ only require E field if this variable is true
fp-require-e @ not -> flag
\ convert double precision int to float
10 s>f nshift s>f f** f* \ apply exponent
3 constant NUM_TYPE_FLOAT \ possible return type for NUMBER?
: (FP.NUMBER?) ( $addr -- 0 | n 1 | d 2 | r 3 , convert string to number )
\ check to see if it is a valid float, if not use old (NUMBER?)
: FP.TERM ( -- , deinstall fp conversion )
what's fp.old.number? is number?
: FP.INIT ( -- , install FP converion )
what's number? is fp.old.number?
['] (fp.number?) is number?
." Floating point numeric conversion installed." cr
1.0 fsmall f- fconstant falmost1
." Should be 1.0 = " falmost1 f. cr
: TSEGF ( r -f- , print in all formats )
." --------------------------------" cr
fdup fs. 4 spaces fdup fe. 4 spaces
fdup fg. 4 spaces fdup f. cr
0.927 fsin 1.234e+22 f* tsegf