sot / Quaternion

Quaternion manipulation
https://sot.github.io/Quaternion
BSD 3-Clause "New" or "Revised" License
3 stars 7 forks source link

xn**2 is sometimes negative #1

Closed jeanconn closed 8 years ago

jeanconn commented 11 years ago

Bug Report and suggested fix from Shuning Bian:

From time to time I encounter ValueErrors when with the Quaternion library when extracting the equatorial angles. For example, this happens:

File "/.../lib/python2.7/site-packages/Quaternion.py", line 233, in _quat2equatorial dec = degrees(atan2(xn , sqrt(1 - xn**2))); ValueError: math domain error

This appears to be because 1-xn2 is occasionally negative due to floating point issues. Since the expression 1-xn2 should never be negative, a simple fix is to wrap it in max(0,1-xn**2) inside sqrt.

jeanconn commented 8 years ago

He suggested this test

quat= Quat((0, 0, 0))
angle = 0
while angle < 360:
    q = Quat((0, angle, 0))
    quat = q * quat
    quat.equatorial
    angle += 0.1