From Newsgroup: comp.lang.forth
On 2/28/2025 5:54 AM, Hans Bezemer wrote:> "Forth is a toy for nerds and
academics. It leads to endless philosophical debates, but no one is
actually doing something useful with the language."
I'll take the challenge. I often need to do quick little engineering
programs for work. Here's the last one I dashed off. Yes portions could
be optimized to avoid repeating calculations, but I thought clarity
suffered. You can persuade me otherwise. Yes I also eschew stack comments.
I just don't find they help me much.
#! /home/bmccool/bin/gforth
\ Floating point print formatted usage: total_width printf
: printf 5 5 f.rdp ; \ digits after decimal and significant digits set to 5
\ Print counter outputs from a 32.768 kHz input clock
: CounterOutput
cr
32.768e03 \ 32.768 kHz input clock
\ 1.280e02 \ 128 Hz input clock
\ 1.953125e-03 \ 1/512 Hz input clock
\ 3.90625e-03 \ 1/256 Hz input clock
\ 15.625e-03 \ 1/64 Hz input clock
\ 0.03125e \ 1/32 Hz input clock
\ 0.625e \ 1/16 Hz input clock
\ 1e \ 1 Hz input clock
\ 0.5e \ 1/2 Hz input clock
5 set-precision
fdup fe. ." Hz Clock Input" cr \ Print input clock
1 \ Start counting at bit 1
begin dup 35 <= \ While bit <= 35
while
dup 9 <= if ." " endif \ Print extra space in the output if bit <= 9 dup . ." Bit " \ Print bit
2.0e f/ fdup 9 printf ." Hz " \ Print cycles / sec \ The word "1/f" is the same as "1.0e fswap f/"
fdup 1/f 9 printf ." Sec " \ Print seconds
fdup 1/f 60.0e f/ 9 printf ." Min " \ Print minutes
fdup 1/f 60.0e f/ 60.0e f/ 9 printf ." Hour " \ Print hours
fdup 1/f 60.0e f/ 60.0e f/ 24.0e f/ 9 printf ." Day " cr \ Print days
1+
repeat
drop
fdrop
;
CounterOutput
32.768E3 Hz Clock Input
1 Bit 1.63840E4 Hz 6.1035E-5 Sec 1.0173E-6 Min 1.6954E-8 Hour 7.064E-10 Day
2 Bit 8.19200E3 Hz 1.2207E-4 Sec 2.0345E-6 Min 3.3908E-8 Hour 1.4129E-9 Day
3 Bit 4.09600E3 Hz 2.4414E-4 Sec 4.0690E-6 Min 6.7817E-8 Hour 2.8257E-9 Day
4 Bit 2.04800E3 Hz 4.8828E-4 Sec 8.1380E-6 Min 1.3563E-7 Hour 5.6514E-9 Day
5 Bit 1.02400E3 Hz 9.7656E-4 Sec 1.6276E-5 Min 2.7127E-7 Hour 1.1303E-8 Day
6 Bit 512.00000 Hz 1.9531E-3 Sec 3.2552E-5 Min 5.4253E-7 Hour 2.2606E-8 Day
7 Bit 256.00000 Hz 3.9062E-3 Sec 6.5104E-5 Min 1.0851E-6 Hour 4.5211E-8 Day
8 Bit 128.00000 Hz 7.8125E-3 Sec 1.3021E-4 Min 2.1701E-6 Hour 9.0422E-8 Day
9 Bit 64.00000 Hz 1.5625E-2 Sec 2.6042E-4 Min 4.3403E-6 Hour 1.8084E-7 Day 10 Bit 32.00000 Hz 3.1250E-2 Sec 5.2083E-4 Min 8.6806E-6 Hour 3.6169E-7 Day
11 Bit 16.00000 Hz 6.2500E-2 Sec 1.0417E-3 Min 1.7361E-5 Hour 7.2338E-7 Day
12 Bit 8.00000 Hz 0.12500 Sec 2.0833E-3 Min 3.4722E-5 Hour 1.4468E-6 Day
13 Bit 4.00000 Hz 0.25000 Sec 4.1667E-3 Min 6.9444E-5 Hour 2.8935E-6 Day
14 Bit 2.00000 Hz 0.50000 Sec 8.3333E-3 Min 1.3889E-4 Hour 5.7870E-6 Day
15 Bit 1.00000 Hz 1.00000 Sec 1.6667E-2 Min 2.7778E-4 Hour 1.1574E-5 Day
16 Bit 0.50000 Hz 2.00000 Sec 3.3333E-2 Min 5.5556E-4 Hour 2.3148E-5 Day
17 Bit 0.25000 Hz 4.00000 Sec 6.6667E-2 Min 1.1111E-3 Hour 4.6296E-5 Day
18 Bit 0.12500 Hz 8.00000 Sec 0.13333 Min 2.2222E-3 Hour 9.2593E-5 Day
19 Bit 6.2500E-2 Hz 16.00000 Sec 0.26667 Min 4.4444E-3 Hour 1.8519E-4 Day
20 Bit 3.1250E-2 Hz 32.00000 Sec 0.53333 Min 8.8889E-3 Hour 3.7037E-4 Day
21 Bit 1.5625E-2 Hz 64.00000 Sec 1.06667 Min 1.7778E-2 Hour 7.4074E-4 Day
22 Bit 7.8125E-3 Hz 128.00000 Sec 2.13333 Min 3.5556E-2 Hour 1.4815E-3 Day
23 Bit 3.9062E-3 Hz 256.00000 Sec 4.26667 Min 7.1111E-2 Hour 2.9630E-3 Day
24 Bit 1.9531E-3 Hz 512.00000 Sec 8.53333 Min 0.14222 Hour 5.9259E-3 Day
25 Bit 9.7656E-4 Hz 1.02400E3 Sec 17.06667 Min 0.28444 Hour 1.1852E-2 Day
26 Bit 4.8828E-4 Hz 2.04800E3 Sec 34.13333 Min 0.56889 Hour 2.3704E-2 Day
27 Bit 2.4414E-4 Hz 4.09600E3 Sec 68.26667 Min 1.13778 Hour 4.7407E-2 Day
28 Bit 1.2207E-4 Hz 8.19200E3 Sec 136.53333 Min 2.27556 Hour 9.4815E-2 Day
29 Bit 6.1035E-5 Hz 1.63840E4 Sec 273.06667 Min 4.55111 Hour 0.18963 Day
30 Bit 3.0518E-5 Hz 3.27680E4 Sec 546.13333 Min 9.10222 Hour 0.37926 Day
31 Bit 1.5259E-5 Hz 6.55360E4 Sec 1.09227E3 Min 18.20444 Hour 0.75852 Day
32 Bit 7.6294E-6 Hz 1.31072E5 Sec 2.18453E3 Min 36.40889 Hour 1.51704 Day
33 Bit 3.8147E-6 Hz 2.62144E5 Sec 4.36907E3 Min 72.81778 Hour 3.03407 Day
34 Bit 1.9073E-6 Hz 5.24288E5 Sec 8.73813E3 Min 145.63556 Hour 6.06815 Day
35 Bit 9.5367E-7 Hz 1.04858E6 Sec 1.74763E4 Min 291.27111 Hour 12.13630 Day
--- Synchronet 3.20c-Linux NewsLink 1.2