Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Forth > Fixed-point mat...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 8 Topic 3983 of 4050
Post > Topic >>

Fixed-point math, ANS, DPL and Win32Forth

by m_l_g3@[EMAIL PROTECTED] Apr 2, 2008 at 12:17 AM

Given that fixed-point math is 3 lines of code in Forth
((
10000 constant scale
: x* scale */ ;
: x/ scale swap */ ;
))
it is quite reasonable to have some built-in support for fixed point
number input.

Historically, there has been the variable DPL acquiring its value as a
side efect of number conversion.

For example,

1000 \ single, dpl=-1
1000. \ double, dpl=0
10.00 \ double, dpl=2

So,
1) Why DPL is not in the standard?
2) Why DPL is not in Win32Forth?
3) Is there an elegant way to implement fixed-point input in ANS?

I wrote fixed-point number input in ANS Forth, but it takes more lines
of code than the whole non-ANS (DPL-based) fixed point number support.


\ fixed-point number support
\ xn denotes a signed fiXed-point Number

: 10^ ( u -- 10^u ) 1 swap 0 ?do 10 * loop ;
4 constant logscale \ number of digits after decimal point

logscale 10^ constant scale
: x* ( xn1 xn2 -- xn3 ) scale */ ;
: x/ ( xn1 xn2 -- xn3 ) scale swap */ ;

: x ( d -- xn ) drop  logscale dpl @[EMAIL PROTECTED]
 0 max - dup abs 10^ swap 0< if /
else * then ;
: x. ( xn -- ) s>d tuck dabs <# logscale 0 ?do # loop [char] . hold #s
rot sign #> type space ;
: [x] ( "xn" -- ) ( run: -- xn ) postpone [ parse-word evaluate x ]
postpone literal ; immediate


\ the ANS version
: xx ( "xn" -- xn )
    parse-word over c@[EMAIL PROTECTED]
 [char] - = dup >r if 1 /string then
    0 0 2swap >number
    dup if
        over c@[EMAIL PROTECTED]
 [char] . <> abort" expected . inside a number" 1 /
string
        logscale min dup >r >number nip 0<> abort" canot convert" r>
    else
        nip
    then
    nip logscale swap - 10^ * r> if negate then
;
: [xx] ( "xn" -- ) ( run: -- xn ) xx postpone literal ; immediate



--
I post, therefore I am




 8 Posts in Topic:
Fixed-point math, ANS, DPL and Win32Forth
m_l_g3@[EMAIL PROTECTED]   2008-04-02 00:17:49 
Re: Fixed-point math, ANS, DPL and Win32Forth
Brad Eckert <nospaambr  2008-04-02 06:00:36 
Re: Fixed-point math, ANS, DPL and Win32Forth
Alex McDonald <blog@[E  2008-04-02 14:44:25 
Re: Fixed-point math, ANS, DPL and Win32Forth
George Hubert <georgea  2008-04-02 16:16:22 
Re: Fixed-point math, ANS, DPL and Win32Forth
m_l_g3 <m_l_g3@[EMAIL   2008-04-04 10:45:33 
Re: Fixed-point math, ANS, DPL and Win32Forth
Coos Haak <chforth@[EM  2008-04-04 20:06:46 
Re: Fixed-point math, ANS, DPL and Win32Forth
"Ed" <nospam  2008-04-05 11:43:43 
Re: Fixed-point math, ANS, DPL and Win32Forth
Albert van der Horst <  2008-04-05 09:27:59 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun May 11 20:01:30 CDT 2008.