Sujet : Re: Complex square root of -1 : zsqrt(-1)
De : melahi_ahmed (at) *nospam* yahoo.fr (ahmed)
Groupes : comp.lang.forthDate : 28. Aug 2024, 23:10:46
Autres entêtes
Organisation : novaBBS
Message-ID : <db254672d6f9222e538709f19433c6d0@www.novabbs.com>
References : 1 2 3 4 5 6 7 8 9 10
User-Agent : Rocksolid Light
On Wed, 28 Aug 2024 21:42:15 +0000, mhx wrote:
How about
: xpythag ( F: a b -- c ) \ compute sqrt(a^2+b^2) without overflow
FABS FSWAP FABS FSWAP
F2DUP F> IF FOVER ( F: a b a -- ) F/ FSQR F1+ FSQRT F* EXIT ENDIF
FDUP F0= IF 0e
ELSE FTUCK ( F: b a b -- ) F/ FSQR F1+ FSQRT F*
ENDIF ;
>
FORTH> 1e-309 0e xpythag +e. 1.0000000000000000000e-0309 ok
FORTH> 1e-319 0e xpythag +e. 9.9999999999999999992e-0320 ok
>
-marcel
In my defintion of |z| I forgot to process the case where a=0 or b=0.
Yes, your definition captures my approach (idea).
It works but for 0e 0e xpythag gives 0e and the fstack is not consumed.
We must empty the fstack in this case.
the defintion becomes
: xpythag ( F: a b -- c ) \ compute sqrt(a^2+b^2) without overflow
FABS FSWAP FABS FSWAP
F2DUP F> IF FOVER ( F: a b a -- ) F/ FSQR F1+ FSQRT F* EXIT ENDIF
FDUP F0= IF fdrop fdrop 0e
ELSE FTUCK ( F: b a b -- ) F/ FSQR F1+ FSQRT F*
ENDIF ;
and for z** we can use this defintion of xpythag for |z|
Ahmed