I'm very new to F#, and I need to write an exponentiation function that accepts any types of variables.
let exp value pow =
let mutable result = 1.0
for i = 1 to abs pow do
result <- value * result
if pow > 0 then result else 1.0 / result
let rec quickExp value pow =
let result =
if pow = 0 then
1.0
else
let half = quickExp value (abs pow / 2)
if pow % 2 = 0 then half * half else value * half * half
if pow > 0 then result else 1.0 / result
Tried inline, but it doesn't work with recursion. I would be grateful for any help!
You need two tricks to get this to work:
inlineand mark the outer functioninline1.0which constraints the type tofloattoLanguagePrimitives.GenericOne, which is (as the name suggests) a generic 1 valueThe following seems to work for me: