In this question the OP tried to isolate an elusive bug in a big program. He observed some double
values and then tried to hard code them as input to a smaller test program. The ideea is great for isolating such a bug.
The method chosen however violates the String Aliasing rule, introducing UB, thus the test is completely unreliable:
watch in debugger the expression:
+------------------------+----------------------| | Watch Expression | Value | +------------------------+----------------------| | *(long long int *)(&a) | 4594681439063077250 | +------------------------+----------------------|
and then assign it in the test program:
double a; *(long long int *)(&a) = 4594681439063077250;
(I strongly suspect) He did this in order to preserve the exact double
value, bit by bit.
The question: How to assign to a double the exact value - bit by bit - observed in a debug session?
This can be accomplished by representing the double as an array of
char
:My interpretation of the Strict Aliasing rule, says this would be UB
your code is fine.
Hexadecimal representation is a good way to assign the exact bit representation of a floating point value. If you cannot use literals because you are not yet on C++ 17, note that strtod recognizes this format.