From: Joe Jordan Date: Wed, 27 Oct 2021 10:15:13 +0000 (+0000) Subject: Fix norm calculation in gmx_spatial X-Git-Url: http://biod.pnpi.spb.ru/gitweb/?p=alexxy%2Fgromacs.git;a=commitdiff_plain;h=5227e867887ba713540bad8a7445509742abcae3 Fix norm calculation in gmx_spatial Casting early to a double should avoid underflow in later multiplication. An assert that the norm is non-negative is added. Fixes #4184 --- diff --git a/src/gromacs/gmxana/gmx_spatial.cpp b/src/gromacs/gmxana/gmx_spatial.cpp index 81ea6d0c77..90eda00f9f 100644 --- a/src/gromacs/gmxana/gmx_spatial.cpp +++ b/src/gromacs/gmxana/gmx_spatial.cpp @@ -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(numcu * numfr) / tot; + norm = double(numcu) * numfr / tot; + GMX_ASSERT(norm >= 0, "The norm should be non-negative."); } else {