I'm getting strange behaviours with this function, it returns NaNs causing the example that uses it to fail sometimes. It seems to work once and subsequently fail consistently.  There might be something wrong with my setup as I don't see any problem with the function.  Did this happen to you?