Passing DoubleFloat types into a Julia function

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 (741 Bytes)

The Error Message I get is

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
  ...

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);

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

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> 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

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.

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