# How to migrate old julia code that called a function with a range

**URL:** https://discourse.julialang.org/t/how-to-migrate-old-julia-code-that-called-a-function-with-a-range/43944
**Category:** General Usage
**Created:** [July 30, 2020, 3:31am UTC](https://discourse.julialang.org/t/how-to-migrate-old-julia-code-that-called-a-function-with-a-range/43944 "2020-07-30T03:31:49Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Peeter\_Joot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peeter_joot/32/16693_2.png) [@Peeter\_Joot](https://discourse.julialang.org/u/Peeter_Joot)
#### Post date: [July 30, 2020, 3:31am UTC](https://discourse.julialang.org/t/how-to-migrate-old-julia-code-that-called-a-function-with-a-range/43944/1 "2020-07-30T03:31:49Z")

</div>

I’d used some really old versions of julia circa 2015, and recently installed 1.4.2 to play around with the current version.

I used to be able to compute arrays of function call values (at least for some types of functions) using ranges (in a way that I seem to recall was matlab like). Here is an example:

```julia
   theta = linrange( 0, 1 * pi, 500 ) ;
   ct = pi * cos(theta) ;

```

With julia-1.4.2 I have to update the linrange to use range or LinRange, but even after doing so, I’m not able to call cos() using a range anymore:

```julia
julia> theta = range( 0, stop=pi, length=500)
0.0:0.006295776860901389:3.141592653589793

julia> cos(theta)
ERROR: MethodError: no method matching cos(::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}})
Closest candidates are:
  cos(::BigFloat) at mpfr.jl:683
  cos(::Missing) at math.jl:1056
  cos(::Complex{Float16}) at math.jl:1005
  ...
Stacktrace:
 [1] top-level scope at none:0

```

Is there a new paradigm to thread a function over a range in this more modern Julia version?

---

<div class="post-metadata">

### Author: ![lucas711642](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas711642/32/12051_2.png) [@lucas711642](https://discourse.julialang.org/u/lucas711642)
#### Post date: [July 30, 2020, 3:34am UTC](https://discourse.julialang.org/t/how-to-migrate-old-julia-code-that-called-a-function-with-a-range/43944/2 "2020-07-30T03:34:44Z")

</div>

You should broadcast `theta`:

```julia
cos.(theta)

```

[https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting-1](https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting-1)

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [July 30, 2020, 3:40am UTC](https://discourse.julialang.org/t/how-to-migrate-old-julia-code-that-called-a-function-with-a-range/43944/3 "2020-07-30T03:40:07Z")

</div>

You might specifically want `ct = cospi.(theta)` the trig functions with `pi` in the end of their name are a little more accurate than multiplying by `pi` since multiplying by an irrational adds some error.  
Edit: this is only if you wanted `cos.(pi*x)` not `pi*cos.(x)`

---

<div class="post-metadata">

### Author: ![Peeter\_Joot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peeter_joot/32/16693_2.png) [@Peeter\_Joot](https://discourse.julialang.org/u/Peeter_Joot)
#### Post date: [July 30, 2020, 3:40am UTC](https://discourse.julialang.org/t/how-to-migrate-old-julia-code-that-called-a-function-with-a-range/43944/4 "2020-07-30T03:40:41Z")

</div>

Thanks Lucus. That explicit syntax is nice. I’ll give it a try.
