Task: Fixes #3781 (rdkit/rdkit#3975)

org: rdkit
repo: rdkit
pr_number: 3975
issue_title: Fixes #3781
issue_body: - fixes energies and gradients for dihedral and angle constraints in both MMFF and UFF
- adds butane scan tests for MMFF and UFF

Now the butane scan works as expected for both UFF...
![image](https://user-images.githubusercontent.com/5244385/112561582-d1a90e80-8dd5-11eb-832a-bc85cfa66098.png)

...and MMFF:
![image](https://user-images.githubusercontent.com/5244385/112561383-64957900-8dd5-11eb-9306-b14bef26d81e.png)

resolved_issues: number: 3781
title: Regression in dihedral constraints
body: In this code snippet I just use an added torsion constraint with a very high force constant to get certain dihedral angle.  It works well in version "2019.03.3" but gives very bad results in "2020.09.1".

```
m = Chem.rdmolfiles.MolFromMolFile('/Users/jasonb/butane.mol',removeHs = False)
mp = AllChem.MMFFGetMoleculeProperties(m)
ff = AllChem.MMFFGetMoleculeForceField(m, mp)
ff.MMFFAddTorsionConstraint(0,1,2,3, False, -180, -180, 10000.0)
ff.Minimize()
Chem.rdmolfiles.MolToMolFile(m,'/Users/jasonb/butane_180.mol')
```
Starting with the attached MOL file, rdkit version "2019.03.3" gives the top result, while version "2020.09.1" gives the bottom version:

<img width="286" alt="image" src="https://user-images.githubusercontent.com/16337306/106640656-ec84a280-654b-11eb-9994-c477e9f11d67.png">

(can't seem to attach a .mol file, renamed to 'butane.txt')

[butane.txt](https://github.com/rdkit/rdkit/files/5913233/butane.txt)

edit:

To add another example, using the same MOL file and this code
```
m = Chem.rdmolfiles.MolFromMolFile('/Users/jasonb/butane.txt',removeHs = False)
mp = AllChem.MMFFGetMoleculeProperties(m)
globalFF = AllChem.MMFFGetMoleculeForceField(m, mp)
m2 = m;
res = []
for x in range(-180,180,5):
	ff2 = AllChem.MMFFGetMoleculeForceField(m2, mp)
	ff2.MMFFAddTorsionConstraint(0,1,2,3, False, x - .1, x + .1, 100000.0)
	ff2.Minimize()
	res.append(globalFF.CalcEnergy(ff2.Positions()))
res
```
I was previously able to get a nice plot like this,
<img width="307" alt="image" src="https://user-images.githubusercontent.com/16337306/106652423-0ed1ec80-655b-11eb-979c-260176ac21ad.png">
but it now returns a **very different** list

<img width="379" alt="image" src="https://user-images.githubusercontent.com/16337306/106652585-480a5c80-655b-11eb-8024-04a1eae90ade.png">

_Originally posted by @jasondbiggs in https://github.com/rdkit/rdkit/discussions/3780_

Reference solution:
fix_patch: diff --git a/Code/ForceField/ForceField.cpp b/Code/ForceField/ForceField.cpp
index b4028891d2f..0133c5f854e 100644
--- a/Code/ForceField/ForceField.cpp
+++ b/Code/ForceField/ForceField.cpp
@@ -17,45 +17,40 @@
 namespace RDKit {
 namespace ForceFieldsHelper {
 void normalizeAngleDeg(double &angleDeg) {
-  double normFactor = 360.0;
-  if (angleDeg < 0.0) {
-    normFactor = -normFactor;
-  }
-  angleDeg = fmod(angleDeg, normFactor);
-  if (fabs(angleDeg) > 180.0) {
-    angleDeg -= normFactor;
+  angleDeg = fmod(angleDeg, 360.0);
+  if (angleDeg < -180.0) {
+    angleDeg += 360.0;
+  } else if (angleDeg > 180.0) {
+    angleDeg -= 360.0;
   }
 }
 
 void computeDihedral(const RDGeom::PointPtrVect &pos, unsigned int idx1,
-    unsigned int idx2, unsigned int idx3, unsigned int idx4,
-    double *dihedral, double *cosPhi, RDGeom::Point3D r[4],
-    RDGeom::Point3D t[2], double d[2]) {
+                     unsigned int idx2, unsigned int idx3, unsigned int idx4,
+                     double *dihedral, double *cosPhi, RDGeom::Point3D r[4],
+                     RDGeom::Point3D t[2], double d[2]) {
   computeDihedral(static_cast<RDGeom::Point3D *>(pos[idx1]),
-    static_cast<RDGeom::Point3D *>(pos[idx2]),
-    static_cast<RDGeom::Point3D *>(pos[idx3]),
-    static_cast<RDGeom::Point3D *>(pos[idx4]), dihedral, cosPhi, r, t, d);
+                  static_cast<RDGeom::Point3D *>(pos[idx2]),
+                  static_cast<RDGeom::Point3D *>(pos[idx3]),
+                  static_cast<RDGeom::Point3D *>(pos[idx4]), dihedral, cosPhi,
+                  r, t, d);
 }
 
-void computeDihedral(const double *pos, unsigned int idx1,
-    unsigned int idx2, unsigned int idx3, unsigned int idx4,
-    double *dihedral, double *cosPhi, RDGeom::Point3D r[4],
-    RDGeom::Point3D t[2], double d[2]) {
-  RDGeom::Point3D p1(pos[3 * idx1], pos[3 * idx1 + 1],
-                     pos[3 * idx1 + 2]);
-  RDGeom::Point3D p2(pos[3 * idx2], pos[3 * idx2 + 1],
-                     pos[3 * idx2 + 2]);
-  RDGeom::Point3D p3(pos[3 * idx3], pos[3 * idx3 + 1],
-                     pos[3 * idx3 + 2]);
-  RDGeom::Point3D p4(pos[3 * idx4], pos[3 * idx4 + 1],
-                     pos[3 * idx4 + 2]);
+void computeDihedral(const double *pos, unsigned int idx1, unsigned int idx2,
+                     unsigned int idx3, unsigned int idx4, double *dihedral,
+                     double *cosPhi, RDGeom::Point3D r[4], RDGeom::Point3D t[2],
+                     double d[2]) {
+  RDGeom::Point3D p1(pos[3 * idx1], pos[3 * idx1 + 1], pos[3 * idx1 + 2]);
+  RDGeom::Point3D p2(pos[3 * idx2], pos[3 * idx2 + 1], pos[3 * idx2 + 2]);
+  RDGeom::Point3D p3(pos[3 * idx3], pos[3 * idx3 + 1], pos[3 * idx3 + 2]);
+  RDGeom::Point3D p4(pos[3 * idx4], pos[3 * idx4 + 1], pos[3 * idx4 + 2]);
   computeDihedral(&p1, &p2, &p3, &p4, dihedral, cosPhi, r, t, d);
 }
 
 void computeDihedral(const RDGeom::Point3D *p1, const RDGeom::Point3D *p2,
-    const RDGeom::Point3D *p3, const RDGeom::Point3D *p4,
-    double *dihedral, double *cosPhi, RDGeom::Point3D r[4],
-    RDGeom::Point3D t[2], double d[2]) {
+                     const RDGeom::Point3D *p3, const RDGeom::Point3D *p4,
+                     double *dihedral, double *cosPhi, RDGeom::Point3D r[4],
+                     RDGeom::Point3D t[2], double d[2]) {
   PRECONDITION(p1, "p1 must not be null");
   PRECONDITION(p2, "p2 must not be null");
   PRECONDITION(p3, "p3 must not be null");
@@ -95,8 +90,8 @@ void computeDihedral(const RDGeom::Point3D *p1, const RDGeom::Point3D *p2,
     *dihedral = -atan2(m.dotProduct(t[1]) / mLength, *cosPhi);
   }
 }
-}
-}
+}  // namespace ForceFieldsHelper
+}  // namespace RDKit
 
 namespace ForceFieldsHelper {
 class calcEnergy {
@@ -152,7 +147,7 @@ class calcGradient {
  private:
   ForceFields::ForceField *mp_ffHolder;
 };
-}
+}  // namespace ForceFieldsHelper
 
 namespace ForceFields {
 ForceField::~ForceField() {
@@ -429,4 +424,4 @@ void ForceField::initDistanceMatrix() {
     dp_distMat[i] = -1.0;
   }
 }
-}
+}  // namespace ForceFields
diff --git a/Code/ForceField/MMFF/AngleConstraint.cpp b/Code/ForceField/MMFF/AngleConstraint.cpp
index 1bd4db0fc02..3670ba1c556 100644
--- a/Code/ForceField/MMFF/AngleConstraint.cpp
+++ b/Code/ForceField/MMFF/AngleConstraint.cpp
@@ -10,7 +10,6 @@
 //  which is included in the file license.txt, found at the root
 //  of the RDKit source tree.
 //
-#include "AngleBend.h"
 #include "AngleConstraint.h"
 #include "Params.h"
 #include <cmath>
@@ -19,28 +18,6 @@
 
 namespace ForceFields {
 namespace MMFF {
-void _pretreatAngles(double &minAngleDeg, double &maxAngleDeg) {
-  minAngleDeg = fmod(minAngleDeg, 360.0);
-  maxAngleDeg = fmod(maxAngleDeg, 360.0);
-  if (minAngleDeg > 180.0) {
-    minAngleDeg -= 360.0;
-  }
-  if (maxAngleDeg > 180.0) {
-    maxAngleDeg -= 360.0;
-  }
-  if ((minAngleDeg < 0.0) && (!(maxAngleDeg < 0.0))) {
-    maxAngleDeg = std::max(fabs(maxAngleDeg), fabs(minAngleDeg));
-    minAngleDeg = 0.0;
-  }
-  minAngleDeg = fabs(minAngleDeg);
-  maxAngleDeg = fabs(maxAngleDeg);
-  if (minAngleDeg > maxAngleDeg) {
-    double t = minAngleDeg;
-    minAngleDeg = maxAngleDeg;
-    maxAngleDeg = t;
-  }
-}
-
 AngleConstraintContrib::AngleConstraintContrib(
     ForceField *owner, unsigned int idx1, unsigned int idx2, unsigned int idx3,
     double minAngleDeg, double maxAngleDeg, double forceConst) {
@@ -48,8 +25,10 @@ AngleConstraintContrib::AngleConstraintContrib(
   URANGE_CHECK(idx1, owner->positions().size());
   URANGE_CHECK(idx2, owner->positions().size());
   URANGE_CHECK(idx3, owner->positions().size());
-  PRECONDITION(maxAngleDeg >= minAngleDeg, "allowedDeltaDeg must be >= 0.0");
-  _pretreatAngles(minAngleDeg, maxAngleDeg);
+  PRECONDITION(!(minAngleDeg > maxAngleDeg),
+               "minAngleDeg must be <= maxAngleDeg");
+  RDKit::ForceFieldsHelper::normalizeAngleDeg(minAngleDeg);
+  RDKit::ForceFieldsHelper::normalizeAngleDeg(maxAngleDeg);
 
   dp_forceField = owner;
   d_at1Idx = idx1;
@@ -68,7 +47,8 @@ AngleConstraintContrib::AngleConstraintContrib(
   URANGE_CHECK(idx1, pos.size());
   URANGE_CHECK(idx2, pos.size());
   URANGE_CHECK(idx3, pos.size());
-  PRECONDITION(maxAngleDeg >= minAngleDeg, "allowedDeltaDeg must be >= 0.0");
+  PRECONDITION(!(minAngleDeg > maxAngleDeg),
+               "minAngleDeg must be <= maxAngleDeg");
 
   double angle = 0.0;
   if (relative) {
@@ -89,19 +69,27 @@ AngleConstraintContrib::AngleConstraintContrib(
   d_at3Idx = idx3;
   minAngleDeg += angle;
   maxAngleDeg += angle;
-  _pretreatAngles(minAngleDeg, maxAngleDeg);
+  RDKit::ForceFieldsHelper::normalizeAngleDeg(minAngleDeg);
+  RDKit::ForceFieldsHelper::normalizeAngleDeg(maxAngleDeg);
   d_minAngleDeg = minAngleDeg;
   d_maxAngleDeg = maxAngleDeg;
   d_forceConstant = forceConst;
 }
 
+double AngleConstraintContrib::computeAngleTerm(double angle) const {
+  double angleTerm = 0.0;
+  if (angle < d_minAngleDeg) {
+    angleTerm = angle - d_minAngleDeg;
+  } else if (angle > d_maxAngleDeg) {
+    angleTerm = angle - d_maxAngleDeg;
+  }
+  return angleTerm;
+}
+
 double AngleConstraintContrib::getEnergy(double *pos) const {
   PRECONDITION(dp_forceField, "no owner");
   PRECONDITION(pos, "bad vector");
 
-  double dist1 = dp_forceField->distance(d_at1Idx, d_at2Idx, pos);
-  double dist2 = dp_forceField->distance(d_at2Idx, d_at3Idx, pos);
-
   RDGeom::Point3D p1(pos[3 * d_at1Idx], pos[3 * d_at1Idx + 1],
                      pos[3 * d_at1Idx + 2]);
   RDGeom::Point3D p2(pos[3 * d_at2Idx], pos[3 * d_at2Idx + 1],
@@ -109,19 +97,14 @@ double AngleConstraintContrib::getEnergy(double *pos) const {
   RDGeom::Point3D p3(pos[3 * d_at3Idx], pos[3 * d_at3Idx + 1],
                      pos[3 * d_at3Idx + 2]);
 
-  RDGeom::Point3D p12 = (p1 - p2) / dist1;
-  RDGeom::Point3D p32 = (p3 - p2) / dist2;
-  double cosTheta = p12.dotProduct(p32);
+  RDGeom::Point3D r[2] = {p1 - p2, p3 - p2};
+  double rLengthSq[2] = {(std::max)(1.0e-5, r[0].lengthSq()),
+                         (std::max)(1.0e-5, r[1].lengthSq())};
+  double cosTheta = r[0].dotProduct(r[1]) / sqrt(rLengthSq[0] * rLengthSq[1]);
   clipToOne(cosTheta);
   double angle = RAD2DEG * acos(cosTheta);
-  double angleTerm = 0.0;
-  if (angle < d_minAngleDeg) {
-    angleTerm = angle - d_minAngleDeg;
-  } else if (angle > d_maxAngleDeg) {
-    angleTerm = angle - d_maxAngleDeg;
-  }
-  double const c = 0.5 * DEG2RAD * DEG2RAD;
-  double res = c * d_forceConstant * angleTerm * angleTerm;
+  double angleTerm = computeAngleTerm(angle);
+  double res = d_forceConstant * angleTerm * angleTerm;
 
   return res;
 }
@@ -131,39 +114,38 @@ void AngleConstraintContrib::getGrad(double *pos, double *grad) const {
   PRECONDITION(pos, "bad vector");
   PRECONDITION(grad, "bad vector");
 
-  double dist[2] = {dp_forceField->distance(d_at1Idx, d_at2Idx, pos),
-                    dp_forceField->distance(d_at2Idx, d_at3Idx, pos)};
-
   RDGeom::Point3D p1(pos[3 * d_at1Idx], pos[3 * d_at1Idx + 1],
                      pos[3 * d_at1Idx + 2]);
   RDGeom::Point3D p2(pos[3 * d_at2Idx], pos[3 * d_at2Idx + 1],
                      pos[3 * d_at2Idx + 2]);
   RDGeom::Point3D p3(pos[3 * d_at3Idx], pos[3 * d_at3Idx + 1],
                      pos[3 * d_at3Idx + 2]);
+
   double *g[3] = {&(grad[3 * d_at1Idx]), &(grad[3 * d_at2Idx]),
                   &(grad[3 * d_at3Idx])};
-  RDGeom::Point3D r[2] = {(p1 - p2) / dist[0], (p3 - p2) / dist[1]};
-  double cosTheta = r[0].dotProduct(r[1]);
+  RDGeom::Point3D r[2] = {p1 - p2, p3 - p2};
+  double rLengthSq[2] = {(std::max)(1.0e-5, r[0].lengthSq()),
+                         (std::max)(1.0e-5, r[1].lengthSq())};
+  double cosTheta = r[0].dotProduct(r[1]) / sqrt(rLengthSq[0] * rLengthSq[1]);
   clipToOne(cosTheta);
-  double sinThetaSq = 1.0 - cosTheta * cosTheta;
-  double sinTheta =
-      std::max(((sinThetaSq > 0.0) ? sqrt(sinThetaSq) : 0.0), 1.0e-8);
 
-  // use the chain rule:
-  // dE/dx = dE/dTheta * dTheta/dx
-
-  // dE/dTheta is independent of cartesians:
   double angle = RAD2DEG * acos(cosTheta);
-  double angleTerm = 0.0;
-  if (angle < d_minAngleDeg) {
-    angleTerm = angle - d_minAngleDeg;
-  } else if (angle > d_maxAngleDeg) {
-    angleTerm = angle - d_maxAngleDeg;
+  double angleTerm = computeAngleTerm(angle);
+
+  double dE_dTheta = 2.0 * RAD2DEG * d_forceConstant * angleTerm;
+
+  RDGeom::Point3D rp = r[1].crossProduct(r[0]);
+  double prefactor = dE_dTheta / (std::max)(1.0e-5, rp.length());
+  double t[2] = {-prefactor / rLengthSq[0], prefactor / rLengthSq[1]};
+  RDGeom::Point3D dedp[3];
+  dedp[0] = r[0].crossProduct(rp) * t[0];
+  dedp[2] = r[1].crossProduct(rp) * t[1];
+  dedp[1] = -dedp[0] - dedp[2];
+  for (unsigned int i = 0; i < 3; ++i) {
+    g[i][0] += dedp[i].x;
+    g[i][1] += dedp[i].y;
+    g[i][2] += dedp[i].z;
   }
-
-  double dE_dTheta = DEG2RAD * d_forceConstant * angleTerm;
-
-  Utils::calcAngleBendGrad(r, dist, g, dE_dTheta, cosTheta, sinTheta);
-}
-}
 }
+}  // namespace MMFF
+}  // namespace ForceFields
diff --git a/Code/ForceField/MMFF/AngleConstraint.h b/Code/ForceField/MMFF/AngleConstraint.h
index e0e6ebd1bdb..48ff353f0a3 100644
--- a/Code/ForceField/MMFF/AngleConstraint.h
+++ b/Code/ForceField/MMFF/AngleConstraint.h
@@ -53,6 +53,7 @@ class RDKIT_FORCEFIELD_EXPORT AngleConstraintContrib
   };
 
  private:
+  double computeAngleTerm(double angle) const;
   int d_at1Idx{-1}, d_at2Idx{-1},
       d_at3Idx{-1};                     //!< indices of atoms forming the angle
   double d_minAngleDeg, d_maxAngleDeg;  //!< rest amplitudes of the angle
diff --git a/Code/ForceField/MMFF/TorsionConstraint.cpp b/Code/ForceField/MMFF/TorsionConstraint.cpp
index 3e87a22059a..d3a391cac76 100644
--- a/Code/ForceField/MMFF/TorsionConstraint.cpp
+++ b/Code/ForceField/MMFF/TorsionConstraint.cpp
@@ -10,7 +10,6 @@
 //  which is included in the file license.txt, found at the root
 //  of the RDKit source tree.
 //
-#include "TorsionAngle.h"
 #include "TorsionConstraint.h"
 #include "Params.h"
 #include <cmath>
@@ -25,8 +24,12 @@
 namespace ForceFields {
 namespace MMFF {
 inline void checkPrecondition(const ForceField *owner, unsigned int idx1,
-    unsigned int idx2, unsigned int idx3, unsigned int idx4) {
+                              unsigned int idx2, unsigned int idx3,
+                              unsigned int idx4, double minDihedralDeg,
+                              double maxDihedralDeg) {
   PRECONDITION(owner, "bad owner");
+  PRECONDITION(!(minDihedralDeg > maxDihedralDeg),
+               "minDihedralDeg must be <= maxDihedralDeg");
   URANGE_CHECK(idx1, owner->positions().size());
   URANGE_CHECK(idx2, owner->positions().size());
   URANGE_CHECK(idx3, owner->positions().size());
@@ -35,29 +38,35 @@ inline void checkPrecondition(const ForceField *owner, unsigned int idx1,
 
 double TorsionConstraintContrib::computeDihedralTerm(double dihedral) const {
   double dihedralTarget = dihedral;
-  if (!(dihedral > d_minDihedralDeg && dihedral < d_maxDihedralDeg)
-      && !(dihedral > d_minDihedralDeg && d_minDihedralDeg > d_maxDihedralDeg)
-      && !(dihedral < d_maxDihedralDeg && d_minDihedralDeg > d_maxDihedralDeg)) {
-    double minDihedralTarget = dihedral - d_minDihedralDeg;
-    double maxDihedralTarget = dihedral - d_maxDihedralDeg;
-    RDKit::ForceFieldsHelper::normalizeAngleDeg(minDihedralTarget);
-    RDKit::ForceFieldsHelper::normalizeAngleDeg(maxDihedralTarget);
-    dihedralTarget = (fabs(minDihedralTarget) < fabs(maxDihedralTarget)
-      ? d_minDihedralDeg : d_maxDihedralDeg);
+  if (!(dihedral > d_minDihedralDeg && dihedral < d_maxDihedralDeg) &&
+      !(dihedral > d_minDihedralDeg && d_minDihedralDeg > d_maxDihedralDeg) &&
+      !(dihedral < d_maxDihedralDeg && d_minDihedralDeg > d_maxDihedralDeg)) {
+    double dihedralMinTarget = dihedral - d_minDihedralDeg;
+    RDKit::ForceFieldsHelper::normalizeAngleDeg(dihedralMinTarget);
+    double dihedralMaxTarget = dihedral - d_maxDihedralDeg;
+    RDKit::ForceFieldsHelper::normalizeAngleDeg(dihedralMaxTarget);
+    if (fabs(dihedralMinTarget) < fabs(dihedralMaxTarget)) {
+      dihedralTarget = d_minDihedralDeg;
+    } else {
+      dihedralTarget = d_maxDihedralDeg;
+    }
   }
   double dihedralTerm = dihedral - dihedralTarget;
   RDKit::ForceFieldsHelper::normalizeAngleDeg(dihedralTerm);
   return dihedralTerm;
 }
 
-void TorsionConstraintContrib::setParameters(ForceField *owner, unsigned int idx1,
-    unsigned int idx2, unsigned int idx3, unsigned int idx4, double minDihedralDeg,
-    double maxDihedralDeg, double forceConst) {
+void TorsionConstraintContrib::setParameters(
+    ForceField *owner, unsigned int idx1, unsigned int idx2, unsigned int idx3,
+    unsigned int idx4, double minDihedralDeg, double maxDihedralDeg,
+    double forceConst) {
   dp_forceField = owner;
   d_at1Idx = idx1;
   d_at2Idx = idx2;
   d_at3Idx = idx3;
   d_at4Idx = idx4;
+  RDKit::ForceFieldsHelper::normalizeAngleDeg(minDihedralDeg);
+  RDKit::ForceFieldsHelper::normalizeAngleDeg(maxDihedralDeg);
   d_minDihedralDeg = minDihedralDeg;
   d_maxDihedralDeg = maxDihedralDeg;
   d_forceConstant = forceConst;
@@ -67,44 +76,39 @@ TorsionConstraintContrib::TorsionConstraintContrib(
     ForceField *owner, unsigned int idx1, unsigned int idx2, unsigned int idx3,
     unsigned int idx4, double minDihedralDeg, double maxDihedralDeg,
     double forceConst) {
-  checkPrecondition(owner, idx1, idx2, idx3, idx4);
-  RDKit::ForceFieldsHelper::normalizeAngleDeg(minDihedralDeg);
-  RDKit::ForceFieldsHelper::normalizeAngleDeg(maxDihedralDeg);
-  setParameters(owner, idx1, idx2, idx3, idx4,
-    minDihedralDeg, maxDihedralDeg, forceConst);
+  checkPrecondition(owner, idx1, idx2, idx3, idx4, minDihedralDeg,
+                    maxDihedralDeg);
+  setParameters(owner, idx1, idx2, idx3, idx4, minDihedralDeg, maxDihedralDeg,
+                forceConst);
 }
 
 TorsionConstraintContrib::TorsionConstraintContrib(
     ForceField *owner, unsigned int idx1, unsigned int idx2, unsigned int idx3,
     unsigned int idx4, bool relative, double minDihedralDeg,
     double maxDihedralDeg, double forceConst) {
-  checkPrecondition(owner, idx1, idx2, idx3, idx4);
-  RDKit::ForceFieldsHelper::normalizeAngleDeg(minDihedralDeg);
-  RDKit::ForceFieldsHelper::normalizeAngleDeg(maxDihedralDeg);
+  checkPrecondition(owner, idx1, idx2, idx3, idx4, minDihedralDeg,
+                    maxDihedralDeg);
   if (relative) {
     double dihedral;
-    RDKit::ForceFieldsHelper::computeDihedral(
-        owner->positions(), idx1, idx2, idx3, idx4, &dihedral);
+    RDKit::ForceFieldsHelper::computeDihedral(owner->positions(), idx1, idx2,
+                                              idx3, idx4, &dihedral);
     dihedral *= RAD2DEG;
     minDihedralDeg += dihedral;
     maxDihedralDeg += dihedral;
-    RDKit::ForceFieldsHelper::normalizeAngleDeg(minDihedralDeg);
-    RDKit::ForceFieldsHelper::normalizeAngleDeg(maxDihedralDeg);
   }
-  setParameters(owner, idx1, idx2, idx3, idx4,
-    minDihedralDeg, maxDihedralDeg, forceConst);
+  setParameters(owner, idx1, idx2, idx3, idx4, minDihedralDeg, maxDihedralDeg,
+                forceConst);
 }
 
 double TorsionConstraintContrib::getEnergy(double *pos) const {
   PRECONDITION(dp_forceField, "no owner");
   PRECONDITION(pos, "bad vector");
   double dihedral;
-  RDKit::ForceFieldsHelper::computeDihedral(
-    pos, d_at1Idx, d_at2Idx, d_at3Idx, d_at4Idx, &dihedral);
+  RDKit::ForceFieldsHelper::computeDihedral(pos, d_at1Idx, d_at2Idx, d_at3Idx,
+                                            d_at4Idx, &dihedral);
   dihedral *= RAD2DEG;
   double dihedralTerm = computeDihedralTerm(dihedral);
-  static double const c = 0.5 * DEG2RAD * DEG2RAD;
-  double res = c * d_forceConstant * dihedralTerm * dihedralTerm;
+  double res = d_forceConstant * dihedralTerm * dihedralTerm;
 
   return res;
 }
@@ -120,23 +124,35 @@ void TorsionConstraintContrib::getGrad(double *pos, double *grad) const {
   RDGeom::Point3D r[4];
   RDGeom::Point3D t[2];
   double d[2];
-  double cosPhi;
   double dihedral;
   RDKit::ForceFieldsHelper::computeDihedral(
-    pos, d_at1Idx, d_at2Idx, d_at3Idx, d_at4Idx, &dihedral, &cosPhi, r, t, d);
+      pos, d_at1Idx, d_at2Idx, d_at3Idx, d_at4Idx, &dihedral, nullptr, r, t, d);
   dihedral *= RAD2DEG;
-  double sinPhiSq = 1.0 - cosPhi * cosPhi;
-  double sinPhi = ((sinPhiSq > 0.0) ? sqrt(sinPhiSq) : 0.0);
-  // dE/dPhi is independent of cartesians:
   double dihedralTerm = computeDihedralTerm(dihedral);
-  double dE_dPhi = DEG2RAD * d_forceConstant * dihedralTerm;
+  double dE_dPhi = 2.0 * RAD2DEG * d_forceConstant * dihedralTerm;
 
-  // FIX: use a tolerance here
-  // this is hacky, but it's per the
-  // recommendation from Niketic and Rasmussen:
-  double sinTerm =
-      -dE_dPhi * (isDoubleZero(sinPhi) ? (1.0 / cosPhi) : (1.0 / sinPhi));
-  Utils::calcTorsionGrad(r, t, d, g, sinTerm, cosPhi);
-}
-}
+  double d23 = dp_forceField->distance(d_at2Idx, d_at3Idx, pos);
+  RDGeom::Point3D r31(pos[3 * d_at3Idx] - pos[3 * d_at1Idx],
+                      pos[3 * d_at3Idx + 1] - pos[3 * d_at1Idx + 1],
+                      pos[3 * d_at3Idx + 2] - pos[3 * d_at1Idx + 2]);
+  RDGeom::Point3D r42(pos[3 * d_at4Idx] - pos[3 * d_at2Idx],
+                      pos[3 * d_at4Idx + 1] - pos[3 * d_at2Idx + 1],
+                      pos[3 * d_at4Idx + 2] - pos[3 * d_at2Idx + 2]);
+  double prefactor = dE_dPhi / d23;
+  RDGeom::Point3D tt[2] = {r[0].crossProduct(r[1]), r[2].crossProduct(r[3])};
+  RDGeom::Point3D dedt[2] = {
+      tt[0].crossProduct(r[2]) / tt[0].lengthSq() * prefactor,
+      tt[1].crossProduct(r[1]) / tt[1].lengthSq() * prefactor};
+  RDGeom::Point3D dedp[4] = {
+      r[2].crossProduct(dedt[0]),
+      r31.crossProduct(dedt[0]) - r[3].crossProduct(dedt[1]),
+      r[0].crossProduct(dedt[0]) + r42.crossProduct(dedt[1]),
+      r[2].crossProduct(dedt[1])};
+  for (unsigned int i = 0; i < 4; ++i) {
+    g[i][0] += dedp[i].x;
+    g[i][1] += dedp[i].y;
+    g[i][2] += dedp[i].z;
+  }
 }
+}  // namespace MMFF
+}  // namespace ForceFields
diff --git a/Code/ForceField/UFF/AngleConstraint.cpp b/Code/ForceField/UFF/AngleConstraint.cpp
index 37111b8aa5b..7c69d20b92c 100644
--- a/Code/ForceField/UFF/AngleConstraint.cpp
+++ b/Code/ForceField/UFF/AngleConstraint.cpp
@@ -10,7 +10,6 @@
 //  which is included in the file license.txt, found at the root
 //  of the RDKit source tree.
 //
-#include "AngleBend.h"
 #include "AngleConstraint.h"
 #include "Params.h"
 #include <cmath>
@@ -19,28 +18,6 @@
 
 namespace ForceFields {
 namespace UFF {
-void _pretreatAngles(double &minAngleDeg, double &maxAngleDeg) {
-  minAngleDeg = fmod(minAngleDeg, 360.0);
-  maxAngleDeg = fmod(maxAngleDeg, 360.0);
-  if (minAngleDeg > 180.0) {
-    minAngleDeg -= 360.0;
-  }
-  if (maxAngleDeg > 180.0) {
-    maxAngleDeg -= 360.0;
-  }
-  if ((minAngleDeg < 0.0) && (!(maxAngleDeg < 0.0))) {
-    maxAngleDeg = std::max(fabs(maxAngleDeg), fabs(minAngleDeg));
-    minAngleDeg = 0.0;
-  }
-  minAngleDeg = fabs(minAngleDeg);
-  maxAngleDeg = fabs(maxAngleDeg);
-  if (minAngleDeg > maxAngleDeg) {
-    double t = minAngleDeg;
-    minAngleDeg = maxAngleDeg;
-    maxAngleDeg = t;
-  }
-}
-
 AngleConstraintContrib::AngleConstraintContrib(
     ForceField *owner, unsigned int idx1, unsigned int idx2, unsigned int idx3,
     double minAngleDeg, double maxAngleDeg, double forceConst) {
@@ -48,8 +25,10 @@ AngleConstraintContrib::AngleConstraintContrib(
   URANGE_CHECK(idx1, owner->positions().size());
   URANGE_CHECK(idx2, owner->positions().size());
   URANGE_CHECK(idx3, owner->positions().size());
-  PRECONDITION(maxAngleDeg >= minAngleDeg, "allowedDeltaDeg must be >= 0.0");
-  _pretreatAngles(minAngleDeg, maxAngleDeg);
+  PRECONDITION(!(minAngleDeg > maxAngleDeg),
+               "minAngleDeg must be <= maxAngleDeg");
+  RDKit::ForceFieldsHelper::normalizeAngleDeg(minAngleDeg);
+  RDKit::ForceFieldsHelper::normalizeAngleDeg(maxAngleDeg);
 
   dp_forceField = owner;
   d_at1Idx = idx1;
@@ -68,7 +47,8 @@ AngleConstraintContrib::AngleConstraintContrib(
   URANGE_CHECK(idx1, pos.size());
   URANGE_CHECK(idx2, pos.size());
   URANGE_CHECK(idx3, pos.size());
-  PRECONDITION(maxAngleDeg >= minAngleDeg, "allowedDeltaDeg must be >= 0.0");
+  PRECONDITION(!(minAngleDeg > maxAngleDeg),
+               "minAngleDeg must be <= maxAngleDeg");
 
   double angle = 0.0;
   if (relative) {
@@ -89,19 +69,27 @@ AngleConstraintContrib::AngleConstraintContrib(
   d_at3Idx = idx3;
   minAngleDeg += angle;
   maxAngleDeg += angle;
-  _pretreatAngles(minAngleDeg, maxAngleDeg);
+  RDKit::ForceFieldsHelper::normalizeAngleDeg(minAngleDeg);
+  RDKit::ForceFieldsHelper::normalizeAngleDeg(maxAngleDeg);
   d_minAngleDeg = minAngleDeg;
   d_maxAngleDeg = maxAngleDeg;
   d_forceConstant = forceConst;
 }
 
+double AngleConstraintContrib::computeAngleTerm(double angle) const {
+  double angleTerm = 0.0;
+  if (angle < d_minAngleDeg) {
+    angleTerm = angle - d_minAngleDeg;
+  } else if (angle > d_maxAngleDeg) {
+    angleTerm = angle - d_maxAngleDeg;
+  }
+  return angleTerm;
+}
+
 double AngleConstraintContrib::getEnergy(double *pos) const {
   PRECONDITION(dp_forceField, "no owner");
   PRECONDITION(pos, "bad vector");
 
-  double dist1 = dp_forceField->distance(d_at1Idx, d_at2Idx, pos);
-  double dist2 = dp_forceField->distance(d_at2Idx, d_at3Idx, pos);
-
   RDGeom::Point3D p1(pos[3 * d_at1Idx], pos[3 * d_at1Idx + 1],
                      pos[3 * d_at1Idx + 2]);
   RDGeom::Point3D p2(pos[3 * d_at2Idx], pos[3 * d_at2Idx + 1],
@@ -109,19 +97,14 @@ double AngleConstraintContrib::getEnergy(double *pos) const {
   RDGeom::Point3D p3(pos[3 * d_at3Idx], pos[3 * d_at3Idx + 1],
                      pos[3 * d_at3Idx + 2]);
 
-  RDGeom::Point3D p12 = (p1 - p2) / dist1;
-  RDGeom::Point3D p32 = (p3 - p2) / dist2;
-  double cosTheta = p12.dotProduct(p32);
+  RDGeom::Point3D r[2] = {p1 - p2, p3 - p2};
+  double rLengthSq[2] = {(std::max)(1.0e-5, r[0].lengthSq()),
+                         (std::max)(1.0e-5, r[1].lengthSq())};
+  double cosTheta = r[0].dotProduct(r[1]) / sqrt(rLengthSq[0] * rLengthSq[1]);
   clipToOne(cosTheta);
   double angle = RAD2DEG * acos(cosTheta);
-  double angleTerm = 0.0;
-  if (angle < d_minAngleDeg) {
-    angleTerm = angle - d_minAngleDeg;
-  } else if (angle > d_maxAngleDeg) {
-    angleTerm = angle - d_maxAngleDeg;
-  }
-  double const c = 0.5 * DEG2RAD * DEG2RAD;
-  double res = c * d_forceConstant * angleTerm * angleTerm;
+  double angleTerm = computeAngleTerm(angle);
+  double res = d_forceConstant * angleTerm * angleTerm;
 
   return res;
 }
@@ -131,39 +114,38 @@ void AngleConstraintContrib::getGrad(double *pos, double *grad) const {
   PRECONDITION(pos, "bad vector");
   PRECONDITION(grad, "bad vector");
 
-  double dist[2] = {dp_forceField->distance(d_at1Idx, d_at2Idx, pos),
-                    dp_forceField->distance(d_at2Idx, d_at3Idx, pos)};
-
   RDGeom::Point3D p1(pos[3 * d_at1Idx], pos[3 * d_at1Idx + 1],
                      pos[3 * d_at1Idx + 2]);
   RDGeom::Point3D p2(pos[3 * d_at2Idx], pos[3 * d_at2Idx + 1],
                      pos[3 * d_at2Idx + 2]);
   RDGeom::Point3D p3(pos[3 * d_at3Idx], pos[3 * d_at3Idx + 1],
                      pos[3 * d_at3Idx + 2]);
+
   double *g[3] = {&(grad[3 * d_at1Idx]), &(grad[3 * d_at2Idx]),
                   &(grad[3 * d_at3Idx])};
-  RDGeom::Point3D r[2] = {(p1 - p2) / dist[0], (p3 - p2) / dist[1]};
-  double cosTheta = r[0].dotProduct(r[1]);
+  RDGeom::Point3D r[2] = {p1 - p2, p3 - p2};
+  double rLengthSq[2] = {(std::max)(1.0e-5, r[0].lengthSq()),
+                         (std::max)(1.0e-5, r[1].lengthSq())};
+  double cosTheta = r[0].dotProduct(r[1]) / sqrt(rLengthSq[0] * rLengthSq[1]);
   clipToOne(cosTheta);
-  double sinThetaSq = 1.0 - cosTheta * cosTheta;
-  double sinTheta =
-      std::max(((sinThetaSq > 0.0) ? sqrt(sinThetaSq) : 0.0), 1.0e-8);
 
-  // use the chain rule:
-  // dE/dx = dE/dTheta * dTheta/dx
-
-  // dE/dTheta is independent of cartesians:
   double angle = RAD2DEG * acos(cosTheta);
-  double angleTerm = 0.0;
-  if (angle < d_minAngleDeg) {
-    angleTerm = angle - d_minAngleDeg;
-  } else if (angle > d_maxAngleDeg) {
-    angleTerm = angle - d_maxAngleDeg;
+  double angleTerm = computeAngleTerm(angle);
+
+  double dE_dTheta = 2.0 * RAD2DEG * d_forceConstant * angleTerm;
+
+  RDGeom::Point3D rp = r[1].crossProduct(r[0]);
+  double prefactor = dE_dTheta / (std::max)(1.0e-5, rp.length());
+  double t[2] = {-prefactor / rLengthSq[0], prefactor / rLengthSq[1]};
+  RDGeom::Point3D dedp[3];
+  dedp[0] = r[0].crossProduct(rp) * t[0];
+  dedp[2] = r[1].crossProduct(rp) * t[1];
+  dedp[1] = -dedp[0] - dedp[2];
+  for (unsigned int i = 0; i < 3; ++i) {
+    g[i][0] += dedp[i].x;
+    g[i][1] += dedp[i].y;
+    g[i][2] += dedp[i].z;
   }
-
-  double dE_dTheta = DEG2RAD * d_forceConstant * angleTerm;
-
-  Utils::calcAngleBendGrad(r, dist, g, dE_dTheta, cosTheta, sinTheta);
-}
-}
 }
+}  // namespace UFF
+}  // namespace ForceFields
diff --git a/Code/ForceField/UFF/AngleConstraint.h b/Code/ForceField/UFF/AngleConstraint.h
index 66bc58f1cec..e9930a8b991 100644
--- a/Code/ForceField/UFF/AngleConstraint.h
+++ b/Code/ForceField/UFF/AngleConstraint.h
@@ -22,7 +22,7 @@ namespace UFF {
 class RDKIT_FORCEFIELD_EXPORT AngleConstraintContrib
     : public ForceFieldContrib {
  public:
-  AngleConstraintContrib()  {};
+  AngleConstraintContrib(){};
   //! Constructor
   /*!
   \param owner       pointer to the owning ForceField
@@ -47,12 +47,15 @@ class RDKIT_FORCEFIELD_EXPORT AngleConstraintContrib
   double getEnergy(double *pos) const;
 
   void getGrad(double *pos, double *grad) const;
+
   virtual AngleConstraintContrib *copy() const {
     return new AngleConstraintContrib(*this);
   };
 
  private:
-  int d_at1Idx{-1}, d_at2Idx{-1}, d_at3Idx{-1};     //!< indices of atoms forming the angle
+  double computeAngleTerm(double angle) const;
+  int d_at1Idx{-1}, d_at2Idx{-1},
+      d_at3Idx{-1};                     //!< indices of atoms forming the angle
   double d_minAngleDeg, d_maxAngleDeg;  //!< rest amplitudes of the angle
   double d_forceConstant;  //!< force constant of the angle constraint
 };
diff --git a/Code/ForceField/UFF/TorsionConstraint.cpp b/Code/ForceField/UFF/TorsionConstraint.cpp
index 5215612b8b7..cef3c0fabdd 100644
--- a/Code/ForceField/UFF/TorsionConstraint.cpp
+++ b/Code/ForceField/UFF/TorsionConstraint.cpp
@@ -10,7 +10,6 @@
 //  which is included in the file license.txt, found at the root
 //  of the RDKit source tree.
 //
-#include "TorsionAngle.h"
 #include "TorsionConstraint.h"
 #include "Params.h"
 #include <cmath>
@@ -25,8 +24,12 @@
 namespace ForceFields {
 namespace UFF {
 inline void checkPrecondition(const ForceField *owner, unsigned int idx1,
-    unsigned int idx2, unsigned int idx3, unsigned int idx4) {
+                              unsigned int idx2, unsigned int idx3,
+                              unsigned int idx4, double minDihedralDeg,
+                              double maxDihedralDeg) {
   PRECONDITION(owner, "bad owner");
+  PRECONDITION(!(minDihedralDeg > maxDihedralDeg),
+               "minDihedralDeg must be <= maxDihedralDeg");
   URANGE_CHECK(idx1, owner->positions().size());
   URANGE_CHECK(idx2, owner->positions().size());
   URANGE_CHECK(idx3, owner->positions().size());
@@ -35,29 +38,35 @@ inline void checkPrecondition(const ForceField *owner, unsigned int idx1,
 
 double TorsionConstraintContrib::computeDihedralTerm(double dihedral) const {
   double dihedralTarget = dihedral;
-  if (!(dihedral > d_minDihedralDeg && dihedral < d_maxDihedralDeg)
-      && !(dihedral > d_minDihedralDeg && d_minDihedralDeg > d_maxDihedralDeg)
-      && !(dihedral < d_maxDihedralDeg && d_minDihedralDeg > d_maxDihedralDeg)) {
-    double minDihedralTarget = dihedral - d_minDihedralDeg;
-    double maxDihedralTarget = dihedral - d_maxDihedralDeg;
-    RDKit::ForceFieldsHelper::normalizeAngleDeg(minDihedralTarget);
-    RDKit::ForceFieldsHelper::normalizeAngleDeg(maxDihedralTarget);
-    dihedralTarget = (fabs(minDihedralTarget) < fabs(maxDihedralTarget)
-      ? d_minDihedralDeg : d_maxDihedralDeg);
+  if (!(dihedral > d_minDihedralDeg && dihedral < d_maxDihedralDeg) &&
+      !(dihedral > d_minDihedralDeg && d_minDihedralDeg > d_maxDihedralDeg) &&
+      !(dihedral < d_maxDihedralDeg && d_minDihedralDeg > d_maxDihedralDeg)) {
+    double dihedralMinTarget = dihedral - d_minDihedralDeg;
+    RDKit::ForceFieldsHelper::normalizeAngleDeg(dihedralMinTarget);
+    double dihedralMaxTarget = dihedral - d_maxDihedralDeg;
+    RDKit::ForceFieldsHelper::normalizeAngleDeg(dihedralMaxTarget);
+    if (fabs(dihedralMinTarget) < fabs(dihedralMaxTarget)) {
+      dihedralTarget = d_minDihedralDeg;
+    } else {
+      dihedralTarget = d_maxDihedralDeg;
+    }
   }
   double dihedralTerm = dihedral - dihedralTarget;
   RDKit::ForceFieldsHelper::normalizeAngleDeg(dihedralTerm);
   return dihedralTerm;
 }
 
-void TorsionConstraintContrib::setParameters(ForceField *owner, unsigned int idx1,
-    unsigned int idx2, unsigned int idx3, unsigned int idx4, double minDihedralDeg,
-    double maxDihedralDeg, double forceConst) {
+void TorsionConstraintContrib::setParameters(
+    ForceField *owner, unsigned int idx1, unsigned int idx2, unsigned int idx3,
+    unsigned int idx4, double minDihedralDeg, double maxDihedralDeg,
+    double forceConst) {
   dp_forceField = owner;
   d_at1Idx = idx1;
   d_at2Idx = idx2;
   d_at3Idx = idx3;
   d_at4Idx = idx4;
+  RDKit::ForceFieldsHelper::normalizeAngleDeg(minDihedralDeg);
+  RDKit::ForceFieldsHelper::normalizeAngleDeg(maxDihedralDeg);
   d_minDihedralDeg = minDihedralDeg;
   d_maxDihedralDeg = maxDihedralDeg;
   d_forceConstant = forceConst;
@@ -67,44 +76,39 @@ TorsionConstraintContrib::TorsionConstraintContrib(
     ForceField *owner, unsigned int idx1, unsigned int idx2, unsigned int idx3,
     unsigned int idx4, double minDihedralDeg, double maxDihedralDeg,
     double forceConst) {
-  checkPrecondition(owner, idx1, idx2, idx3, idx4);
-  RDKit::ForceFieldsHelper::normalizeAngleDeg(minDihedralDeg);
-  RDKit::ForceFieldsHelper::normalizeAngleDeg(maxDihedralDeg);
-  setParameters(owner, idx1, idx2, idx3, idx4,
-    minDihedralDeg, maxDihedralDeg, forceConst);
+  checkPrecondition(owner, idx1, idx2, idx3, idx4, minDihedralDeg,
+                    maxDihedralDeg);
+  setParameters(owner, idx1, idx2, idx3, idx4, minDihedralDeg, maxDihedralDeg,
+                forceConst);
 }
 
 TorsionConstraintContrib::TorsionConstraintContrib(
     ForceField *owner, unsigned int idx1, unsigned int idx2, unsigned int idx3,
     unsigned int idx4, bool relative, double minDihedralDeg,
     double maxDihedralDeg, double forceConst) {
-  checkPrecondition(owner, idx1, idx2, idx3, idx4);
-  RDKit::ForceFieldsHelper::normalizeAngleDeg(minDihedralDeg);
-  RDKit::ForceFieldsHelper::normalizeAngleDeg(maxDihedralDeg);
+  checkPrecondition(owner, idx1, idx2, idx3, idx4, minDihedralDeg,
+                    maxDihedralDeg);
   if (relative) {
     double dihedral;
-    RDKit::ForceFieldsHelper::computeDihedral(
-        owner->positions(), idx1, idx2, idx3, idx4, &dihedral);
+    RDKit::ForceFieldsHelper::computeDihedral(owner->positions(), idx1, idx2,
+                                              idx3, idx4, &dihedral);
     dihedral *= RAD2DEG;
     minDihedralDeg += dihedral;
     maxDihedralDeg += dihedral;
-    RDKit::ForceFieldsHelper::normalizeAngleDeg(minDihedralDeg);
-    RDKit::ForceFieldsHelper::normalizeAngleDeg(maxDihedralDeg);
   }
-  setParameters(owner, idx1, idx2, idx3, idx4,
-    minDihedralDeg, maxDihedralDeg, forceConst);
+  setParameters(owner, idx1, idx2, idx3, idx4, minDihedralDeg, maxDihedralDeg,
+                forceConst);
 }
 
 double TorsionConstraintContrib::getEnergy(double *pos) const {
   PRECONDITION(dp_forceField, "no owner");
   PRECONDITION(pos, "bad vector");
   double dihedral;
-  RDKit::ForceFieldsHelper::computeDihedral(
-    pos, d_at1Idx, d_at2Idx, d_at3Idx, d_at4Idx, &dihedral);
+  RDKit::ForceFieldsHelper::computeDihedral(pos, d_at1Idx, d_at2Idx, d_at3Idx,
+                                            d_at4Idx, &dihedral);
   dihedral *= RAD2DEG;
   double dihedralTerm = computeDihedralTerm(dihedral);
-  static double const c = 0.5 * DEG2RAD * DEG2RAD;
-  double res = c * d_forceConstant * dihedralTerm * dihedralTerm;
+  double res = d_forceConstant * dihedralTerm * dihedralTerm;
 
   return res;
 }
@@ -120,23 +124,35 @@ void TorsionConstraintContrib::getGrad(double *pos, double *grad) const {
   RDGeom::Point3D r[4];
   RDGeom::Point3D t[2];
   double d[2];
-  double cosPhi;
   double dihedral;
   RDKit::ForceFieldsHelper::computeDihedral(
-    pos, d_at1Idx, d_at2Idx, d_at3Idx, d_at4Idx, &dihedral, &cosPhi, r, t, d);
+      pos, d_at1Idx, d_at2Idx, d_at3Idx, d_at4Idx, &dihedral, nullptr, r, t, d);
   dihedral *= RAD2DEG;
-  double sinPhiSq = 1.0 - cosPhi * cosPhi;
-  double sinPhi = ((sinPhiSq > 0.0) ? sqrt(sinPhiSq) : 0.0);
-  // dE/dPhi is independent of cartesians:
   double dihedralTerm = computeDihedralTerm(dihedral);
-  double dE_dPhi = DEG2RAD * d_forceConstant * dihedralTerm;
+  double dE_dPhi = 2.0 * RAD2DEG * d_forceConstant * dihedralTerm;
 
-  // FIX: use a tolerance here
-  // this is hacky, but it's per the
-  // recommendation from Niketic and Rasmussen:
-  double sinTerm =
-      -dE_dPhi * (isDoubleZero(sinPhi) ? (1.0 / cosPhi) : (1.0 / sinPhi));
-  Utils::calcTorsionGrad(r, t, d, g, sinTerm, cosPhi);
-}
-}
+  double d23 = dp_forceField->distance(d_at2Idx, d_at3Idx, pos);
+  RDGeom::Point3D r31(pos[3 * d_at3Idx] - pos[3 * d_at1Idx],
+                      pos[3 * d_at3Idx + 1] - pos[3 * d_at1Idx + 1],
+                      pos[3 * d_at3Idx + 2] - pos[3 * d_at1Idx + 2]);
+  RDGeom::Point3D r42(pos[3 * d_at4Idx] - pos[3 * d_at2Idx],
+                      pos[3 * d_at4Idx + 1] - pos[3 * d_at2Idx + 1],
+                      pos[3 * d_at4Idx + 2] - pos[3 * d_at2Idx + 2]);
+  double prefactor = dE_dPhi / d23;
+  RDGeom::Point3D tt[2] = {r[0].crossProduct(r[1]), r[2].crossProduct(r[3])};
+  RDGeom::Point3D dedt[2] = {
+      tt[0].crossProduct(r[2]) / tt[0].lengthSq() * prefactor,
+      tt[1].crossProduct(r[1]) / tt[1].lengthSq() * prefactor};
+  RDGeom::Point3D dedp[4] = {
+      r[2].crossProduct(dedt[0]),
+      r31.crossProduct(dedt[0]) - r[3].crossProduct(dedt[1]),
+      r[0].crossProduct(dedt[0]) + r42.crossProduct(dedt[1]),
+      r[2].crossProduct(dedt[1])};
+  for (unsigned int i = 0; i < 4; ++i) {
+    g[i][0] += dedp[i].x;
+    g[i][1] += dedp[i].y;
+    g[i][2] += dedp[i].z;
+  }
 }
+}  // namespace UFF
+}  // namespace ForceFields
diff --git a/Code/ForceField/UFF/TorsionConstraint.h b/Code/ForceField/UFF/TorsionConstraint.h
index 362c5324835..84108b61087 100644
--- a/Code/ForceField/UFF/TorsionConstraint.h
+++ b/Code/ForceField/UFF/TorsionConstraint.h
@@ -22,8 +22,7 @@ namespace UFF {
 class RDKIT_FORCEFIELD_EXPORT TorsionConstraintContrib
     : public ForceFieldContrib {
  public:
-  TorsionConstraintContrib()
-       {};
+  TorsionConstraintContrib(){};
   //! Constructor
   /*!
   \param owner          pointer to the owning ForceField
@@ -55,9 +54,10 @@ class RDKIT_FORCEFIELD_EXPORT TorsionConstraintContrib
   };
 
  private:
-  void setParameters(ForceField *owner, unsigned int idx1,
-    unsigned int idx2, unsigned int idx3, unsigned int idx4,
-    double minDihedralDeg, double maxDihedralDeg, double forceConst);
+  void setParameters(ForceField *owner, unsigned int idx1, unsigned int idx2,
+                     unsigned int idx3, unsigned int idx4,
+                     double minDihedralDeg, double maxDihedralDeg,
+                     double forceConst);
   double computeDihedralTerm(double dihedral) const;
   int d_at1Idx{-1}, d_at2Idx{-1}, d_at3Idx{-1},
       d_at4Idx{-1};  //!< indices of atoms forming the dihedral angle
diff --git a/ReleaseNotes.md b/ReleaseNotes.md
index 4e66dd72e87..ba2e94eccd6 100644
--- a/ReleaseNotes.md
+++ b/ReleaseNotes.md
@@ -34,6 +34,11 @@
   is not canonical, using this argument currently causes the results to depend
   on the input atom numbering. Note that this can lead to an exception being
   thrown. (#3942)
+- Gradients for angle and torsional restraints in both UFF and MMFF were computed
+  incorrectly, which could give rise to potential instability during minimization.
+  As part of fixing this problem, force constants have been switched to using
+  kcal/degree^2 units instead of kcal/rad^2 units, consistently with the fact that
+  angle and dihedral restraints are specified in degrees. (#3975)
 
 ## Highlights
 - MolDraw2D now does a much better job of handling query features like common