Wrong output from det(Array{Complex})

Note that Complex32 stores its real and imaginary components as Float16s (so that the total size of a Complex32 is 32 bytes). Float16 is a really small floating point type, so large errors can be expected. For Complex128, the determinant is -4.654054919228656e-11 + 4.618527782440645e-12im, which isn’t too bad. Floating point math is just different from math with real numbers, so you may have to lower your expectations a bit when using them. See also this excellent post: PSA: floating-point arithmetic - #5 by Tamas_Papp.

If you really need a lot of precision (at the cost of performance), you could use Complex{BigFloat}. If you want to go that route, note that something like 26.0-52.0im constructs a Complex{Float64}, and if the number you want isn’t exactly representable as a Complex{Float64}, you’ll lose precision already before converting to BigFloat. Alternatively, in your specific case, the real and imaginary parts are integers, so if you replace e.g. 26.0-52.0im with 26-52im and construct an Array{BigFloat}, you won’t lose precision.

See also: Accuracy of Computing Determinants - #3 by andreasnoack.

2 Likes