openbabel/openbabel — pull request #49 by andersx (2013-12-05 19:28:23+00:00)
Title: Fixing numerical stability in CalcTorsionAngle, and updating OBMol::Get/...

Note: This is the same pull-request as my previous pull-req (which I closed). Just squashed into one commit.

There were numerical instability in the way dihedral angles were calculated. The formula used relied on acos(x), which is ill-posed for several values of x. The old implementation also needed a workaround for a possible division by zero.
As a result it was for instance not possible to set a dihedral angle to 180, but only 179.974376548 at best. This means that errors can accumulate when setting and getting torsion angles multiple times.

This update uses atan2 instead of acos and thus does not need any checks or thresholds since there are no divisions and relevant issues regarding numerical stability are resolved in .

See for instance this discussion of torsion angles on stackexchange:
http://math.stackexchange.com/questions/47059/how-do-i-calculate-a-dihedral-angle-given-cartesian-coordinates.