Erratic behavior with complex arithmetic, e.g., sin(z) returns NaN
jp.long
My code is producing unexpected results when taking the sine of a complex number. I wrote the following test function to research the problem.
Function testsine()
variable /c a
a=cmplx(0,1)
print "a= ", a
print "sin(a)= ", sin(a)
print "sin(sqrt(-1))= ", sin(sqrt(-1))
end
[Click and drag to move]
variable /c a
a=cmplx(0,1)
print "a= ", a
print "sin(a)= ", sin(a)
print "sin(sqrt(-1))= ", sin(sqrt(-1))
end
[Click and drag to move]
The results printed in the Command Window are pasted here:
•testsine()
a= (0,1)
sin(a)= 0
sin(sqrt(-1))= NaN
The built-in sine function is not handling complex numbers as the manual leads me to believe, i.e., sin(x+iy)=sin(x)cosh(y)+ i cos(x)sinh(y). Shouldn't this have produced the result (0, 1.1752) in both cases above, i.e., the sin(a) and sin(sqrt(-1))?
This is rather a 'problem' of the print command not expecting a complex number unless explicitly getting handed one. Try this:
variable/c a,b,c
a=cmplx(0,1)
b=sin(a)
c=sin(sqrt(-1))
print "a= ", a
print "sin(a)= ", b
print "sin(sqrt(-1))= ", c
End
Will give:
a= (0,1)
sin(a)= (0,1.1752)
sin(sqrt(-1))= (0,1.1752)
June 5, 2021 at 07:15 pm - Permalink
Wow, who knew?
Well, chozo did! Thanks!
June 6, 2021 at 05:33 am - Permalink
print/C also solves the problem easily
variable /c a
a=cmplx(0,1)
print/C "a= ", a
print/C "sin(a)= ", sin(a)
end
with result
a= (0,1)
sin(a)= (0,1.1752)
June 6, 2021 at 06:16 am - Permalink
Oh, yes. I always forget that print has more flags than just /D. :-)
June 6, 2021 at 06:20 am - Permalink