# Conversion from float to complex?

**URL:** <https://discourse.julialang.org/t/conversion-from-float-to-complex/111890>\
**Category:** New to Julia\
**Created:** [March 20, 2024, 4:30pm UTC](https://discourse.julialang.org/t/conversion-from-float-to-complex/111890 "2024-03-20T16:30:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sarah\_Groves](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sarah_groves/32/207216_2.png) [@Sarah\_Groves](https://discourse.julialang.org/u/Sarah_Groves)\
**Post date:** [March 20, 2024, 4:30pm UTC](https://discourse.julialang.org/t/conversion-from-float-to-complex/111890/1 "2024-03-20T16:30:37Z")

</div>

I currently have code that takes two arrays, makes a grid of them, and then calculates the square root of the squares of each value:

```julia
hx = 1
hy = 1
Nx = 16
Ny = 16
x = hx .* (0:Nx-1)
y = hy .* (0:Ny-1)

function meshgrid(x, y)
    X = [i for i in x, j in 1:length(y)]
    Y = [j for i in 1:length(x), j in y]
    return X, Y
end

xx, yy = meshgrid(x, y)

@. R = sqrt((xx - 0.5)^2 + (yy - 0.5)^2)

```

However, xx and yy are both matrices of float64s, while R is calculated as complex values. Any idea why the change in variable type? All of the imaginary parts are 0, as expected. Would the best way to fix this be to change the variables back to floats (and how?) or to use a different function? It is not clear to me why my code for R is returning a matrix of complex numbers. Thanks!

---

<div class="post-metadata">

**Author:** ![IlianPihlajamaa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ilianpihlajamaa/32/28766_2.png) [@IlianPihlajamaa](https://discourse.julialang.org/u/IlianPihlajamaa)\
**Post date:** [March 20, 2024, 4:42pm UTC](https://discourse.julialang.org/t/conversion-from-float-to-complex/111890/2 "2024-03-20T16:42:33Z")

</div>

As of right now, your code does not run in an empty julia REPL. This is because your line

```julia
@. R = sqrt((xx - 0.5)^2 + (yy - 0.5)^2)

```

inserts the computed values on the right hand-side into an existing matrix R, which is not defined in your code snippet. In your session, you probably have an existing complex matrix R, which causes the problem. If you change this line to

```julia
R = @. sqrt((xx - 0.5)^2 + (yy - 0.5)^2)

```

The problem should be solved, because this will create a new matrix which is correctly inferred to have real elements.

---

<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:** [March 20, 2024, 4:53pm UTC](https://discourse.julialang.org/t/conversion-from-float-to-complex/111890/3 "2024-03-20T16:53:29Z")

</div>

> [@IlianPihlajamaa](#):
>
> `R = @. sqrt((xx - 0.5)^2 + (yy - 0.5)^2)`

Or use `R = @. hypot(xx - 0.5, yy - 0.5)`, which calls the [`hypot` function](https://docs.julialang.org/en/v1/base/math/#Base.Math.hypot), which can avoid spurious underflow/overflow when the arguments are very large/small, compared to `sqrt(a^2 + b^2)`.

---

<div class="post-metadata">

**Author:** ![Sarah\_Groves](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sarah_groves/32/207216_2.png) [@Sarah\_Groves](https://discourse.julialang.org/u/Sarah_Groves)\
**Post date:** [March 20, 2024, 5:03pm UTC](https://discourse.julialang.org/t/conversion-from-float-to-complex/111890/4 "2024-03-20T17:03:20Z")

</div>

Thank you so much!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 20, 2024, 7:12pm UTC](https://discourse.julialang.org/t/conversion-from-float-to-complex/111890/5 "2024-03-20T19:12:42Z")

</div>

> [@Sarah\_Groves](#):
>
> ```julia
> xx, yy = meshgrid(x, y)
> 
> @. R = sqrt((xx - 0.5)^2 + (yy - 0.5)^2)
> 
> ```

BTW, the `meshgrid` function is entirely redundant, you can just directly use `x` and `y` with broadcasting (notice the `'`):

```julia
R = @. sqrt((x' - 0.5)^2 + (y - 0.5)^2)

```

`meshgrid`-like functions are a Matlabism (I suspect), but isn’t even needed in Matlab (nor in numpy), you can use the same broadcasting mechanism in those, rendering `meshgrid` functions obsolete.
