# Passing DoubleFloat types into a Julia function

**URL:** <https://discourse.julialang.org/t/passing-doublefloat-types-into-a-julia-function/94425>\
**Category:** New to Julia\
**Tags:** package, doublefloats\
**Created:** [February 10, 2023, 6:59pm UTC](https://discourse.julialang.org/t/passing-doublefloat-types-into-a-julia-function/94425 "2023-02-10T18:59:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thushara\_Gunaratne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thushara_gunaratne/32/20372_2.png) [@Thushara\_Gunaratne](https://discourse.julialang.org/u/Thushara_Gunaratne)\
**Post date:** [February 10, 2023, 6:59pm UTC](https://discourse.julialang.org/t/passing-doublefloat-types-into-a-julia-function/94425/1 "2023-02-10T18:59:43Z")

</div>

I’m trying to create a julia function in order to optimize the implementation of a FIR filter that can handle data and coefficients represented with different Floating-Point formats (i.e. Double64, Double32, Double16, Float64, Float32, Float16, etc.). However I’m getting a Method error. I really appreciate if anyone can suggest me how to do this in a proper way.

[Test\_Function.jl](https://discourse.julialang.org/uploads/short-url/n6xJKSjhR4pDNKVYaZj0IQPMjtn.jl) (741 Bytes)

The Error Message I get is

```julia
ERROR: MethodError: no method matching Double64(::StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64})
Closest candidates are:
  (::Type{T})(::AbstractChar) where T<:Union{AbstractChar, Number} at char.jl:50
  (::Type{T})(::Base.TwicePrecision) where T<:Number at twiceprecision.jl:266
  (::Type{T})(::ColorTypes.AbstractGray) where T<:Real at C:\Users\xxxxxxx\.julia\packages\ColorTypes\1dGw6\src\conversions.jl:115
  ...

```

```plaintext

FloatFormt = Double64; #Double32, Double16

function function_with_nongeneric_numerics(;
    FloatFormt::DataType,
    N::Int64,
    x::Vector{FloatFormt}
    )
    
    h = FloatFormt((0:1:N-1)/N);
    xMask = zeros(FloatFormt)(N)
    Nx = length(x);
    y = zeros(FloatFormt, Nx);
    for i in 1 : 1 : Nx
        xMask[2:end] = xMask[1:end-1];
        xMask[1] = x[i];
        y[i] = sum(xMask.*h)/N;
    end #FOR i
    return(;
    y)
end #Function

#The Body of the Main Function
Nx = 150;
N = 4;

Fs = FloatFormt(100);
Tsamp = range(start=0, step=1/Fs, length=N);

x = cospi.(2*mod.(0.125*collect(Tsamp),2))

y = function_with_nongeneric_numerics(;
    FloatFormt,
    N,
    x);

```

---

<div class="post-metadata">

**Author:** ![heltonmc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heltonmc/32/43044_2.png) [@heltonmc](https://discourse.julialang.org/u/heltonmc)\
**Post date:** [February 10, 2023, 7:15pm UTC](https://discourse.julialang.org/t/passing-doublefloat-types-into-a-julia-function/94425/2 "2023-02-10T19:15:51Z")

</div>

I’m assuming this error can be simplified down to…

```julia
julia> using DoubleFloats
julia> FloatFormt = Double64; #Double32, Double16
julia> N = 12
julia> h = FloatFormt((0:1:N-1)/N);
ERROR: MethodError: no method matching Double64(::StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64})
Closest candidates are:
  (::Type{T})(::AbstractChar) where T<:Union{AbstractChar, Number} at char.jl:50
  (::Type{T})(::Base.TwicePrecision) where T<:Number at twiceprecision.jl:266
  DoubleFloat{T}(::T, ::DoubleFloat{T}) where T<:Union{Float16, Float32, Float64} at ~/.julia/packages/DoubleFloats/XUcgs/src/Double.jl:1

```

Using broadcasting there will help…

```julia
julia> h = FloatFormt.((0:1:N-1)/N)
12-element Vector{Double64}:
 0.0
 0.08333333333333333
 0.16666666666666666
 0.25
.
.
.

```

I would recommend not using this structure though as you can use more type generic code based on your input arguments. So something like

```julia
function foo(x::Vector{T}) where T
# use type of T for conversions within function
end

```

Now you can use `T` for precision parameters within function based on your input types.

---

<div class="post-metadata">

**Author:** ![Thushara\_Gunaratne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thushara_gunaratne/32/20372_2.png) [@Thushara\_Gunaratne](https://discourse.julialang.org/u/Thushara_Gunaratne)\
**Post date:** [February 10, 2023, 7:22pm UTC](https://discourse.julialang.org/t/passing-doublefloat-types-into-a-julia-function/94425/3 "2023-02-10T19:22:48Z")

</div>

Thank you very much! Let me digest and try the new structure you propose!
