It looks exactly same function as i have, just more wrapped in functions and added range check at the end (0..1 is the range between line start and end). I'm wondering if i have a typo somewhere... I did it according to more than 1 math site, just couldn't find any source code.

Btw in your function, if i assume right, you may get same division by zero which is handled in my code.