• Re: Forth-tran

    From Marcel Hendrix@21:1/5 to Buzz McCool on Tue Sep 20 13:09:58 2022
    On Tuesday, September 20, 2022 at 9:59:49 PM UTC+2, Buzz McCool wrote:
    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:
    : voltage_droop ( F: current -- droop) [ -4.0e 87.0e f/ ] FLITERAL f* ;
    \ Don't forget the stack comments.
    [..]
    bye
    Why?

    -marcel

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Buzz McCool@21:1/5 to All on Tue Sep 20 12:59:46 2022
    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.


    #! /usr/bin/gforth
    ( Calculations for an AC Power Supply )
    ( Prints current, voltage, DC power, AC power, & AC line cord current.)
    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 )
    : voltage_droop -4.0e 87.0e f/ f* ; ( From AC Power Supply specs )
    : v DC_MAX_V f+ ;
    : dcp f* ;
    : acp E f/ PF f/ ;
    : line_cord_current AC_MIN_V f/ ;
    ( 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 )
    : calculations_for_ac_power_supply
    0.0e begin fdup ( Initialize current to 0.0 )
    DC_MAX_I f<= while fdup fdup ." I=" 6 2 2 f.rdp ." A" fdup
    voltage_droop v fdup ." V=" 5 2 2 f.rdp ." V"
    dcp fdup ." DC P=" 7 2 2 f.rdp ." W" ( DC Power )
    acp ." AC P=" fdup 7 2 2 f.rdp ." VA" ( AC Power )
    line_cord_current ." AC Cord I=" 5 2 2 f.rdp ." A" cr
    1.0e f+ repeat ;
    calculations_for_ac_power_supply
    bye


    $ ./forth_droop.fth
    I= 0.00A V=53.75V DC P= 0.00W AC P= 0.00VA AC Cord I= 0.00A
    I= 1.00A V=53.70V DC P= 53.70W AC P= 64.58VA AC Cord I= 0.32A
    I= 2.00A V=53.66V DC P= 107.32W AC P= 129.05VA AC Cord I= 0.65A
    I= 3.00A V=53.61V DC P= 160.84W AC P= 193.41VA AC Cord I= 0.97A
    .
    .
    .
    I=102.00A V=49.06V DC P=5004.16W AC P=6017.50VA AC Cord I=30.09A
    I=103.00A V=49.01V DC P=5048.48W AC P=6070.80VA AC Cord I=30.35A
    I=104.00A V=48.97V DC P=5092.71W AC P=6123.99VA AC Cord I=30.62A

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Buzz McCool@21:1/5 to Marcel Hendrix on Tue Sep 20 13:50:35 2022
    Thanks for the tips Marcel. I'll brush up stack comments and literals.

    On 9/20/2022 1:09 PM, Marcel Hendrix wrote:
    bye
    Why?

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marcel Hendrix@21:1/5 to Buzz McCool on Tue Sep 20 15:40:03 2022
    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:
    bye
    Why?

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxforth@21:1/5 to Buzz McCool on Wed Sep 21 13:04:10 2022
    On 21/09/2022 5:59 am, 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.


    I'd factor it something like this.

    : f.r ( r width -- ) 2 2 f.rdp ;

    : .calc ( r -- )
    fdup ." I=" 6 .rjust ." A"
    fdup voltage_droop v fdup ." V=" 5 f.r ." V"
    dcp fdup ." DC P=" 7 f.r ." W" ( DC Power )
    acp ." AC P=" fdup 7 f.r ." VA" ( AC Power )
    line_cord_current ." AC Cord I=" 5 f.r ." A" cr ;

    : calculations ( -- )
    0.0e ( Initialize current to 0.0 )
    begin fdup DC_MAX_I f<= while
    fdup .calc 1.0e f+
    repeat ;

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxforth@21:1/5 to dxforth on Wed Sep 21 13:06:28 2022
    On 21/09/2022 1:04 pm, dxforth wrote:

    Correction:

    : f.r ( r width -- )  2 2 rot f.rdp ;

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to mhx@iae.nl on Wed Sep 21 13:41:02 2022
    In article <ca91a8e0-75bc-4208-86a5-9ef526c377cen@googlegroups.com>,
    Marcel Hendrix <mhx@iae.nl> wrote:
    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:
    bye
    Why?

    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).

    The opposite is true.

    Try
    -------------8<-----------------------
    : count 10 0 DO I . CR LOOP ;
    count
    bye
    -------------8<-----------------------

    Store it on count.frt

    Without the `bye
    gforth count.frt | wc
    lands in limbo.

    As is, it works as intended:
    gforth count.frt | wc
    redefined count 10 10 30

    At least in 0.7.3 it makes no difference if you use
    #! /usr/bin/gforth
    to use it in script fashion.

    In redirection at least gforth terminates at end of pipe:
    ~/euler$ gforth <count.frt | wc
    redefined count 17 47 237
    ~/euler$

    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$


    Groetjes Albert



    -marcel
    --
    "in our communism country Viet Nam, people are forced to be
    alive and in the western country like US, people are free to
    die from Covid 19 lol" duc ha
    albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Fourthy Forth@21:1/5 to All on Wed Sep 21 09:39:53 2022
    Thank goodness no S on the end.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxforth@21:1/5 to Fourthy Forth on Thu Sep 22 15:28:54 2022
    On 22/09/2022 2:39 am, Fourthy Forth wrote:
    Thank goodness no S on the end.

    Too cryptic for me. There's an FDROP that's missing from the end.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Buzz McCool@21:1/5 to dxforth on Thu Sep 22 07:22:35 2022
    On 9/21/2022 10:28 PM, dxforth wrote:
    There's an FDROP that's missing from the end.

    Thanks for this and the refactoring ideas!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Hans Bezemer@21:1/5 to dxforth on Fri Sep 23 06:16:47 2022
    On Wednesday, September 21, 2022 at 5:04:13 AM UTC+2, dxforth wrote:
    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 like that one - but something still bothers me about "calc". I'd probably factor out all individual print statements there,
    so I end up with something like:

    : calc .i .voltage_droop .dcp .acp .line_cord_current ;

    But then again - there seems to be dependencies between the routines, which defines the chain that has to be created.
    In any case - if you document the stack diagram, it may become much more clear what is actually happening there:

    : .i fdup ." I=" 6 .rjust ." A" ; ( F: r -- r)
    : .voltage_droop fdup voltage_droop v fdup ." V=" 5 f.r ." V" ; ( F: r1 -- r1 r2)
    : .dcp dcp fdup ." DC P=" 7 f.r ." W" ; ( F: r1 r2 -- r3)
    : .acp acp ." AC P=" fdup 7 f.r ." VA" ; ( F: r1 -- r2)
    : .line_cord_current line_cord_current ." AC Cord I=" 5 f.r ." A" cr ; ( F: r --)

    I've got NO idea what is actually coming in and going out - I'm not an electric engineer :(

    Hans Bezemer

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Buzz McCool@21:1/5 to Hans Bezemer on Fri Sep 23 07:38:31 2022
    On 9/23/2022 6:16 AM, Hans Bezemer wrote:
    I've got NO idea what is actually coming in and going out - I'm not an electric engineer

    Thanks for the examples. I'll make you an honorary "double E" for trying.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From minforth@arcor.de@21:1/5 to none albert on Fri Sep 23 10:25:03 2022
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From S Jack@21:1/5 to All on Fri Sep 23 21:58:18 2022
    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 )
    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 np
    >r \ rack nd
    tell \ print s2, item
    swap 0 <# r> 0 ?do # loop asc . hold r> 0 ?do # loop #s #>
    r> over - 0 max spaces
    type \ 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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to minf...@arcor.de on Sat Sep 24 10:21:32 2022
    In article <17785a5c-67f3-4208-bcf0-59d51cc625e3n@googlegroups.com>, minf...@arcor.de <minforth@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) have
    to digest text, not Forth sources.

    What do you mean?
    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).

    Groetjes Albert
    --
    "in our communism country Viet Nam, people are forced to be
    alive and in the western country like US, people are free to
    die from Covid 19 lol" duc ha
    albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From minforth@arcor.de@21:1/5 to none albert on Sat Sep 24 08:41:25 2022
    none albert schrieb am Samstag, 24. September 2022 um 10:21:35 UTC+2:
    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) have
    to digest text, not Forth sources.
    What do you mean?
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From S Jack@21:1/5 to minf...@arcor.de on Sat Sep 24 10:26:18 2022
    On Saturday, September 24, 2022 at 10:41:26 AM UTC-5, minf...@arcor.de wrote:
    Okay. Would this work?
    lina filterprogram < inputdatafile | sort > outputdatafile

    :) cat ac_power | frogd '5 hload bye' 5<&0 | head

    I= 000.00A V= 053.75V DC P= 000.00W AC P= 000.00VA AC Cord I= 000.00A
    I= 001.00A V= 053.71V DC P= 053.71W AC P= 064.58VA AC Cord I= 000.32A
    I= 002.00A V= 053.66V DC P= 107.32W AC P= 129.05VA AC Cord I= 000.64A
    I= 003.00A V= 053.62V DC P= 160.86W AC P= 193.43VA AC Cord I= 000.96A
    I= 004.00A V= 053.57V DC P= 214.28W AC P= 257.66VA AC Cord I= 001.28A
    I= 005.00A V= 053.53V DC P= 267.65W AC P= 321.84VA AC Cord I= 001.60A
    I= 006.00A V= 053.48V DC P= 320.88W AC P= 385.85VA AC Cord I= 001.92A
    I= 007.00A V= 053.43V DC P= 374.01W AC P= 449.74VA AC Cord I= 002.24A
    I= 008.00A V= 053.39V DC P= 427.12W AC P= 513.60VA AC Cord I= 002.56A
    :) --
    me

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to All on Sat Sep 24 20:48:01 2022
    In article <4f366b7c-c04a-4181-b31a-3621b951ab73n@googlegroups.com>,
    <SNIP>

    Okay. Would this work?
    lina filterprogram < inputdatafile | sort > outputdatafile

    Of course not. Is the above example not clear enough?

    You could try
    lina -f ' INCLUDE filterprogram' < inputdatafile | sort > outputdatafile

    Normally I would do ( avoiding recompilation).

    lina -c filter.frt

    cat inputdatafile | filter| sort > outputdatafile.

    Or you can add a first line in 'filterprogram' by
    #!/usr/bin/lina -s

    (and make filterprogram executable by `` chmod -=x ''

    lina has a dozen faces, choosen by the first option
    ( -a :_Make_want_available_silently ) \ AvdH B0nov25
    ( -c PROGRAM :_compile_PROGRAM_to_binary ) \ AvdH C0jun03
    ( -d :_add_defined_then_include )
    ( -e :_Load_system_electives ) \ AvdH A3sep01
    ( -f :_Forth_words_to_be_executed_80_chars) \ AvdH A1oct05
    ( -g GROW :_grow_by_megabytes ) \ AvdH B4may28
    ( -h :_help,_show_options ) \ AvdH B6feb26
    ( -i BINARY-PATH LIBRARY-PATH SHELL-PATH ) \ C2jul10
    ( -l LIBRARY :_LIBRARY_to_be_used_for_blocks ) \ AvdH C2feb22
    ( -m/--/--help/--version :_Help_and_version_information )
    ( -s SCRIPT-FILE :_Interpret_SCRIPT-FILE ) \ AvdH C0jun03
    ( -t FILE :_Try_to_compile_FILE_by_all_means ) \ AvdH A1oct26
    ( -v :_Version_and_copyright_information_)
    ( -w :_Make_want_available ) \ AvdH B2sep22

    Other options are available. You can add a -b option by filling
    in the second screen in forth.lab.


    Groetjes Albert
    --
    "in our communism country Viet Nam, people are forced to be
    alive and in the western country like US, people are free to
    die from Covid 19 lol" duc ha
    albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From S Jack@21:1/5 to minf...@arcor.de on Sat Sep 24 13:30:28 2022
    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.

    cat ac_power | frogd '5 hload bye' 5<&0 | head | frogd '"f/fdata.f" fload "job" /go anon cr ." BOO " type ; 5 hdread bye' 5<&0

    BOO
    BOO I= 000.00A V= 053.75V DC P= 000.00W AC P= 000.00VA AC Cord I= 000.00A BOO I= 001.00A V= 053.71V DC P= 053.71W AC P= 064.58VA AC Cord I= 000.32A BOO I= 002.00A V= 053.66V DC P= 107.32W AC P= 129.05VA AC Cord I= 000.64A BOO I= 003.00A V= 053.62V DC P= 160.86W AC P= 193.43VA AC Cord I= 000.96A BOO I= 004.00A V= 053.57V DC P= 214.28W AC P= 257.66VA AC Cord I= 001.28A BOO I= 005.00A V= 053.53V DC P= 267.65W AC P= 321.84VA AC Cord I= 001.60A BOO I= 006.00A V= 053.48V DC P= 320.88W AC P= 385.85VA AC Cord I= 001.92A BOO I= 007.00A V= 053.43V DC P= 374.01W AC P= 449.74VA AC Cord I= 002.24A BOO I= 008.00A V= 053.39V DC P= 427.12W AC P= 513.60VA AC Cord I= 002.56A :)

    i. First frogd, with HLOAD, executed piped script generating data;
    Frogd supplied the 'bye' (optional).
    Data was further piped to 'head' to limit amount of data.
    i. Second frogd loaded some Forth code, HDREAD, to act as filter to
    pre-pend "BOO" to the piped data. Note the pre-pending action was
    defined in the arguments (just for show).

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Hans Bezemer@21:1/5 to S Jack on Sun Sep 25 04:13:34 2022
    On Saturday, September 24, 2022 at 6:58:20 AM UTC+2, S Jack wrote:
    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 )
    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 np
    r \ rack nd
    tell \ print s2, item
    swap 0 <# r> 0 ?do # loop asc . hold r> 0 ?do # loop #s #>
    over - 0 max spaces
    type \ 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/

    "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.

    Hans Bezemer

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From S Jack@21:1/5 to the.bee...@gmail.com on Sun Sep 25 07:35:24 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From S Jack@21:1/5 to S Jack on Sun Sep 25 07:49:38 2022
    On Saturday, September 24, 2022 at 3:30:29 PM UTC-5, S Jack wrote:
    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.

    Using Forth sh script to encode piped in text data:
    :) cat encode
    #! b/myfrog INC
    "---" xyz:key smv \ set key ( key edited out --- )
    0 xyz:key.index !
    cr cr anon char+ xyz tell ; 0 hdread
    bye
    :) cat txt/twheels.txt | ./encode


    300A150A1010443B4F040411551711071C0A0E5505150A53081B5506171C074500000016165F4503550A1706140D001A1652001545111409174F1202075944780C120807550B040A0145151C101A4F1B0C11550B1E0B16174217161D1B1B00105217520D1A061B160817415332075508170E1D0006550D064F0615421403130
    61D16167F05521812090E55051C0B532C4216081B02110006550B1C4153260D000816015411420701130C1B45161D01521F160103191752061545111005060A1745111A44181A00116806101D0017450D1B44060716451210001303004B42251101071601421A02144F15170D1844060716451514081E4F120B065509130B16
    450B0144134F150A0D01441D1D7911151A44100A150A101044140E1F090B1B0352000500105B4435000745170548521F06160A1000521B1B0042170D190A530703160F520E1D0142110D164F1A1142140313061D4B68220D06071A0B421444040A011C42060C1D1D0745161C09174F3A45151417520E11090755101D4F010C0
    61044131D1C100C1144060716450A1A11010A53110D55101A0A790703160F521612170655131A0A0100423C44050006090655160701530C0C010B521B1B004213011C0C1645161A44011B1C155955001B0B1D4216550F1C00044503170B071B790710140F171C5D4542380B060716171155101A061D0E421D0D15071F1C421A
    02521B01040B1B0D1C0853120A10011E1C5F45000010521D16014217081D0017000655061D16006F0C1012171D53020B0301521B1B000F5505521B1B0A17120C064179:)
    --
    me

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From S Jack@21:1/5 to S Jack on Sun Sep 25 08:07:16 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marcel Hendrix@21:1/5 to the.bee...@gmail.com on Sun Sep 25 08:56:46 2022
    On Sunday, September 25, 2022 at 1:13:36 PM UTC+2, 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/

    "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 current Intel/AMD CPUs, integer multiplication and division is done
    on the FPU, and addition is single cycle (if memory/registers allow).
    I don't think 'ridiculous' is more than a few percent, and flexible -- what does that even mean. The only advantage is that you need to type two
    characters less.

    -marcel

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From S Jack@21:1/5 to S Jack on Sun Sep 25 11:49:44 2022
    On Sunday, September 25, 2022 at 10:07:17 AM UTC-5, S Jack wrote:
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From S Jack@21:1/5 to S Jack on Sun Sep 25 12:03:42 2022
    On Sunday, September 25, 2022 at 9:35:26 AM UTC-5, S Jack wrote:
    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

    Looking closer at it, the Brodie math was not much different from what I did. He used a constant for
    the scaling where I just plugged in a literal when I needed it. Saw a old quote "Any decent programmer
    should be able to keep the decimal place in his head." Sure he was use to using slide rule.
    --
    me

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From S Jack@21:1/5 to S Jack on Sun Sep 25 11:52:25 2022
    On Sunday, September 25, 2022 at 10:07:17 AM UTC-5, S Jack wrote:
    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

    My bad; data didn't have newline so reposting with snipped data.

    :) cat sh/encode
    #! b/myfrog INC
    -- encode Text encoder
    -- Filter to read text from stdin and
    -- output XYZ encoded text to stdout
    --.
    "(dummy)" xyz:key smv \ set key ( key edited; replaced with (dummy))
    0 xyz:key.index !
    cr cr anon char+ xyz tell ; 0 hdread
    bye
    :) cat txt/twheels.txt|sh/encode

    300A150A1010443B4F0404115517110 ... (data snipped) 161D1B1B00105217520D1A061B16081 ... (Data was stream without newlines) 6081B02110006550B1C4153260D0007 ...
    06101D0017450D1B440607164512104 ...
    B0144134F150A0D01441D1D7911151B ...
    42170D190A530703160F520E1D01421 ...
    5151417520E11090755101D4F010C0D ...
    00423C44050006090655160701530C0 ...
    90710140F171C5D4542380B06071617 ...
    16014217081D0017000655061D16006 ...
    17120C064179:)

    --
    me

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From S Jack@21:1/5 to S Jack on Sun Sep 25 13:09:34 2022
    On Sunday, September 25, 2022 at 2:19:42 PM UTC-5, S Jack wrote:
    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.

    [i] val valDpl
    -- Number of siginificant digits
    Configuration setting
    [i] DPL' ( -- n )
    -- Number of significant digits plus one non-sig digit
    [i] ROUND ( n -- n' )
    -- Round number n
    [i] DPN ( n|d -- n dpl )
    -- Convert single or double number low value (high value dropped)
    -- to single number plus its decimal place
    [i] %DPN ( n|d -- n dpl')
    -- Do DPN and adjust dpl for percentage (2 is added to dpl value)
    [i] DPADJ ( n dpl -- n' )
    -- Decimal place adjustment for n
    -- case dpl
    -- positive: add digits
    -- negative: chop digits
    -- 0 : no adjustment
    [i] *DPN ( n1 dpl1 n2 dpl2 -- n )
    -- Multiply two dpn numbers
    [i] +DPN ( n1 dpl1 n2 dpl2 -- n )
    -- Add two dpn numbers
    [i] +DPN' ( n1 dpl1 n2' -- n )
    -- Add intermediate number n2' to dpn (n1 dpl1)
    n2' is made dpn using DPL' for its dpl
    [i] DISCOUNT ( n1 dpl n2 %dpl -- n )
    -- n is amount after n2 percent of n1 is taken from n1
    [i] % ( n1 n2 -- n )
    -- Percent calculation: divide (n1*100) by n2
    -- Adjust for sig digits plus one non-sig digit
    [s] Formatting and printing
    [i] 2val DPFMT
    -- DP format c1 c2
    -- c1 is prepending character if non-zero
    -- c2 is appending character if non-zero
    [i] -DPFMT
    -- Retrun DPFMT to its default value
    Default is no pre-pending character
    and space for appended character
    [i] (DP) ( n c1 c2 -- s )
    -- Convert single number n to counted string s
    -- Number of decimal places is taken from valDPL
    -- If c1 is not zero, append to end of string.
    -- If c2 is not zero, place it after the sign.
    [i] .DP ( n -- )
    -- Round number and print formatted for valDPL decimal places
    [i] R.DP ( n fld -- )
    -- Round number and print formatted for valDPL decimal places
    -- in field width fld
    [i] .% ( n -- )
    -- Print percentage to valDPL precision
    [i] R.% ( n fld -- )
    -- Print percentage to valDPL precision in field width fld.
    [i] .$DP ( n -- )
    -- Print n to valDPL precision with pre-pended dollor sign
    -- and appended space
    [i] R.$DP ( n fld -- )
    -- Print percentage to valDPL precision in field width fld.
    [i] .R$DPL ( n dpl fld -- )
    -- Print percentage to DPL precision in field width fld.
    --
    me

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to the.beez.speaks@gmail.com on Sun Sep 25 21:19:07 2022
    In article <843c3b3a-6778-46b3-b3e2-672bea736092n@googlegroups.com>,
    Hans Bezemer <the.beez.speaks@gmail.com> wrote:
    On Saturday, September 24, 2022 at 6:58:20 AM UTC+2, S Jack wrote:
    On Tuesday, September 20, 2022 at 2:59:49 PM UTC-5, Buzz McCool wrote:

    Using integer frog math:
    go
    <SNIP>
    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 )
    "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.

    That can be true in the 80's. Nowadays both integer and fp math
    are even more ridiculously fast, and of course fp is likely faster.
    It doesn't even matter.

    The only considerations are
    1. is fp available?
    2. has fp the potential to make code simpler?

    Under ciforth you have to load fp, that may be a threshold.


    Hans Bezemer

    Groetjes Albert
    --
    "in our communism country Viet Nam, people are forced to be
    alive and in the western country like US, people are free to
    die from Covid 19 lol" duc ha
    albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From S Jack@21:1/5 to All on Sun Sep 25 12:19:41 2022
    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.
    Testing decimal numbers


    Decimal number multiplication
    10 percent of 12.25
    12.25 DPN 10 %DPN *DPN .DP ==> 1.23
    Formatted output
    12.25 DPN 10 %DPN *DPN 8 .RDP ==> 1.23
    Decimal number addition
    40,998.00 DPN 3,484.83 DPN +DPN .DP ==> 44482.83
    Discount
    12.50 DPN 32 %DPN DISCOUNT .DP ==> 8.50

    Calculating percent
    Percent 30 of 60
    30 60 % .% ==> 50.00%
    Formatted output
    30 60 % 8 .R% ==> 50.00%

    Example
    New truck cost after sale's tax

    New truck sale price: $40,998.00.
    Sales tax rate: 8.5%
    Find amount of sales tax and total cost of new truck:

    40,998.00 DPN \ new truck sale price
    cr ." Truck sticker price: " 2DUP 15 .R$DPL
    2DUP
    8.5 %DPN *DPN \ calculate tax
    cr ." Amount of sales tax: " DUP 15 .R$DP
    cr ." New truck total cost: " +DPN' 15 .R$DP

    Truck sticker price: $40998.00
    Amount of sales tax: $3484.83
    New truck total cost: $44482.83
    -fin-
    --
    me

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From S Jack@21:1/5 to S Jack on Sun Sep 25 16:38:07 2022
    On Sunday, September 25, 2022 at 3:09:35 PM UTC-5, S Jack wrote:
    :) ./ac_power

    -- Calculations for an AC Power Supply
    -- Prints current, voltage, DC power, AC power, & AC line cord current
    -- for a number of current values.

    100 const SCALE ( scale factor for two decimal places )
    -- Note: values assume two decimal place
    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 SCALE */ ;
    : acp SCALE EF */ SCALE PF */ ;
    : line_cord_current SCALE AC_MIN_V */ ;

    ( Formatting )
    ( s1 item string )
    ( s2 unit string )
    ( nd number of digits after the decimal point )
    ( np minimum number of significant digits )
    ( nr total width of the output )
    ( sc format string count )
    ( sf formatted string )

    : ndigits ( n -- ) 0 ?DO # LOOP ; \ format n digits
    : field_padding ( sc nr -- ) OVER - 0 MAX SPACES ; \ field width padding
    : dot ( -- ) asc . hold ; \ format a dot
    : n_format ( n s2 np nd -- s2 sf sc ) \ format the value
    2>r \ rack ( np nd )
    swap 0 <# r> ndigits dot r> ndigits #s #> ;
    : .rdp ( n s2 np nd nr s1 -- )
    tell \ print s1 (item)
    >r \ rack nr
    n_format \ format n
    r> field_padding \ field width padding
    type \ print formatted n
    tell \ print s2 (units)
    space
    ;

    : print_ac_power
    cr
    0000 ( Initialize current to 00.00 )
    BEGIN
    dup DC_MAX_I <= WHILE
    dup dup dup "A" 2 2 7 "I=" .rdp
    voltage_droop v dup "V" 2 2 6 "V=" .rdp
    dcp dup "W" 2 2 8 "DC P=" .rdp ( DC Power )
    acp dup "VA" 2 2 8 "AC P=" .rdp ( AC Power )
    line_cord_current "A" 2 2 6 "AC Cord I=" .rdp cr
    100 + \ increment current by 1.00A
    REPEAT DROP
    ;

    print_ac_power
    I= 000.00A V=053.75V DC P= 000.00W AC P= 000.00VA AC Cord I=000.00A
    I= 001.00A V=053.71V DC P= 053.71W AC P= 064.58VA AC Cord I=000.32A
    I= 002.00A V=053.66V DC P= 107.32W AC P= 129.05VA AC Cord I=000.64A
    ...
    I= 102.00A V=049.07V DC P= 5005.14W AC P= 6018.68VA AC Cord I=030.09A
    I= 103.00A V=049.02V DC P= 5049.06W AC P= 6071.49VA AC Cord I=030.35A
    I= 104.00A V=048.97V DC P= 5092.88W AC P= 6124.19VA AC Cord I=030.62A

    bye:)
    This my last. Thought about reworking it with my decimal code but that would have been way
    too much work. I just replace literal scale values with a constant and called it good. Only
    rework needed was to factor the .RDP .

    Interesting to compare the last line here, done with integer math to the original op's line done
    in floating point.
    --
    me

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxforth@21:1/5 to Marcel Hendrix on Mon Sep 26 12:50:53 2022
    On 26/09/2022 1:56 am, Marcel Hendrix wrote:

    On current Intel/AMD CPUs, integer multiplication and division is done
    on the FPU,

    That seems convoluted.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From S Jack@21:1/5 to S Jack on Mon Sep 26 01:43:37 2022
    On Sunday, September 25, 2022 at 6:38:08 PM UTC-5, S Jack wrote:

    This my last.

    Knew I would eat those words. Needed proper scaling and rounding:
    :) ./job

    -- Calculations for an AC Power Supply
    -- Prints current, voltage, DC power, AC power, & AC line cord current
    -- for a number of current values.

    10000 const SCALE
    0009900 const PF ( power factor )
    0008400 const EF ( efficiency )
    0537500 const DC_MAX_V ( max DC voltage )
    2000000 const AC_MIN_V ( min AC voltage )
    1040000 const DC_MAX_I ( max DC current )

    : voltage_droop -40000 870000 */ ; ( From AC Power Supply specs )
    : v DC_MAX_V + ;
    : dcp SCALE */ ;
    : acp SCALE EF */ SCALE PF */ ;
    : line_cord_current SCALE AC_MIN_V */ ;

    ( Formatting )
    ( nr total width of the output )
    : field_padding ( sc nr -- ) OVER - 0 MAX SPACES ; \ field width padding
    : dot ( -- ) asc . hold ; \ format a dot
    : n_format ( n -- a u ) \ format the value
    50 + 100 / \ rounding
    0 <# # # dot #s #> ;
    : .rdp ( n nr -- )
    >r \ rack nr
    n_format \ format n
    r> field_padding \ field width padding
    type \ print formatted n
    ;

    : print_ac_power
    cr
    000000 ( Initialize current to 00.00 )
    BEGIN
    dup DC_MAX_I <= WHILE
    dup dup dup ." I=" 7 .rdp ." A "
    voltage_droop v dup ." V=" 6 .rdp ." V "
    dcp dup ." DC P=" 8 .rdp ." W " ( DC Power )
    acp dup ." AC P=" 8 .rdp ." VA " ( AC Power )
    line_cord_current ." AC Cord I=" 6 .rdp ." A " cr
    10000 + \ increment current by 1.00A
    REPEAT DROP
    ;

    print_ac_power
    I= 0.00A V= 53.75V DC P= 0.00W AC P= 0.00VA AC Cord I= 0.00A
    I= 1.00A V= 53.70V DC P= 53.70W AC P= 64.58VA AC Cord I= 0.32A
    I= 2.00A V= 53.66V DC P= 107.32W AC P= 129.05VA AC Cord I= 0.65A
    I= 3.00A V= 53.61V DC P= 160.84W AC P= 193.41VA AC Cord I= 0.97A
    ...
    I= 103.00A V= 49.01V DC P= 5048.48W AC P= 6070.81VA AC Cord I= 30.35A
    I= 104.00A V= 48.97V DC P= 5092.71W AC P= 6123.99VA AC Cord I= 30.62A

    bye:)

    op's last value:
    I=104.00A V=48.97V DC P=5092.71W AC P=6123.99VA AC Cord I=30.62A
    :)
    --
    me

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From S Jack@21:1/5 to S Jack on Mon Sep 26 05:05:04 2022
    On Monday, September 26, 2022 at 3:43:39 AM UTC-5, S Jack wrote:

    Reworked it so parameters could be given in un-scaled form.

    :) ./job

    -- Calculations for an AC Power Supply
    -- Prints current, voltage, DC power, AC power, & AC line cord current
    -- for a number of current values.

    10000 const SF \ scale factor
    : scale ( d -- n ) drop 100 * ; \ scale double with 2 place decimal
    : *. ( n n -- n ) SF */ ;
    : /. ( n n -- n ) SF swap */ ;

    0.99 scale const PF ( power factor )
    0.84 scale const EF ( efficiency )
    053.75 scale const DC_MAX_V ( max DC voltage )
    200.00 scale const AC_MIN_V ( min AC voltage )
    104.00 scale const DC_MAX_I ( max DC current )

    : VOLTAGE_DROOP ( current -- volts )
    [ -4.00 scale ] literal
    [ 87.00 scale ] literal */ ;
    : V ( volts -- 'volts ) DC_MAX_V + ;
    : DCP ( current voltage -- dcpower ) *. ;
    : ACP ( dcpower -- acpower ) EF /. PF /. ;
    : LINE_CORD_CURRENT ( acpower -- cordcurrent ) AC_MIN_V /. ;

    ( Formatting )
    ( nr total width of the output )
    : field_padding ( sc nr -- ) OVER - 0 MAX SPACES ; \ field width padding
    : dot ( -- ) asc . hold ; \ format a dot
    : round ( n -- n' ) 50 + 100 / ; \ round number
    : n_format ( n -- a u ) \ format value
    round 0 <# # # dot #s #> ;
    : .rdp ( n nr -- )
    >r \ rack nr
    n_format \ format n
    r> field_padding \ field width padding
    type \ print formatted n
    ;

    : print_ac_power
    cr
    0 ( Initialize current to 0 )
    BEGIN
    dup DC_MAX_I <= WHILE
    dup dup dup ." I=" 7 .rdp ." A "
    VOLTAGE_DROOP V dup ." V=" 6 .rdp ." V "
    DCP dup ." DC P=" 8 .rdp ." W " ( DC Power )
    ACP dup ." AC P=" 8 .rdp ." VA " ( AC Power )
    LINE_CORD_CURRENT ." AC Cord I=" 6 .rdp ." A " cr
    [ 1.00 scale ] literal + \ increment current by 1.00A
    REPEAT DROP
    ;

    print_ac_power
    I= 0.00A V= 53.75V DC P= 0.00W AC P= 0.00VA AC Cord I= 0.00A
    I= 1.00A V= 53.70V DC P= 53.70W AC P= 64.58VA AC Cord I= 0.32A
    I= 2.00A V= 53.66V DC P= 107.32W AC P= 129.05VA AC Cord I= 0.65A
    I= 3.00A V= 53.61V DC P= 160.84W AC P= 193.41VA AC Cord I= 0.97A
    ...
    I= 102.00A V= 49.06V DC P= 5004.16W AC P= 6017.51VA AC Cord I= 30.09A
    I= 103.00A V= 49.01V DC P= 5048.48W AC P= 6070.81VA AC Cord I= 30.35A
    I= 104.00A V= 48.97V DC P= 5092.71W AC P= 6123.99VA AC Cord I= 30.62A

    bye:)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Buzz McCool@21:1/5 to Buzz McCool on Mon Sep 26 09:49:57 2022
    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


    $ ./forth_droop.fth
    I= 0.00A V=53.75V DCP= 0.00W ACP= 0.00VA AC Cord I= 0.00A
    I= 1.00A V=53.70V DCP= 53.70W ACP= 64.58VA AC Cord I= 0.32A
    I= 2.00A V=53.66V DCP= 107.32W ACP= 129.05VA AC Cord I= 0.65A
    .
    .
    .
    I=102.00A V=49.06V DCP=5004.16W ACP=6017.50VA AC Cord I=30.09A
    I=103.00A V=49.01V DCP=5048.48W ACP=6070.80VA AC Cord I=30.35A
    I=104.00A V=48.97V DCP=5092.71W ACP=6123.99VA AC Cord I=30.62A

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxforth@21:1/5 to Buzz McCool on Tue Sep 27 03:22:58 2022
    On 27/09/2022 2:49 am, Buzz McCool wrote:
    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

    fdrop should be placed after repeat i.e.

    repeat fdrop ;

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From S Jack@21:1/5 to S Jack on Tue Sep 27 07:11:05 2022
    On Monday, September 26, 2022 at 7:05:05 AM UTC-5, S Jack wrote:
    On Monday, September 26, 2022 at 3:43:39 AM UTC-5, S Jack wrote:

    Reworked it so parameters could be given in un-scaled form.
    For Hans, using the "Brodie" constant:
    :) ./foo

    -- Calculations for an AC Power Supply
    -- Prints current, voltage, DC power, AC power, & AC line cord current
    -- for a number of current values.
    16384 const 0x4000 \ for clarifacation

    0x4000 const SF \ scale factor
    : *. ( n n -- n ) SF */ ;
    : /. ( n n -- n ) SF swap */ ;
    : scale ( d -- n ) drop 100 /. ;
    : => scale const ; \ assign scaled constants

    0.99 => PF ( power factor )
    0.84 => EF ( efficiency )
    053.75 => DC_MAX_V ( max DC voltage )
    200.00 => AC_MIN_V ( min AC voltage )
    104.00 => DC_MAX_I ( max DC current )

    -- AC power supply specs:
    -4.00 => VD_K1 ( voltage droop factor 1 )
    87.00 => VD_K2 ( voltage dropp factor 2 )

    : VOLTAGE_DROOP ( current -- volts ) VD_K1 VD_K2 */ ;
    : V ( volts -- 'volts ) DC_MAX_V + ;
    : DCP ( voltage current -- dcpower ) *. ;
    : ACP ( dcpower -- acpower ) EF /. PF /. ;
    : LINE_CORD_CURRENT ( acpower -- cordcurrent ) AC_MIN_V /. ;

    ( Formatting )
    ( nr total width of the output )
    : padding ( sc nr -- ) OVER - 0 MAX SPACES ; \ field width padding
    : dot ( -- ) asc . hold ; \ format a dot
    : round ( n -- n' ) 5 + 100 / ; \ round number
    : format ( n -- a u ) \ format value
    10,000 *.
    round 0 <# # # dot #s #> ;
    : .rdp ( n nr -- )
    >r format
    r> padding \ print field padding if needed
    type \ print formatted value
    ;

    1.00 => 1amp
    : +1amp ( current -- current' ) 1amp + ; \ increment current by 1 amp

    : ac_power
    cr
    0 ( Initialize current to 0 )
    BEGIN
    dup DC_MAX_I <= WHILE >r
    r ." I=" 7 .RDP ." A "
    r VOLTAGE_DROOP V dup ." V=" 6 .RDP ." V "
    r DCP dup ." DC P=" 8 .RDP ." W " ( DC Power )
    ACP dup ." AC P=" 8 .RDP ." VA " ( AC Power )
    LINE_CORD_CURRENT ." AC Cord I=" 6 .RDP ." A " cr
    r> +1amp \ increment current
    REPEAT drop
    ;

    ac_power
    I= 0.00A V= 53.75V DC P= 0.00W AC P= 0.00VA AC Cord I= 0.00A
    I= 1.00A V= 53.70V DC P= 53.70W AC P= 64.58VA AC Cord I= 0.32A
    I= 2.00A V= 53.65V DC P= 107.31W AC P= 129.05VA AC Cord I= 0.64A
    I= 3.00A V= 53.61V DC P= 160.83W AC P= 193.41VA AC Cord I= 0.96A
    ...
    I= 102.00A V= 49.06V DC P= 5004.15W AC P= 6017.80VA AC Cord I= 30.08A
    I= 103.00A V= 49.01V DC P= 5048.48W AC P= 6071.11VA AC Cord I= 30.35A
    I= 104.00A V= 48.96V DC P= 5092.71W AC P= 6124.30VA AC Cord I= 30.62A

    -fin-
    --
    me

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Buzz McCool@21:1/5 to dxforth on Tue Sep 27 14:14:55 2022
    On 9/26/2022 10:22 AM, dxforth wrote:

    fdrop should be placed after repeat i.e.

    repeat fdrop ;


    Yes, that makes the calculations_for_ac_power_supply word self
    contained. That word now reads per the below. Thanks again!


    : 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
    fdrop ; \ remove leftover current loop count value

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)