Julia generics function type parameter

As it was said, you can abuse Julia type system, but this is highly non-idiomatic and should never ever be used in practice.

struct ApproxPi{T} end

function ApproxPi{T}(n) where T
    tot = zero(T) 
    for i in 1:n
        x = rand(T)
        y = rand(T)
        if x^2 + y^2 < 1
            tot+=1
        end
    end
    tot / n * 4
end

julia> ApproxPi{Float32}(100_000)
3.14144f0

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top