Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 43 |
Nodes: | 6 (0 / 6) |
Uptime: | 94:18:47 |
Calls: | 290 |
Calls today: | 1 |
Files: | 904 |
Messages: | 76,378 |
-1+0i 0.5 c:^ .0.000000+1.000000i
-1+0i 0.5 c:^ dup c:* .-1.000000+0.000000i
Hi,
With gforth, complex.fs included.
When calculating the square root of -1, I find that:
-1e 0e 0.5e 0e z** z. 0.0000000000000000612303176911189+1.i ok
-1e 0e zsqrt z. NaN ok
What's the problem with zsqrt?
NB.
-1e 1e-16 zsqrt z. 0.0000000000000000612303176911189+1.i ok
-1e 1e-100 zsqrt z. 0.0000000000000000612303176911189+1.i ok
Ahmed
Hi,
With gforth, complex.fs included.
When calculating the square root of -1, I find that:
-1e 0e 0.5e 0e z** z. 0.0000000000000000612303176911189+1.i ok
-1e 0e zsqrt z. NaN ok
What's the problem with zsqrt?
Hi,
With gforth, complex.fs included.
-1e 0e zsqrt z. NaN ok
Hi,
With gforth, complex.fs included.
When calculating the square root of -1, I find that:
-1e 0e 0.5e 0e z** z. 0.0000000000000000612303176911189+1.i ok
-1e 0e zsqrt z. NaN ok
What's the problem with zsqrt?
NB.
-1e 1e-16 zsqrt z. 0.0000000000000000612303176911189+1.i ok
-1e 1e-100 zsqrt z. 0.0000000000000000612303176911189+1.i ok
0e 0e 1e 0e z^ z.
nan + inan ok \ incorrect
0e 0e 1e 0e z^ z.
nan + inan ok \ incorrect
0e 0e 0e 0e z** z. NaNai ok
it must be 1 ( tested with python, julia and matlab)
and
0e 0e 1e 0e z** z. NaNai ok, it must be 0
in general, with gforth: 0e 0e (a+bi) z** gives false results,
it must give 0e when (a+bi)<>0e.
So the definition of z** also must be revised.
Another one (always with complex.fs included):
0e 0e 0e 0e z** z. NaNai ok
it must be 1 ( tested with python, julia and matlab)
and
0e 0e 1e 0e z** z. NaNai ok, it must be 0
I think this version gives the same results as Matlab
: z** ( z: a+ib c+id -- e+if)
zdup z0= if zdrop zdrop 1e 0e exit then
fover f0> if zover z0= if zdrop zdrop 0e 0e exit then then
zdup f0= f0< and if zover z0= if zdrop zdrop Inf 0e exit then then
zswap zln z* zexp
;
some tests:
0e 0e 0e 0e z** z. 1. ok
0e 0e 1e 0e z** z. 0 ok
0e 0e -1e 0e z** z. inf ok
1e 1e 0e 0e z** z. 1. ok
1e 1e 0e 1e z** z. 0.428829006294368 +0.154871752464247 i ok
0e 0e 0e 1e z** z. NaN+NaNi ok
0e 0e 1e 1e z** z. 0 ok
0e 0e -1e 1e z** z. NaN+NaNi ok
-1e 0e 0.5e 0e z** z. 0.0000000000000000612303176911189 +1. i ok
-1e 0e 0e 0e z** z. 1. ok
-1e 1e 0e 0e z** z. 1. ok
-1e 1e 1e 0e z** z. -1. +1. i ok
-1e 1e 1e 1e z** z. -0.121339466446359 +0.0569501178644237 i ok
Ahmed
And what does Matlab/gForth give for this?
1e-309 0e -1e 1e z** z. ( 7.188026e+0307 -9.974133e+0308 ) ok
-marcel
polar gives 0e for 1e-309 0e ( 1e-309 0e >polar z. gives 0e) and that'sit must gives 1e-309 0e for 1e-309 0e.
a problem.
polar depends on |z| which gives 0e for 1e-309 0e ( 1e-309 0e |z| f.|z| depends on zsqabs which gives 0e for 1e-309 0e ( 1e-309 0e zsqabs f.
gives 0e) and that's a problem.
Another definition for |z|
: |z| ( z: a+ib -- m) ( f: a b--m)
fover fabs fover fabs ( f: a b |a| |b|)
fmax ( f: a b mx)
frot frot ( f: mx a b)
fover fabs fover fabs fmax ( f: mx a b mx)
ftuck ( f: mx a mx b mx)
f/ ( f: mx a mx b/mx)
fdup f* frot frot ( f: mx [b/mx]^2 a mx)
f/ fdup f* f+ fsqrt f* ;
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.
On Wed, 28 Aug 2024 21:42:15 +0000, mhx wrote:[..]
How about
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|