Divn
Full title | Divn |
Year of release | 2007 |
Publisher | Ricardo Fernandes Lopes |
Producer / Author(s) | Ricardo Fernandes Lopes |
Memory | 3K |
Type | Utility |
Cost : | PD |
Download | Divn
[CRC32 E0CF7CE8] Distribution Permission Allowed | Group 1 |
Instructions
A code to divide a number to n places using integer maths.
22 7 20 /n will return
3.14285714285714285714
Algorithm: ________________ x = a/b a = a - b*x print x. loop from 1 to n: a = 10*a x = a/b a = a - b*x print x end loop : BS ( backspace ) 15388 @ 1- 15388 ! ; : X ( b a -- b a' x , x=a/b and a'=a-b*x ) OVER OVER OVER ( b a b a b ) / ( b a b x ) DUP >R ( b a b x ) * - R> ( b a' x ) ; : /N ( a b n -- ) CR SWAP ROT ( n b a ) X . BS ( print integer part ) ASCII . EMIT ( decimal separator ) ROT 0 ( b a n 0 ) DO ( print n fractional digits ) 10 * ( 10*a ) X ASCII 0 + EMIT ( print one digit ) LOOP DROP DROP CR ;