1

I'm writing a C++ program and in an attempt to fix a bug I've isolated a section of my main program into a separate file. I'll call the main program Prog1 and the separated section Prog2.

I've managed to successfully find and fix the bug in Prog2 and I've copied the changes over to Prog1. When I run everything the two programs output the same result IFF the only data type I'm using in both programs is type double.

I was hoping to change all doubles to long double for increased precision but in doing so, the output of Prog1 is slightly different than Prog2.

My thinking is that this is an issue of promotion because certain calculations in Prog1 use long doubles whereas in Prog2 I occasionally just use literals. For example:

Prog1:

long double num = 1.0;

value1 = num + 2.0;

Prog2:

value2 = 1.0 + 2.0;

Where the results of value1 and value2 will not be exactly equal (see: Is floating point math broken?).

Now I could just change every literal in both programs to type long double (e.g. 1.0L) then see if the outputs match but unfortunately, that's easier said than done (there are a lot).

So my question is:

Since the output of my two programs exactly matches when using doubles, is it safe to assume Prog1 is functioning correctly if Prog2 is? Even though the results differ when using long doubles?

Community
  • 1
  • 1
Paul Warnick
  • 903
  • 2
  • 13
  • 26
  • No it is not. A promotion can change behavior. I got to track down one of those bugs recently where a promotion from `float` to `double` changed the behavior in an initially incomprehensible way. Once located, I did all math with `float` and the seeming logical inconsistency was resolved. – Dark Falcon Jun 23 '16 at 19:11
  • Does changing literals to 1.0 l and 2.0l help? Off topic: typedefs or templates are useful ;) – Aleksei Guzev Jun 23 '16 at 19:15
  • @AlekseiGuzev I'm actually really new to C++ but almost everyone I talk to mentions those. I'll definitely be looking into them soon, thanks. – Paul Warnick Jun 23 '16 at 19:17
  • Bah. `typedefs` are so 2010. [The cool kids are all using `using` now](http://en.cppreference.com/w/cpp/language/type_alias). – user4581301 Jun 23 '16 at 19:18
  • @DarkFalcon Hmm, would you suggest that I take the hard way out and change all literals in both programs to long doubles (to avoid any promotion whatsoever)? – Paul Warnick Jun 23 '16 at 19:19
  • Well, you could do that until the program output is identical, or you can verify your numbers some other way. Since I don't know the problem domain, the best I can suggest is avoiding the promotion. – Dark Falcon Jun 23 '16 at 19:23
  • @DarkFalcon Makes sense. But actually, what about the identical outputs using just doubles (i.e. no promotion). Is it then safe to assume the original program is now correct? – Paul Warnick Jun 23 '16 at 19:26
  • I am far older than 2010 that is why I prefer templates ;) – Aleksei Guzev Jun 23 '16 at 19:26
  • 1
    See http://stackoverflow.com/questions/37977151/what-happens-to-the-value-of-a-floating-point-number-when-its-assigned-to-a-lon/37977225#37977225 for why you get different results in the two versions. – Barmar Jun 23 '16 at 19:28
  • @Barmar Trust me, I've seen it (it's my question). – Paul Warnick Jun 23 '16 at 19:29

0 Answers0