Skip to content

Commit

Permalink
Check for division by zero in some descriptors.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmay committed Feb 10, 2022
1 parent 5e41fcb commit ca84278
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 3 deletions.
Expand Up @@ -190,6 +190,8 @@ public DescriptorValue calculate(IAtom atom, IAtomContainer ac) {

atomicHardness = 2 * atomicHardness;
atomicHardness = atomicHardness * 0.172;
if (atomicHardness == 0)
return getDummyDescriptorValue(new ArithmeticException("atomicHardness was 0 before division!"));
atomicHardness = 1 / atomicHardness;
return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), new DoubleResult(
atomicHardness), NAMES);
Expand Down
Expand Up @@ -339,8 +339,8 @@ public DescriptorValue calculate(IAtomContainer atomContainer) {
}

// relative descriptors
double rpcg = maxpcharge / totpcharge;
double rncg = maxncharge / totncharge;
double rpcg = totpcharge == 0d ? 0 : maxpcharge / totpcharge;
double rncg = totncharge == 0d ? 0 : maxncharge / totncharge;
double rpcs = atomSurfaces[pidx] * rpcg;
double rncs = atomSurfaces[nidx] * rncg;

Expand Down
Expand Up @@ -128,6 +128,8 @@ public DescriptorValue calculate(IAtomContainer container) {
nsp2++;
else if (atom.getHybridization() == Hybridization.SP3) nsp3++;
}
if (nsp2+nsp3 == 0)
return getDummyDescriptorValue(new ArithmeticException("Cannot divide by 0"));
double ratio = nsp3 / (double) (nsp2 + nsp3);
return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(),
new DoubleResult(ratio), getDescriptorNames());
Expand Down
Expand Up @@ -415,6 +415,8 @@ public DescriptorValue calculate(IAtomContainer container) {
sum = 0.0;
for (int i = 0; i < 3; i++)
sum += lambda[i];
if (sum == 0.0)
return getDummyDescriptorValue(new ArithmeticException("Lambda sum was 0, cannot divide by 0!"));
for (int i = 0; i < 3; i++)
k = (lambda[i] / sum) - (1.0 / 3.0);
k = k / (4.0 / 3.0);
Expand Down
Expand Up @@ -159,13 +159,15 @@ private Point3d getCenterOfMass(Point3d[] p, double[] atwt) {
double x = 0.;
double y = 0.;
double z = 0.;
double totalmass = 0.;
double totalmass = 0d;
for (int i = 0; i < p.length; i++) {
x += atwt[i] * p[i].x;
y += atwt[i] * p[i].y;
z += atwt[i] * p[i].z;
totalmass += atwt[i];
}
if (totalmass == 0d)
return new Point3d(0,0,0);
return (new Point3d(x / totalmass, y / totalmass, z / totalmass));
}

Expand Down

0 comments on commit ca84278

Please sign in to comment.