# ForwardDiff cannot assign ForwardDiff.Dual to input array

**URL:** <https://discourse.julialang.org/t/forwarddiff-cannot-assign-forwarddiff-dual-to-input-array/37522>\
**Category:** General Usage\
**Tags:** question, forwarddiff\
**Created:** [April 13, 2020, 5:54pm UTC](https://discourse.julialang.org/t/forwarddiff-cannot-assign-forwarddiff-dual-to-input-array/37522 "2020-04-13T17:54:21Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![bashonubuntu](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bashonubuntu](https://discourse.julialang.org/u/bashonubuntu)\
**Post date:** [April 13, 2020, 6:57pm UTC](https://discourse.julialang.org/t/forwarddiff-cannot-assign-forwarddiff-dual-to-input-array/37522/4 "2020-04-13T18:57:28Z")

</div>

Try using `eltype()` to debug. This often how I catch errors when I’m using ForwardDiff in my code. It’s likely that one of the many parameters might be having its type forced into something like an array of floats.

Same code using `eltype()` below. Notice that special ForwardDiff.Dual type being created. This is why you want to avoid forcing the type of a parameter to be Float64 because Julia can’t convert ForwardDiff.Dual type to a Float64, hence the "“cannot convert ForwardDiff.Dual to Float64 error.”

If you force the type of your parameter to be an array of floats, your code will try to promote ForwardDiff.Dual type to a Float64 and will fail.

```julia
function f_new(meta::Array{T}, init::Array{F}) where {T<:Real, F<:Float64}
    params = Array{T}(undef, size(init)[1], 1)
    println(eltype(params))
    for i=1:5
        params[i] = init[i]
    end
    println(eltype(params))
    for i=2:5
        params[i] = meta[1] * params[i-1]
    end
    println(eltype(params))
    return sum(params)
end

s = [1.,2.,3.,4.,5.]
ForwardDiff.gradient(x -> f_new(x, s), [2.0])

julia> ForwardDiff.gradient(x -> f_new(x, s), [2.0])
ForwardDiff.Dual{ForwardDiff.Tag{getfield(Main, Symbol("##73#74")),Float64},Float64,1}
ForwardDiff.Dual{ForwardDiff.Tag{getfield(Main, Symbol("##73#74")),Float64},Float64,1}
ForwardDiff.Dual{ForwardDiff.Tag{getfield(Main, Symbol("##73#74")),Float64},Float64,1}
1-element Array{Float64,1}:
 49.0

```

---

_[View the full topic](https://discourse.julialang.org/t/forwarddiff-cannot-assign-forwarddiff-dual-to-input-array/37522)._
