šŸš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.forth
4 messages
4 total messages Started by dxf Sun, 18 Oct 2026 16:09
ROUND function
#126
Author: dxf
Date: Sun, 18 Oct 2026 16:09
34 lines
1513 bytes
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 
 
 
Re: ROUND function
#128
Author: marcel hendrix
Date: Sun, 18 Oct 2026 16:47
15 lines
1649 bytes
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
Re: ROUND function
#133
Author: dxf
Date: Mon, 19 Oct 2026 01:29
21 lines
1379 bytes
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.Ā  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

https://pastebin.com/skQArj3E

(If F^N looks familiar, I adapted it from JVN's dd^n )
Re: ROUND function
#148
Author: dxf
Date: Tue, 20 Oct 2026 13:50
29 lines
1688 bytes
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.Ā  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
> 
> 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.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads