[Jprogramming] rounding complex numbers
Bo Jacoby
bojacoby at yahoo.dk
Wed May 2 18:11:29 HKT 2012
Hello J'ers
Even if a complex number is actually real it may display nonzero imaginary part because of round-off errors. Adding and subtracting 1e6j1e6 removes the round-off error, but is there a better or more general solution?
UM=.%:%~_1^]%~[:+:[:*/~i. NB. Unitary Matrix generator
rd=.]&.(1e6j1e6&+) NB. round-off cleaner
u=.n+/ .*+n=.UM 3 NB. unit matrix
u NB. ugly display
1 _2.78e_17j_1.11e_16 1.11e_16j_2.22e_16
_2.78e_17j1.11e_16 1 _2.78e_17j_1.11e_16
1.11e_16j2.22e_16 _2.78e_17j1.11e_16 1
rd u NB. nice display
1 0 0
0 1 0
0 0 1
More information about the Programming
mailing list