Minor issue with if statement and may about types?

Hello, I just tried to make the following function.

function CRRA_bernoulli(ω::Float16,x::Int16)
    if ω == 1.00
        val = log( exp(1), x )
    else
        val = ( x / ( 1 - ω ) ) ^ ( 1 - ω )
    end
    return val
end

However, the result of putting ω = 1.0 showed the value 1.0.

CRRA_bernoulli(1.0,3850)

I do not understand why this is happening… Is ther e somebody who can help?

As written, this doesn’t run since 1.0 isn’t a Float16 and 3850 isn’t an Int16. You have another method defined for this function that is being called instead.

I found out that as I changed 16 to 64 for all, the problem would be solved.
I wonder if using 64 is a very typical thing to do. I was actually using 16 to save my memory.
Thanks for the help, btw!

if you wanted this to work for 16, you could by calling CRRA_bernoulli(Float16(1.0),Int16(3850)) although you should be aware that on most CPUS it will be a lot slower. You also don’t need the type annotations at all. They don’t make things faster. If you want them for documentation purposes, I would recommend ::Real and ::Integer which will let you pass in arbitrary sized data.

I thought if I described things in more detail, then the performance would improve.
I never know about this. Thank you for the advice.

Julia compiles a new assembly for your function for each combination of input types. types on function signatures only control dispatch.

2 Likes