OpenSees Cloud

OpenSees AMI

Something Like a Double Negative

Original Post - 03 Nov 2023 - Michael H. Scott

Visit Structural Analysis Is Simple on Substack.


Double negatives are frowned upon in English, but are acceptable in other languages such as Spanish. For example, “No hay nada” is perfectly fine but may sound odd to native English speakers.

Along similar lines, I recently came across some OpenSees code that looked something like this:

const int N = 20;
double Fx[N];

// Put some data in Fx

double F = 0.0;
for (int i = 0; i < N; i++)
   F += -1.0 * Fx[i];

I understand what’s going on here despite some kind of double negative.

First of all, you can negate a variable, even if the value is negative, by simply putting the negative sign in front, i.e., -Fx[i] instead of multiplication -1.0 * Fx[i].

Second, you can use -= to subtract from a variable, i.e., F -= Fx[i] instead of F += -Fx[i].

Perhaps the first negative resulted from unawareness of the second negative because no one would write F -= 1.0 * Fx[i].

Anyway, putting it all together, the code should be:

double F = 0.0;
for (int i = 0; i < N; i++)
   F -= Fx[i];

A good compiler might fix this awkward coding, but never rely on compiler optimizations to clean up your mess. Unlike spoken languages, compilers have a hard time understanding what you really meant to say.