Fix norm calculation in gmx_spatial
authorJoe Jordan <ejjordan12@gmail.com>
Wed, 27 Oct 2021 10:15:13 +0000 (10:15 +0000)
committerAndrey Alekseenko <al42and@gmail.com>
Wed, 27 Oct 2021 10:15:13 +0000 (10:15 +0000)
Casting early to a double should avoid underflow in later multiplication.
An assert that the norm is non-negative is added.

Fixes #4184

src/gromacs/gmxana/gmx_spatial.cpp

index 81ea6d0c771e69ab82f7ac83cb57570e5f56c4f4..90eda00f9f476496105fd0c7f04c78c8f5097516 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2007,2008,2009,2010,2011 by the GROMACS development team.
  * Copyright (c) 2012,2013,2014,2015,2017 by the GROMACS development team.
- * Copyright (c) 2019,2020, by the GROMACS development team, led by
+ * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -52,6 +52,7 @@
 #include "gromacs/utility/arraysize.h"
 #include "gromacs/utility/cstringutil.h"
 #include "gromacs/utility/futil.h"
+#include "gromacs/utility/gmxassert.h"
 #include "gromacs/utility/smalloc.h"
 
 static const double bohr =
@@ -456,7 +457,8 @@ int gmx_spatial(int argc, char* argv[])
             * (maxz - minz + 1 - (2 * iIGNOREOUTER));
     if (bCALCDIV)
     {
-        norm = static_cast<double>(numcu * numfr) / tot;
+        norm = double(numcu) * numfr / tot;
+        GMX_ASSERT(norm >= 0, "The norm should be non-negative.");
     }
     else
     {