# Arrays of complex values with very small imaginary part

**URL:** <https://discourse.julialang.org/t/arrays-of-complex-values-with-very-small-imaginary-part/79621>\
**Category:** Numerics\
**Tags:** question, linearalgebra, complex-numbers\
**Created:** [April 18, 2022, 8:14am UTC](https://discourse.julialang.org/t/arrays-of-complex-values-with-very-small-imaginary-part/79621 "2022-04-18T08:14:18Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 18, 2022, 5:53pm UTC](https://discourse.julialang.org/t/arrays-of-complex-values-with-very-small-imaginary-part/79621/2 "2022-04-18T17:53:31Z")

</div>

> [@garrek](#):
>
> The problem is that sometimes the imaginary part is very very small when it turns out the matrix is real-valued.

See e.g.

> [@PSA: floating-point arithmetic](https://discourse.julialang.org/t/psa-floating-point-arithmetic/8678):
>
> Sometimes people are surprised by the results of floating-point calculations such as julia\> 5/6 0.8333333333333334 # shouldn't the last digit be 3? julia\> 2.6 - 0.7 - 1.9 2.220446049250313e-16 # shouldn't the answer be 0? These are not bugs in Julia. They’re consequences of the IEEE-standard 64-bit binary representation of floating-point numbers that is burned into computer hardware, which Julia and many other languages use by default. Brief explanation You can t…

For example, if you just have some roundoff errors in your construction of the complex arrays, you could get a tiny imaginary part in the eigenvalues.

If the real-ness of eigenvalues is physically meaningful, the best approach would be to construct your matrices so that this is guaranteed. e.g. construct your matrices so that they are Hermitian (and use `Hermitian(M)`) in the cases where you expect real eigenvalues. In a physical problem, often you can do this by expressing the physics in the right way…

If this is not possible, you’ll have to resort to some kind of precision test, e.g. replace `isreal(v)` with something like `norm(imag(v)) ≤ norm(imag(v)) * eps(eltype(v)) * length(v)`. But these kinds of tests are hard to make 100% robust.

---

_[View the full topic](https://discourse.julialang.org/t/arrays-of-complex-values-with-very-small-imaginary-part/79621)._
