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?