Re: Representing decimal part as regular integer on low 16 bits?



Op Tue, 12 Sep 2006 23:32:15 +0200 schreef Frank Buss:

<snip>
A quick and dirty implementation in Forth:

A little bit too quick and too dirty I might add ;-)

\ reads a number with 3 digits
: n3 ( n n -- n) drop ;
With a 16 bit implementation you lose too much precision here!

\ multiplies two 3 digit numbers
: *3 ( n n -- n ) * 1000 / ;

Remember the */ operator that every Forth has, much better precision:
: *3 ( n n -- n ) 1000 */ ;


\ divides two 3 digit number
: /3 ( n n -- n ) swap 1000 * swap / ;

Here too:
: /3 ( n n -- n ) 1000 swap */ ;

1.234 n3 42.123 n3 *3 .3 -> 51.979
Does not do what you want in a 16 bit Forth.

51.979 n3 42.123 n3 /3 .3 -> 1.233

Is it possible to write a parser for "n3" in Forth, which reads the next
number sequence like "create" reads the next char sequence? I've read
something about >in, source etc., but don't have an idea how to use it.

In short, use doubles for 16 bit implementations: high word the integer
part, the low word as the fraction times 65536. Adding and subtracting with
the normal D+ and D-.

For 32 bit implementations you could use single precision and normal + and
-. Decimal point between bit 15 and 16.

--
Coos

CHForth, 16 bit DOS applications
http://home.hccnet.nl/j.j.haak/forth.html
.



Relevant Pages

  • Re: floating point round off errors
    ... using higher precision internally in critical areas). ... The nice thing about verilog implementations is that you ... Verilog and VHDL both. ...
    (comp.arch.fpga)
  • Re: Very big numbers
    ... loosing any precision (and you have compiler that can do it) use long ... long int. ... In many implementations long double is capable to hold any integer ...
    (comp.lang.c)
  • Re: unsigned long to double
    ... viza wrote: ... integers can be converted to double without loss of precision. ... implementations with 32-bit longs are still pretty common. ...
    (comp.lang.c)
  • Re: Kind of NOT integer constant
    ... A processor that does NOTas a default precision operation ... set bits right adjusted in the extended precision result). ... consistent and standard conforming. ... For implementations that aren't two's complement, ...
    (comp.lang.fortran)