I'm curious if anyone can point[..]
out ways to make this program simpler, more readable, or better in any way.
: voltage_droop -4.0e 87.0e f/ f* ; ( From AC Power Supply specs )\ Most people will do:
byeWhy?
byeWhy?
On 9/20/2022 1:09 PM, Marcel Hendrix wrote:
byeWhy?
If I don't have the "bye", the script stays in gforth:
$ ./forth_droop.fth
.
.
.
I=104.00A V=48.97V DC P=5092.71W AC P=6123.99VA AC Cord I=30.62A
Gforth 0.7.3, Copyright (C) 1995-2008 Free Software Foundation, Inc.
Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'
Type `bye' to exit
Here's a little program I wrote in Forth to do some simple engineering calculations. Not being a Forth expert, I'm curious if anyone can point out ways to make this program simpler, more readable, or better in any way.
On Tuesday, September 20, 2022 at 10:50:38 PM UTC+2, Buzz McCool wrote:
On 9/20/2022 1:09 PM, Marcel Hendrix wrote:
byeWhy?
If I don't have the "bye", the script stays in gforth:
$ ./forth_droop.fth
.
.
.
I=104.00A V=48.97V DC P=5092.71W AC P=6123.99VA AC Cord I=30.62A
Gforth 0.7.3, Copyright (C) 1995-2008 Free Software Foundation, Inc.
Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'
Type `bye' to exit
That means that you can't chain or pipeline useful scripts together (without editing).
-marcel--
Thank goodness no S on the end.
There's an FDROP that's missing from the end.
On 21/09/2022 5:59 am, Buzz McCool wrote:
: .calc ( r -- )
fdup ." I=" 6 .rjust ." A" ( a ) fdup voltage_droop v fdup ." V=" 5 f.r ." V" ( a b)
dcp fdup ." DC P=" 7 f.r ." W" ( DC Power ) ( a*b)
acp ." AC P=" fdup 7 f.r ." VA" ( AC Power ) ( a*b/e/pf) line_cord_current ." AC Cord I=" 5 f.r ." A" cr ;
I've got NO idea what is actually coming in and going out - I'm not an electric engineer
But I like the lina behaviour better. If used in a pipe
or redirection, no extraneous prints occur.
~/euler$ lina < count.frt | wc
10 10 30
~/euler$
none albert schrieb am Mittwoch, 21. September 2022 um 13:41:08 UTC+2:
But I like the lina behaviour better. If used in a pipe
or redirection, no extraneous prints occur.
~/euler$ lina < count.frt | wc
10 10 30
~/euler$
This looks as if inputs piped through stdin are interpreted automatically, >which is not always desired. Eg most filters (classical pipe usage) have
to digest text, not Forth sources.
In article <17785a5c-67f3-4208...@googlegroups.com>,
minf...@arcor.de <minf...@arcor.de> wrote:
none albert schrieb am Mittwoch, 21. September 2022 um 13:41:08 UTC+2:
But I like the lina behaviour better. If used in a pipe
or redirection, no extraneous prints occur.
~/euler$ lina < count.frt | wc
10 10 30
~/euler$
This looks as if inputs piped through stdin are interpreted automatically, >which is not always desired. Eg most filters (classical pipe usage) haveWhat do you mean?
to digest text, not Forth sources.
1. source is text
2. a Forth used as a filter digests text, i.e. it QUITs it.
(repl read evaluate print loop is named QUIT in Forth).
Of course you can interpret text differently, called scripting.
If you want scripting, you can doit.
The following example is of a script that doubles each line on output.
This is how you do it in lina:
~/euler$ cat doit.frt
#!/usr/bin/lina -s
: my-interpret BEGIN '(ACCEPT) CATCH 0= WHILE 2DUP TYPE CR TYPE CR REPEAT ; my-interpret
~/euler$ cat doit.frt
#!/usr/bin/lina -s
: my-interpret BEGIN '(ACCEPT) CATCH 0= WHILE 2DUP TYPE CR TYPE CR REPEAT ; my-interpret
# This particular interpreter doubles each line:
~/euler$ doit.frt < doit.frt
#!/usr/bin/lina -s
#!/usr/bin/lina -s
: my-interpret BEGIN '(ACCEPT) CATCH 0= WHILE 2DUP TYPE CR TYPE CR REPEAT ;
: my-interpret BEGIN '(ACCEPT) CATCH 0= WHILE 2DUP TYPE CR TYPE CR REPEAT ; my-interpret
my-interpret
You need not define an interpreter explicitly.
"
BEGIN '(ACCEPT) CATCH 0= WHILE 2DUP TYPE CR TYPE CR REPEAT
"
is sufficient.
Note: (ACCEPT) is like ACCEPT without the hassle to define a buffer.
Note: '(ACCEPT) is like ' ACCEPT or ['] ACCEPT (I can't remember which).
Okay. Would this work?
lina filterprogram < inputdatafile | sort > outputdatafile
Okay. Would this work?
lina filterprogram < inputdatafile | sort > outputdatafile
none albert schrieb am Mittwoch, 21. September 2022 um 13:41:08 UTC+2:
This looks as if inputs piped through stdin are interpreted automatically, which is not always desired. Eg most filters (classical pipe usage) have
to digest text, not Forth sources.
On Tuesday, September 20, 2022 at 2:59:49 PM UTC-5, Buzz McCool wrote:If you have 64bit precision, you could also do "Brodie math". That gives you about
Using integer frog math:
go
00099 const PF ( power factor )
00084 const EF ( efficiency )
05375 const DC_MAX_V ( max DC voltage )
20000 const AC_MIN_V ( min AC voltage )
10400 const DC_MAX_I ( max DC current )
: voltage_droop -400 8700 */ ; ( From AC Power Supply specs )
: v DC_MAX_V + ;
: dcp 100 */ ;
: acp 100 EF */ 100 PF */ ;
: line_cord_current 100 AC_MIN_V */ ;
( Print formatted syntax: nr nd np .rdp )
( s1 unit string )
( s2 item string )
( nr total width of the output )
( nd number of digits after the decimal point )
( np minimum number of significant digits )
: .rdp ( n s1 s2 nr nd np -- )
rot >r \ rack nr
r \ rack nptell \ print s2, item
r \ rack nd
swap 0 <# r> 0 ?do # loop asc . hold r> 0 ?do # loop #s #>
over - 0 max spacestype \ print value
tell \ print s1, units
;
: calculations ( _for_ac_power_supply )
0000 ( Initialize current to 00.00 )
BEGIN
dup DC_MAX_I <= WHILE
dup dup dup "A" "I=" 6 2 2 .rdp
voltage_droop v dup "V" "V=" 5 2 2 .rdp
dcp dup "W" "DC P=" 7 2 2 .rdp ( DC Power )
acp dup "VA" "AC P=" 7 2 2 .rdp ( AC Power )
line_cord_current "A" "AC Cord I=" 5 2 2 .rdp cr
100 +
REPEAT DROP
;
I=000.00AV=053.75VDC P= 000.00WAC P= 000.00VAAC Cord I=000.00A I=001.00AV=053.71VDC P= 053.71WAC P= 064.58VAAC Cord I=000.32A I=002.00AV=053.66VDC P= 107.32WAC P= 129.05VAAC Cord I=000.64A I=003.00AV=053.62VDC P= 160.86WAC P= 193.43VAAC Cord I=000.96A I=004.00AV=053.57VDC P= 214.28WAC P= 257.66VAAC Cord I=001.28A
...
I=102.00AV=049.07VDC P=5005.14WAC P=6018.68VAAC Cord I=030.09A I=103.00AV=049.02VDC P=5049.06WAC P=6071.49VAAC Cord I=030.35A I=104.00AV=048.97VDC P=5092.88WAC P=6124.19VAAC Cord I=030.62A
-fin-
--
me
If you have 64bit precision, you could also do "Brodie math". That gives you about
4 decimal places of precision throughout:
https://sourceforge.net/p/forth-4th/wiki/Fixed%20point%20arithmetic/
On Friday, September 23, 2022 at 12:25:04 PM UTC-5, minf...@arcor.de wrote:
none albert schrieb am Mittwoch, 21. September 2022 um 13:41:08 UTC+2:
This looks as if inputs piped through stdin are interpreted automatically, which is not always desired. Eg most filters (classical pipe usage) have to digest text, not Forth sources.
300A150A1010443B4F040411551711071C0A0E5505150A53081B5506171C074500000016165F4503550A17061
If you have 64bit precision, you could also do "Brodie math". That gives you about
4 decimal places of precision throughout:
https://sourceforge.net/p/forth-4th/wiki/Fixed%20point%20arithmetic/
"Brodie math" seems to have been forgotten a bit in the Forth community. Which
is a shame, because current word width only makes is more viable.
BTW, it's *ridiculously* fast - and quite flexible.
On Sunday, September 25, 2022 at 9:49:39 AM UTC-5, S Jack wrote:
300A150A1010443B4F040411551711071C0A0E5505150A53081B5506171C074500000016165F4503550A17061
Strange, on my terminal that was a nice block of code of 80 column rows. This editor
stripped out the newlines. If you hit post to reply, the code in block form is seen.
Anyway, some notes on the Forth script:
i. ANON ... ; is for anonymous which is my :NONAME
i. XYZ is the encoder
i. HDREAD ( xt h -- ) is reading the handle h (0) and executing xt (the anon word) on each line of input.
--
me
On Sunday, September 25, 2022 at 6:13:36 AM UTC-5, the.bee...@gmail.com wrote:
If you have 64bit precision, you could also do "Brodie math". That gives you about
4 decimal places of precision throughout:
https://sourceforge.net/p/forth-4th/wiki/Fixed%20point%20arithmetic/
6 tiny words for 64 bit fix point math; hard to beat.
tks
--
me
On Sunday, September 25, 2022 at 9:49:39 AM UTC-5, S Jack wrote:
300A150A1010443B4F040411551711071C0A0E5505150A53081B5506171C074500000016165F4503550A17061
Strange, on my terminal that was a nice block of code of 80 column rows. This editor
stripped out the newlines. If you hit post to reply, the code in block form is seen.
Anyway, some notes on the Forth script:
i. ANON ... ; is for anonymous which is my :NONAME
i. XYZ is the encoder
i. HDREAD ( xt h -- ) is reading the handle h (0) and executing xt (the anon word) on each line of input.
--
me
On Sunday, September 25, 2022 at 2:03:44 PM UTC-5, S Jack wrote:
A while back a did a little piece on decimal (fixed point) numbers which made use
of the dot in a number making the number double and setting DPL to its position.
On Saturday, September 24, 2022 at 6:58:20 AM UTC+2, S Jack wrote:<SNIP>
On Tuesday, September 20, 2022 at 2:59:49 PM UTC-5, Buzz McCool wrote:
Using integer frog math:
go
00099 const PF ( power factor )"Brodie math" seems to have been forgotten a bit in the Forth community. Which >is a shame, because current word width only makes is more viable.
00084 const EF ( efficiency )
05375 const DC_MAX_V ( max DC voltage )
20000 const AC_MIN_V ( min AC voltage )
10400 const DC_MAX_I ( max DC current )
BTW, it's *ridiculously* fast - and quite flexible.
Hans Bezemer
On current Intel/AMD CPUs, integer multiplication and division is done
on the FPU,
This my last.
Here's a little program I wrote in Forth to do some simple engineering calculations. Not being a Forth expert, I'm curious if anyone can point
out ways to make this program simpler, more readable, or better in any way.
On 9/20/2022 12:59 PM, Buzz McCool wrote:
Here's a little program I wrote in Forth to do some simple engineering calculations. Not being a Forth expert, I'm curious if anyone can point out ways to make this program simpler, more readable, or better in any way.
Here's what I have after the excellent suggestions I received. I think it is indeed simpler, more readable, and better all around. Thank you!
#! /usr/bin/gforth
\ Calculations for an AC Power Supply
\ Prints current, voltage, DC power, AC power, & AC line cord current 1.0e fconstant AMPS_TO_INCREMENT \ Number of amps to increment
0.99e fconstant PF \ Floating point constant for power factor
0.84e fconstant E \ ... efficiency
53.75e fconstant DC_MAX_V \ ... max DC voltage
200.0e fconstant AC_MIN_V \ ... min AC voltage
104.0e fconstant DC_MAX_I \ ... max DC current
\ Print float formatted syntax: nr nd np f.rdp
\ nr total width of the output
\ nd number of digits after the decimal point
\ np minimum number of significant digits
\ Since nd & np are being kept the same, define a new word
: f.r 2 2 f.rdp ; ( F: r width --)
\ The ( comment) after a definition describes the stack effect.
\ The "F:" denotes the floating point stack, and "r" a real number.
\ Before the `--' is the state of the stack before definition execution
\ After the `--' is the state of the stack after the execution
: curr fdup ." I=" 6 f.r ." A" ; ( F: r -- r)
\ "[" switches from compiling mode to interpreter mode, allowing the
\ inlining of constants or code into word definitions
\ "fliteral" has the arithmetic in [ ] performed only once at compile time
\ -4.0e & 87.0e are droop slope numbers from the power supply specification
: voltage_droop [ -4.0e 87.0e f/ ] fliteral f* ; ( F: r1 -- r2)
: volt fdup voltage_droop DC_MAX_V f+ fdup ." V=" 5 f.r ." V" ; ( F: r1 -- r1 r2)
: dcp f* fdup ." DCP=" 7 f.r ." W" ; ( F: r1 r2 -- r3)
: acp E f/ PF f/ fdup ." ACP=" 7 f.r ." VA" ; ( F: r1 -- r2)
: line_cord_curr AC_MIN_V f/ ." AC Cord I=" 5 f.r ." A" cr ; ( F: r --)
: calculations_for_ac_power_supply
0.0e \ Initialize current to 0.0 amps
begin fdup DC_MAX_I f<= while
fdup curr volt dcp acp line_cord_curr AMPS_TO_INCREMENT f+
repeat ;
calculations_for_ac_power_supply
fdrop \ remove leftover current loop count value
bye
On Monday, September 26, 2022 at 3:43:39 AM UTC-5, S Jack wrote:For Hans, using the "Brodie" constant:
Reworked it so parameters could be given in un-scaled form.
fdrop should be placed after repeat i.e.
repeat fdrop ;
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 51:33:35 |
Calls: | 6,712 |
Calls today: | 5 |
Files: | 12,243 |
Messages: | 5,355,038 |
Posted today: | 1 |