• ROUND function

    From dxf@dxforth@gmail.com to comp.lang.forth on Mon Oct 19 03:09:07 2026
    From Newsgroup: comp.lang.forth

    It appears ROUND() is a fairly common function. I created it using
    a factor from REPRESENT. Are there other ways implementing it?


    \ SCALE ( r1 n -- r2 ) scale r1 left/right by n digits

    : ROUND ( r1 n -- r2 )
    >r fbad? if 2drop rdrop end
    r@ scale fround r> negate scale ;

    :noname cr begin refill while source 2dup type space
    evaluate cr repeat ; execute

    876.543e 3 round f. 876.543
    876.543e 2 round f. 876.54
    876.543e 1 round f. 876.5
    876.543e 0 round f. 877.
    876.543e -1 round f. 880.
    876.543e -2 round f. 900.
    876.543e -3 round f. 1000.

    456.789e 3 round f. 456.789
    456.789e 2 round f. 456.79
    456.789e 1 round f. 456.8
    456.789e 0 round f. 457.
    456.789e -1 round f. 460.
    456.789e -2 round f. 500.
    456.789e -3 round f. 0.

    +nan 3 round f. +NAN
    -inf -3 round f. -INF



    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From marcel hendrix@mhx@iae.nl to comp.lang.forth on Sun Oct 18 18:47:46 2026
    From Newsgroup: comp.lang.forth

    On 10/18/2026 6:09 PM, dxf wrote:

    It appears ROUND() is a fairly common function. I created it using
    a factor from REPRESENT. Are there other ways implementing it?


    \ SCALE ( r1 n -- r2 ) scale r1 left/right by n digits

    : ROUND ( r1 n -- r2 )
    >r fbad? if 2drop rdrop end
    r@ scale fround r> negate scale ;
    [..]

    Interesting. Please show your current SCALE and fbad? words.
    -marcel
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From dxf@dxforth@gmail.com to comp.lang.forth on Mon Oct 19 12:29:27 2026
    From Newsgroup: comp.lang.forth

    On 19/10/2026 3:47 am, marcel hendrix wrote:
    On 10/18/2026 6:09 PM, dxf wrote:

    It appears ROUND() is a fairly common function.-a I created it using
    a factor from REPRESENT.-a Are there other ways implementing it?


    \ SCALE ( r1 n -- r2 )-a scale r1 left/right by n digits

    : ROUND ( r1 n -- r2 )
    -a-a >r-a fbad? if-a 2drop-a rdrop-a end
    -a-a r@ scale-a fround-a r> negate scale ;
    [..]

    Interesting. Please show your current SCALE and fbad? words.
    -marcel

    https://pastebin.com/skQArj3E

    (If F^N looks familiar, I adapted it from JVN's dd^n )

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From dxf@dxforth@gmail.com to comp.lang.forth on Wed Oct 21 00:50:37 2026
    From Newsgroup: comp.lang.forth

    On 19/10/2026 12:29 pm, dxf wrote:
    On 19/10/2026 3:47 am, marcel hendrix wrote:
    On 10/18/2026 6:09 PM, dxf wrote:

    It appears ROUND() is a fairly common function.-a I created it using
    a factor from REPRESENT.-a Are there other ways implementing it?


    \ SCALE ( r1 n -- r2 )-a scale r1 left/right by n digits

    : ROUND ( r1 n -- r2 )
    -a-a >r-a fbad? if-a 2drop-a rdrop-a end
    -a-a r@ scale-a fround-a r> negate scale ;
    [..]

    Interesting. Please show your current SCALE and fbad? words.
    -marcel

    https://pastebin.com/skQArj3E

    (If F^N looks familiar, I adapted it from JVN's dd^n )

    BTW for this use SCALE can be simplified to:

    : SCALE ( r1 n -- r2 ) >r f10. r> f^n f* ;

    as only numbers with powers <= precision would be involved.
    In REPRESENT SCALE had to deal multipliers exceeding 10^308.

    --- Synchronet 3.22a-Linux NewsLink 1.2