Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #56336 > unrolled thread

What words should be included in a minimal number theory package?

Started bylehs <skydda.barnen@gmail.com>
First post2017-03-26 03:13 -0700
Last post2017-03-28 10:55 -0700
Articles 11 on this page of 31 — 10 participants

Back to article view | Back to comp.lang.forth


Contents

  What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-03-26 03:13 -0700
    Re: What words should be included in a minimal number theory package? krishna.myneni@ccreweb.org - 2017-03-26 06:35 -0700
      Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-03-26 10:14 -0700
        Re: What words should be included in a minimal number theory package? NN <november.nihal@gmail.com> - 2017-03-26 12:50 -0700
          Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-03-26 17:03 -0700
            Re: What words should be included in a minimal number theory package? "Elizabeth D. Rather" <erather@forth.com> - 2017-03-26 14:16 -1000
              Re: What words should be included in a minimal number theory package? hughaguilar96@gmail.com - 2017-03-26 18:00 -0700
              Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-03-26 18:13 -0700
                Re: What words should be included in a minimal number theory package? "Elizabeth D. Rather" <erather@forth.com> - 2017-03-26 16:18 -1000
                  Re: What words should be included in a minimal number theory package? Paul Rubin <no.email@nospam.invalid> - 2017-03-26 22:14 -0700
                    Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-03-26 23:43 -0700
                      Re: What words should be included in a minimal number theory package? "Elizabeth D. Rather" <erather@forth.com> - 2017-03-26 21:38 -1000
                      Re: What words should be included in a minimal number theory package? minforth@arcor.de - 2017-03-27 03:30 -0700
                        Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-03-27 04:16 -0700
                  Re: What words should be included in a minimal number theory package? albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-03-28 01:54 +0200
                Re: What words should be included in a minimal number theory package? krishna.myneni@ccreweb.org - 2017-03-27 12:04 -0700
                  Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-03-27 12:56 -0700
                    Re: What words should be included in a minimal number theory package? krishna.myneni@ccreweb.org - 2017-04-01 06:38 -0700
                      Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-04-01 07:41 -0700
                        Re: What words should be included in a minimal number theory package? krishna.myneni@ccreweb.org - 2017-04-17 16:08 -0700
                          Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-04-17 16:34 -0700
                            Re: What words should be included in a minimal number theory package? Julian Fondren <julian.fondren@gmail.com> - 2017-04-17 20:50 -0700
                              Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-04-18 00:20 -0700
                            Re: What words should be included in a minimal number theory package? krishna.myneni@ccreweb.org - 2017-04-18 04:29 -0700
                          Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-04-18 01:31 -0700
                Re: What words should be included in a minimal number theory package? mhx@iae.nl - 2017-04-18 01:33 -0700
                  Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-04-18 01:44 -0700
    Re: What words should be included in a minimal number theory package? albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-03-28 01:45 +0200
      Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-03-27 23:33 -0700
        Re: What words should be included in a minimal number theory package? hughaguilar96@gmail.com - 2017-03-28 10:26 -0700
          Re: What words should be included in a minimal number theory package? lehs <skydda.barnen@gmail.com> - 2017-03-28 10:55 -0700

Page 2 of 2 — ← Prev page 1 [2]


#57511

Fromlehs <skydda.barnen@gmail.com>
Date2017-04-17 16:34 -0700
Message-ID<2a1f9b3e-943c-4781-96a2-4566ab2f8e7c@googlegroups.com>
In reply to#57505
Den tisdag 18 april 2017 kl. 01:08:27 UTC+2 skrev krishna...@ccreweb.org:
> On Saturday, April 1, 2017 at 9:41:51 AM UTC-5, lehs wrote:
> ...
> > 
> > Thank YOU! I've changed the address: https://github.com/Lehs/ANS-Forth-libraries
> 
> I've tried it now. Although I don't know what all of the words do, here are a few comments:
> 
> 1) The word NEXTPRIME returns the same number if the argument is prime, e.g.
> 
> 19 nextprime .
> 19  ok
> 
> 2) Consider using the common usage word(s), [DEFINED] and/or [UNDEFINED] , instead of devising your own.
> 
> 3) Maybe an implementation of a prime counting function would be useful to include, for small upper bound, e.g.
> 
> : pcf ( u -- ucount )
>   0 swap 0 do i isprime if 1+ then loop ;
> 
> That's quick and dirty, and not efficient at all once "u" is on the order of 1 million.
> 
> 4) Some test code, perhaps in a separate file, would help the user understand if the words are working as intended.
> 
> Thanks very much for this!
> 
> : show-primes ( u -- )
>    0 do i isprime if i 6 .r then i 1+ 10 mod 0= if cr then loop ;
> 
> 100 show-primes
>      2     3     5     7
>     11    13    17    19
>     23    29
>     31    37
>     41    43    47
>     53    59
>     61    67
>     71    73    79
>     83    89
>     97
>  ok
> 
> Krishna

I decided that nextprime ( n -- p ) should give n if n was prime. I don't know if it's unconventional?

If I use [defined] etc I still have to include the code since those are not ANS standard.

There is a fast prime counting function PI in primecounting.4th which use squeezed tables described in http://forthmath.blogspot.se/2015/12/prime-tables-and-prime-counting-function.html and even a function PNR@ ( n -- Pn ) that gives the nth prime.

I shall include examples.

Lars-Erik Svahn
https://forthmath.blogspot.se
https://github.com/Lehs/ANS-Forth-libraries

[toc] | [prev] | [next] | [standalone]


#57531

FromJulian Fondren <julian.fondren@gmail.com>
Date2017-04-17 20:50 -0700
Message-ID<476ace07-dec0-4cff-9584-17dd091d9f84@googlegroups.com>
In reply to#57511
On Monday, April 17, 2017 at 6:34:51 PM UTC-5, lehs wrote:
> If I use [defined] etc I still have to include the code since those are not ANS standard.

It's not Forth-94, but it's Forth-2012: http://forth-standard.org/standard/tools/

[toc] | [prev] | [next] | [standalone]


#57534

Fromlehs <skydda.barnen@gmail.com>
Date2017-04-18 00:20 -0700
Message-ID<b8d2e659-cd32-4951-9abb-ebe28974e5a9@googlegroups.com>
In reply to#57531
Thanks! This seems to be the first place to look after names of non ANS words. Nice that even the code is given as definition since formal human language isn't quit as unambiguos as code.

[toc] | [prev] | [next] | [standalone]


#57542

Fromkrishna.myneni@ccreweb.org
Date2017-04-18 04:29 -0700
Message-ID<48ce2d70-e4fb-4928-a97a-23b492a58dcf@googlegroups.com>
In reply to#57511
On Monday, April 17, 2017 at 6:34:51 PM UTC-5, lehs wrote:
> Den tisdag 18 april 2017 kl. 01:08:27 UTC+2 skrev 
> ...
> I decided that nextprime ( n -- p ) should give n if n was prime. I don't know if it's unconventional?
> 

Ok. It was unexpected but the behavior is fine if it was by design -- it's possibly more useful the way you have define it.


> ... There is a fast prime counting function PI in primecounting.4th which use squeezed tables described in http://forthmath.blogspot.se/2015/12/prime-tables-and-prime-counting-function.html and even a function PNR@ ( n -- Pn ) that gives the nth prime.

Thanks for the link.

Krishna

[toc] | [prev] | [next] | [standalone]


#57536

Fromlehs <skydda.barnen@gmail.com>
Date2017-04-18 01:31 -0700
Message-ID<4e5c7390-ab5b-4f58-b26e-3fc00b8107e9@googlegroups.com>
In reply to#57505
Den tisdag 18 april 2017 kl. 01:08:27 UTC+2 skrev krishna...@ccreweb.org:
> On Saturday, April 1, 2017 at 9:41:51 AM UTC-5, lehs wrote:
> ...
> > 
> > Thank YOU! I've changed the address: https://github.com/Lehs/ANS-Forth-libraries
> 
> I've tried it now. Although I don't know what all of the words do, here are a few comments:
> 
> 1) The word NEXTPRIME returns the same number if the argument is prime, e.g.
> 
> 19 nextprime .
> 19  ok
> 
> 2) Consider using the common usage word(s), [DEFINED] and/or [UNDEFINED] , instead of devising your own.
> 
> 3) Maybe an implementation of a prime counting function would be useful to include, for small upper bound, e.g.
> 
> : pcf ( u -- ucount )
>   0 swap 0 do i isprime if 1+ then loop ;
> 
> That's quick and dirty, and not efficient at all once "u" is on the order of 1 million.
> 
> 4) Some test code, perhaps in a separate file, would help the user understand if the words are working as intended.
> 
> Thanks very much for this!
> 
> : show-primes ( u -- )
>    0 do i isprime if i 6 .r then i 1+ 10 mod 0= if cr then loop ;
> 
> 100 show-primes
>      2     3     5     7
>     11    13    17    19
>     23    29
>     31    37
>     41    43    47
>     53    59
>     61    67
>     71    73    79
>     83    89
>     97
>  ok
> 
> Krishna

Okay, I've looked around and it seems to be a convention that 

nextprime ( n -- p )

should give the next prime greater than n.
Lars-Erik Svahn
https://forthmath.blogspot.se
https://github.com/Lehs/ANS-Forth-libraries

[toc] | [prev] | [next] | [standalone]


#57537

Frommhx@iae.nl
Date2017-04-18 01:33 -0700
Message-ID<1e8ccb14-6ed6-405c-932d-6852b5fcd074@googlegroups.com>
In reply to#56364
On Monday, March 27, 2017 at 3:13:17 AM UTC+2, lehs wrote:
> Forth has all what is needed except libraries for functions like:
> 
> : isprime \ u -- flag
>   dup 5 u< if 2 4 within exit then
>   dup 1 and 0= if 0= exit then
>   true over sqrtf 3 umax 1+ 3
>   do over i umod 0= 
>      if 0= leave then 2
>   +loop nip ;
> 
> : ugcd \ u1 u2 -- u      \ Greatest common divisor
>   2dup u< if swap then   \ smallest of a b on top of stack (tos)
>   ?dup 0= if exit then   \ return second value if tos is zero
>   begin tuck             \ y x y first time b a b
>      0 swap um/mod       \ y x 0 y --> y r q
>      drop dup 0=         \ y r [r=0]
>   until drop ;           \ y
> 
> : u*mod \ u1 u2 u3 -- u1*u2(mod u3)
>   >r r@ umod swap r@ umod um*
>   r> um/mod drop ;
> 
> : u**mod \ b a m -- x
>   locals| m a b | 
>   1                      \ preparation for repeated multiplication
>   a log~ 0               \ n 0 n is the number of binary digits in a
>   ?do a 1 i lshift and   \ flag the i-th digit of a is 0/1
>      if b m u*mod        \ multiply if flag
>      then b dup m        \ square b for each i: b b^2 b^4 b^8 ...
>      u*mod to b          \ new squared number to b
>   loop ;                 \ result of the repeated multiplication
> 
> With such libraries Forth would be perfect for integer math. But as
> it is every user of standard Forth has to reinvent all those words
> that is necessary building stones for all integer math that might
> interest mathematicians.

That is a sweeping statement. Good Forths have all these simple
words and many more, some (i.e. iForth) even with arbitrary
precision. And there *are* (Forth) libraries and publications
of this stuff.

So, in the future, please write:

"But as it is, I THINK THAT every user of standard Forth has to
reinvent all those words that is necessary building stones for
all integer math that might interest mathematicians.
I MIGHT BE WRONG."

-marcel

[toc] | [prev] | [next] | [standalone]


#57538

Fromlehs <skydda.barnen@gmail.com>
Date2017-04-18 01:44 -0700
Message-ID<cecb557f-b4b0-4777-985d-bc1c690bc6d4@googlegroups.com>
In reply to#57537
Den tisdag 18 april 2017 kl. 10:33:03 UTC+2 skrev m...@iae.nl:
> On Monday, March 27, 2017 at 3:13:17 AM UTC+2, lehs wrote:
> > Forth has all what is needed except libraries for functions like:
> > 
> > : isprime \ u -- flag
> >   dup 5 u< if 2 4 within exit then
> >   dup 1 and 0= if 0= exit then
> >   true over sqrtf 3 umax 1+ 3
> >   do over i umod 0= 
> >      if 0= leave then 2
> >   +loop nip ;
> > 
> > : ugcd \ u1 u2 -- u      \ Greatest common divisor
> >   2dup u< if swap then   \ smallest of a b on top of stack (tos)
> >   ?dup 0= if exit then   \ return second value if tos is zero
> >   begin tuck             \ y x y first time b a b
> >      0 swap um/mod       \ y x 0 y --> y r q
> >      drop dup 0=         \ y r [r=0]
> >   until drop ;           \ y
> > 
> > : u*mod \ u1 u2 u3 -- u1*u2(mod u3)
> >   >r r@ umod swap r@ umod um*
> >   r> um/mod drop ;
> > 
> > : u**mod \ b a m -- x
> >   locals| m a b | 
> >   1                      \ preparation for repeated multiplication
> >   a log~ 0               \ n 0 n is the number of binary digits in a
> >   ?do a 1 i lshift and   \ flag the i-th digit of a is 0/1
> >      if b m u*mod        \ multiply if flag
> >      then b dup m        \ square b for each i: b b^2 b^4 b^8 ...
> >      u*mod to b          \ new squared number to b
> >   loop ;                 \ result of the repeated multiplication
> > 
> > With such libraries Forth would be perfect for integer math. But as
> > it is every user of standard Forth has to reinvent all those words
> > that is necessary building stones for all integer math that might
> > interest mathematicians.
> 
> That is a sweeping statement. Good Forths have all these simple
> words and many more, some (i.e. iForth) even with arbitrary
> precision. And there *are* (Forth) libraries and publications
> of this stuff.
> 
> So, in the future, please write:
> 
> "But as it is, I THINK THAT every user of standard Forth has to
> reinvent all those words that is necessary building stones for
> all integer math that might interest mathematicians.
> I MIGHT BE WRONG."
> 
> -marcel

Yes, as it seems there are fantastic libraries hidden on the Internet which some of them seems to be difficult to use.

After all, 'easy to use' is an advantages among with 'extremely efficient'.

It's a dull thing to do, looking around for libraries that, if you found them, are hard to use.

[toc] | [prev] | [next] | [standalone]


#56406

Fromalbert@cherry.spenarnc.xs4all.nl (Albert van der Horst)
Date2017-03-28 01:45 +0200
Message-ID<obc85t$kcf$1@cherry.spenarnc.xs4all.nl>
In reply to#56336
In article <162e9326-e7e5-4f7b-a253-70b0c4ada914@googlegroups.com>,
lehs  <skydda.barnen@gmail.com> wrote:
>While developing my BigZ (https://forthmath.blogspot.se/ https://github.com=
>/Lehs/BigZ) I often create integer sequences which I try to find on https:/=
>/oeis.org/. Now and then I propose a new sequence and have to design a prog=
>ram that generate the sequence.=20
>
>However, I always use Forth which probably is very unknown by OEIS users an=
>d lacks the most math words that has to be used, as SQRTF ( u -- floor ) an=
>d PRIME ( u -- flag ).
>
>(ANS-Forth)
>\ prime \ n -- flag
>: Gauss_twins \ n -- a(n)
>  dup * locals| square | 0
>  begin 1+ dup dup * square + prime
>     over 2 + dup * square + prime and
>  until ;
>
>So far I've solved the problem by state the stack diagram for words not inc=
>luded in the ANS standard, but it would be nice to refer to a minimal stand=
>ard package and what about:
>
>sqrtf \ u -- floor=20
>umod \ u q -- u(mod q)
>u*mod \ u v q -- uv(mod q)
>u**mod \ u v q -- u**v(mod q)
>ugcd \ u v -- w
>prime \ u -- flag
>nextprime \ u -- p

Get rid of the u. They are numbers.
When was the last time that you had a number between
18446744073709551615 and half of that?

I have
FACTOR  \ For   n   and  p  return smallest  factor  larger than p.
PRIMES? \ For  p  return  flag  whether it is prime
GCD    \ Your GCD
SQRT    \ Your sqrtf
XGCD   \ Extended gcd, for A B return C and GCD such that A*C + B*X is GCD
CHS    \ m n -- m "over" n (choose function)
PYR    \ pyramidal function
TRI   \ triangular function
MU    \ moebius function
PHI   \ totient function
FAC   \ faculty (!)

Also a modulo package ( all arithmetic operators modulo a fixed
number, a global that you can fill in.)
Code can be stolen form the lab file of one of the ciforth's.

Groetjes Albert
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

[toc] | [prev] | [next] | [standalone]


#56415

Fromlehs <skydda.barnen@gmail.com>
Date2017-03-27 23:33 -0700
Message-ID<7b76c295-7a19-4e51-b08f-b8fb4c852874@googlegroups.com>
In reply to#56406
Den tisdag 28 mars 2017 kl. 01:44:40 UTC+2 skrev Albert van der Horst:
> In article <162e9326-e7e5-4f7b-a253-70b0c4ada914@googlegroups.com>,
> lehs  <skydda.barnen@gmail.com> wrote:
> >While developing my BigZ (https://forthmath.blogspot.se/ https://github.com=
> >/Lehs/BigZ) I often create integer sequences which I try to find on https:/=
> >/oeis.org/. Now and then I propose a new sequence and have to design a prog=
> >ram that generate the sequence.=20
> >
> >However, I always use Forth which probably is very unknown by OEIS users an=
> >d lacks the most math words that has to be used, as SQRTF ( u -- floor ) an=
> >d PRIME ( u -- flag ).
> >
> >(ANS-Forth)
> >\ prime \ n -- flag
> >: Gauss_twins \ n -- a(n)
> >  dup * locals| square | 0
> >  begin 1+ dup dup * square + prime
> >     over 2 + dup * square + prime and
> >  until ;
> >
> >So far I've solved the problem by state the stack diagram for words not inc=
> >luded in the ANS standard, but it would be nice to refer to a minimal stand=
> >ard package and what about:
> >
> >sqrtf \ u -- floor=20
> >umod \ u q -- u(mod q)
> >u*mod \ u v q -- uv(mod q)
> >u**mod \ u v q -- u**v(mod q)
> >ugcd \ u v -- w
> >prime \ u -- flag
> >nextprime \ u -- p
> 
> Get rid of the u. They are numbers.
> When was the last time that you had a number between
> 18446744073709551615 and half of that?
> 
> I have
> FACTOR  \ For   n   and  p  return smallest  factor  larger than p.
> PRIMES? \ For  p  return  flag  whether it is prime
> GCD    \ Your GCD
> SQRT    \ Your sqrtf
> XGCD   \ Extended gcd, for A B return C and GCD such that A*C + B*X is GCD
> CHS    \ m n -- m "over" n (choose function)
> PYR    \ pyramidal function
> TRI   \ triangular function
> MU    \ moebius function
> PHI   \ totient function
> FAC   \ faculty (!)
> 
> Also a modulo package ( all arithmetic operators modulo a fixed
> number, a global that you can fill in.)
> Code can be stolen form the lab file of one of the ciforth's.
> 
> Groetjes Albert
> -- 
> Albert van der Horst, UTRECHT,THE NETHERLANDS
> Economic growth -- being exponential -- ultimately falters.
> albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

No, I focus on unsigned integers.

\ Unsigned integer number theory basicsinglext.4th 
\
\ isprime \ u -- flag
\ nextprime \ u -- p
\ primefactors \ u -- q1 q2 ... qn n
\ radical \ n -- r
\ totients \ n -- t
\ mobius \ n -- m
\ bigomega \ n -- b 
\ smallomega \ n -- s
\ legendre \ a p -- i
\ tau \ u -- n    The number of divisors of u
\ sigma \ u -- s  The sum of all divisors of u
\ choose \ n k -- nCk
\ sqrtf \ u -- floor
\ log~ \ n -- 1+log n
\ umin \ u1 u2 -- u
\ umax \ u1 u2 -- u
\ umod \ u q -- u(mod q)
\ ugcd \ u1 u2 -- u
\ u*mod \ u1 u2 u3 -- u1*u2(mod u3)
\ u**mod \ b a m -- b^a(mod m)

https://github.com/Lehs/ANS-Forth-libraries

[toc] | [prev] | [next] | [standalone]


#56424

Fromhughaguilar96@gmail.com
Date2017-03-28 10:26 -0700
Message-ID<9fea1b31-de68-45fa-9689-1bf2ed77cb32@googlegroups.com>
In reply to#56415
On Monday, March 27, 2017 at 11:33:50 PM UTC-7, lehs wrote:
> Den tisdag 28 mars 2017 kl. 01:44:40 UTC+2 skrev Albert van der Horst:
> > Get rid of the u. They are numbers.
> > When was the last time that you had a number between
> > 18446744073709551615 and half of that?
> 
> No, I focus on unsigned integers.
> 
> \ Unsigned integer number theory basicsinglext.4th 
> \
> \ isprime \ u -- flag
> \ nextprime \ u -- p
> \ primefactors \ u -- q1 q2 ... qn n
> \ radical \ n -- r
> \ totients \ n -- t
> \ mobius \ n -- m
> \ bigomega \ n -- b 
> \ smallomega \ n -- s
> \ legendre \ a p -- i
> \ tau \ u -- n    The number of divisors of u
> \ sigma \ u -- s  The sum of all divisors of u
> \ choose \ n k -- nCk
> \ sqrtf \ u -- floor
> \ log~ \ n -- 1+log n
> \ umin \ u1 u2 -- u
> \ umax \ u1 u2 -- u
> \ umod \ u q -- u(mod q)
> \ ugcd \ u1 u2 -- u
> \ u*mod \ u1 u2 u3 -- u1*u2(mod u3)
> \ u**mod \ b a m -- b^a(mod m)
> 
> https://github.com/Lehs/ANS-Forth-libraries

I have double-precision GCD etc. in the novice-package.

I have CF.4TH in the novice-package that calculates continued fractions. This was written by Nathaniel Grosman in a "Forth Dimensions" article for 16-bit Forth-83 --- I upgraded it slightly to work under 32-bit ANS-Forth.

Porting this to ANS-Forth was one of my very first ANS-Forth efforts. Elizabeth Rather told me that I can never be a Forth programmer because I complained about the lack of double-precision division in ANS-Forth.

Here is the first half of the CF.4TH code:
-------------------------------------------------------------------------

\ ****** 
\ ****** This is the first section, which provides UD/MOD.
\ ****** 

: LPswap ( d1 d2 d3 d4 -- d3 d4 d1 d2 )
    >r >r 2swap  >r >r 2swap  r> r> r> r> 2swap
    >r >r 2swap  r> r> ;
    
: LPdup ( d1 d2 -- d1 d2 d1 d2 )    
    2dup >r >r  2over r> r> ;
    
: LPover ( d1 d2 d3 d4 -- d1 d2 d3 d4 d1 d2 )        
    >r >r >r >r  LPdup  r> r> r> r>  LPswap ;

: LProt ( d1 d2 d3 d4 d5 d6 -- d3 d4 d5 d6 d1 d2 )
    >r >r >r >r LPswap  r> r> r> r> LPswap ;
    
: LPdrop ( d1 d2 -- )   
    drop drop drop drop ;
    
: um/ ( ud un -- un )                   \ divide UD by UN and drop remainder
    um/mod  swap drop ;
    
: u*/ ( Umultiplier Udividend Udivisor -- Uquotient )     
    >r um*  r> um/mod nip ;
        
: t* ( ud un -- ut )    
    dup rot um* >r >r
            um*
    0 r> r> d+ ;            

: t/ ( ut un -- ud )
    >r      r@ um/mod swap
    rot 0   r@ um/mod swap
    rot     r> um/mod swap drop
    0 2swap swap d+ ;
    
: ut*/ ( ud un un -- ud )               \ this was called U*/ in Grossman's article, but I'm already using that name
    >r  t*  r> t/ ;

: d- ( d1 d2 -- d1-d2 )    
    dnegate d+ ;
        
: narrow-ud/mod ( UDividend UNdivisor -- UDremainder UDquotient )    
    drop >r 2dup r@                     \ shuck high cell of divisor
    1 swap ut*/                         \ -- UDquotient
    2swap 2over r> 1 ut*/ d- 2swap ;    \ -- UDrem UDquot
    
variable numH
variable denH
variable denL
variable denscale
2variable num
2variable den
0 1 2constant superbase                 \ this was 65536. in Grossman's article

: us>d                                  \ convert unsigned single-cell to double-cell
    0 ;

: ud/mod-tuck ( ud ud -- )              \ save parts of num and den
    2dup den 2!  denH !  denL !
    2dup num 2!  numH !  drop ;
    
: ud/mod-denscale ( -- un )             \ for scaling-up den
    superbase  denH @ 1+ um/
    denscale ! ;
    
: scale-den ( -- )                      \ multiply denominator by scale factor
    den 2@  denscale @  1  ut*/
    denH !  denL ! ;
    
: wide-quot ( -- ud )                   \ if divisor needs more than one cell
    num 2@  numH @  us>d
    denL @  denH @  ut*/  d-
    denscale @  denH @  ut*/  swap drop ;
    
: ?narrow-divisor ( d -- ? )            \ is divisor < superbase?
    dup 0= ;
    
: wide-rem ( -- ud )                    \ remainder in wide division
    dup num 2@ rot  den 2@
    rot 1 ut*/ d-  rot us>d ;
    
: wide-ud/mod ( UDdividend UDdivisor -- UDremainder UDquotient )    
    ud/mod-tuck
    ud/mod-denscale
    scale-den
    wide-quot
    wide-rem ;
    
: ud/mod ( UDdividend UDdivisor -- UDremainder UDquotient )    
    ?narrow-divisor if  narrow-ud/mod
    else                wide-ud/mod     then ;
    
: udmod ( UDdividend UDdivisor -- UDremainder )    
    ud/mod  2drop ;
    
: ud/ ( UDdividend UDdivisor -- UDquotient )    
    ud/mod  2swap 2drop ;

[toc] | [prev] | [next] | [standalone]


#56426

Fromlehs <skydda.barnen@gmail.com>
Date2017-03-28 10:55 -0700
Message-ID<e9bfae55-4a2d-4387-8d8e-5c478cf5b663@googlegroups.com>
In reply to#56424
Den tisdag 28 mars 2017 kl. 19:26:37 UTC+2 skrev hughag...@gmail.com:
> On Monday, March 27, 2017 at 11:33:50 PM UTC-7, lehs wrote:
> > Den tisdag 28 mars 2017 kl. 01:44:40 UTC+2 skrev Albert van der Horst:
> > > Get rid of the u. They are numbers.
> > > When was the last time that you had a number between
> > > 18446744073709551615 and half of that?
> > 
> > No, I focus on unsigned integers.
> > 
> > \ Unsigned integer number theory basicsinglext.4th 
> > \
> > \ isprime \ u -- flag
> > \ nextprime \ u -- p
> > \ primefactors \ u -- q1 q2 ... qn n
> > \ radical \ n -- r
> > \ totients \ n -- t
> > \ mobius \ n -- m
> > \ bigomega \ n -- b 
> > \ smallomega \ n -- s
> > \ legendre \ a p -- i
> > \ tau \ u -- n    The number of divisors of u
> > \ sigma \ u -- s  The sum of all divisors of u
> > \ choose \ n k -- nCk
> > \ sqrtf \ u -- floor
> > \ log~ \ n -- 1+log n
> > \ umin \ u1 u2 -- u
> > \ umax \ u1 u2 -- u
> > \ umod \ u q -- u(mod q)
> > \ ugcd \ u1 u2 -- u
> > \ u*mod \ u1 u2 u3 -- u1*u2(mod u3)
> > \ u**mod \ b a m -- b^a(mod m)
> > 
> > https://github.com/Lehs/ANS-Forth-libraries
> 
> I have double-precision GCD etc. in the novice-package.
> 
> I have CF.4TH in the novice-package that calculates continued fractions. This was written by Nathaniel Grosman in a "Forth Dimensions" article for 16-bit Forth-83 --- I upgraded it slightly to work under 32-bit ANS-Forth.
> 
> Porting this to ANS-Forth was one of my very first ANS-Forth efforts. Elizabeth Rather told me that I can never be a Forth programmer because I complained about the lack of double-precision division in ANS-Forth.
> 
> Here is the first half of the CF.4TH code:
> -------------------------------------------------------------------------
> 
> \ ****** 
> \ ****** This is the first section, which provides UD/MOD.
> \ ****** 
> 
> : LPswap ( d1 d2 d3 d4 -- d3 d4 d1 d2 )
>     >r >r 2swap  >r >r 2swap  r> r> r> r> 2swap
>     >r >r 2swap  r> r> ;
>     
> : LPdup ( d1 d2 -- d1 d2 d1 d2 )    
>     2dup >r >r  2over r> r> ;
>     
> : LPover ( d1 d2 d3 d4 -- d1 d2 d3 d4 d1 d2 )        
>     >r >r >r >r  LPdup  r> r> r> r>  LPswap ;
> 
> : LProt ( d1 d2 d3 d4 d5 d6 -- d3 d4 d5 d6 d1 d2 )
>     >r >r >r >r LPswap  r> r> r> r> LPswap ;
>     
> : LPdrop ( d1 d2 -- )   
>     drop drop drop drop ;
>     
> : um/ ( ud un -- un )                   \ divide UD by UN and drop remainder
>     um/mod  swap drop ;
>     
> : u*/ ( Umultiplier Udividend Udivisor -- Uquotient )     
>     >r um*  r> um/mod nip ;
>         
> : t* ( ud un -- ut )    
>     dup rot um* >r >r
>             um*
>     0 r> r> d+ ;            
> 
> : t/ ( ut un -- ud )
>     >r      r@ um/mod swap
>     rot 0   r@ um/mod swap
>     rot     r> um/mod swap drop
>     0 2swap swap d+ ;
>     
> : ut*/ ( ud un un -- ud )               \ this was called U*/ in Grossman's article, but I'm already using that name
>     >r  t*  r> t/ ;
> 
> : d- ( d1 d2 -- d1-d2 )    
>     dnegate d+ ;
>         
> : narrow-ud/mod ( UDividend UNdivisor -- UDremainder UDquotient )    
>     drop >r 2dup r@                     \ shuck high cell of divisor
>     1 swap ut*/                         \ -- UDquotient
>     2swap 2over r> 1 ut*/ d- 2swap ;    \ -- UDrem UDquot
>     
> variable numH
> variable denH
> variable denL
> variable denscale
> 2variable num
> 2variable den
> 0 1 2constant superbase                 \ this was 65536. in Grossman's article
> 
> : us>d                                  \ convert unsigned single-cell to double-cell
>     0 ;
> 
> : ud/mod-tuck ( ud ud -- )              \ save parts of num and den
>     2dup den 2!  denH !  denL !
>     2dup num 2!  numH !  drop ;
>     
> : ud/mod-denscale ( -- un )             \ for scaling-up den
>     superbase  denH @ 1+ um/
>     denscale ! ;
>     
> : scale-den ( -- )                      \ multiply denominator by scale factor
>     den 2@  denscale @  1  ut*/
>     denH !  denL ! ;
>     
> : wide-quot ( -- ud )                   \ if divisor needs more than one cell
>     num 2@  numH @  us>d
>     denL @  denH @  ut*/  d-
>     denscale @  denH @  ut*/  swap drop ;
>     
> : ?narrow-divisor ( d -- ? )            \ is divisor < superbase?
>     dup 0= ;
>     
> : wide-rem ( -- ud )                    \ remainder in wide division
>     dup num 2@ rot  den 2@
>     rot 1 ut*/ d-  rot us>d ;
>     
> : wide-ud/mod ( UDdividend UDdivisor -- UDremainder UDquotient )    
>     ud/mod-tuck
>     ud/mod-denscale
>     scale-den
>     wide-quot
>     wide-rem ;
>     
> : ud/mod ( UDdividend UDdivisor -- UDremainder UDquotient )    
>     ?narrow-divisor if  narrow-ud/mod
>     else                wide-ud/mod     then ;
>     
> : udmod ( UDdividend UDdivisor -- UDremainder )    
>     ud/mod  2drop ;
>     
> : ud/ ( UDdividend UDdivisor -- UDquotient )    
>     ud/mod  2swap 2drop ;

Impressive with double precision division words, but it doesn't solve the problems with single precision number theoretical words.

If someone loaded https://github.com/Lehs/ANS-Forth-libraries there was an error with TOTIENTS TAU SIGMA SIGMA** which is corrected now.

It really doesn't matter where libraries are stored as long as they are described appropriate enough to be possible to google when there is a certain need.

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.forth


csiph-web