I use the function trace() to calculate the trace of a matrix. This is not important. But for this specific matrix this value is -1. My variable "trace" is set to this from the function. However when I compare this value as a boolean it shows they are not equal. Why is this happening?


double trace = R.trace();
double t = -1;
std::cout << "\nTrace: \n" << trace << "\n Matrix: \n" << R;
printf("\nbool: %d: sub: %f type: %s\n", trace == -1.0, trace, typeid(trace).name());
printf("\nbool: %d: sub: %f type: %s\n", t == -1.0, t, typeid(t).name());


           1  3.52206e-06 -2.42913e-11
 3.52206e-06           -1  6.44708e-06
-1.58434e-12 -6.44708e-06           -1
bool: 0: sub: -1.000000 type: d

bool: 1: sub: -1.000000 type: d

So for some reason it does not realize they are equal?

I am using g++ (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0 x86_64


1 Answer 1


for this specific matrix this value is -1.

Ahhh, but it isn't!

It displays as negative one.

Low order bits of the significand are being suppressed from the display. Add +1 to it and print that sum if you want to see them.

Alternatively, use %a sprintf formatting to see the hex representation. It reveals all bits.

Given a pair of FP numbers a, b, it is generally frowned on to ask whether a == b.

You're usually better off defining a tiny epsilon and then asking if abs(a - b) < eps.

In this code, rather than passing this expression to printf(): trace == -1.0,
prefer to pass abs(trace - (-1)) (so you're adding +1). Then you can compare that quantity to a small epsilon.

  • \$\begingroup\$ Wow, I thought printing the %f would of been enough to show the lower order bits but I guess not. Ok thanks. \$\endgroup\$ Commented Oct 31, 2023 at 2:40
  • \$\begingroup\$ This is code review, not stack overflow. \$\endgroup\$
    – pacmaninbw
    Commented Oct 31, 2023 at 11:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.