Hello everybody. Faced with colleagues with the following problem. Here is some C code to illustrate it:
In this code, printing the d variable displays 0.620000. However, after storing this variable in a decimal variable (7,4) and printing it to the screen, the number 0.6199 is displayed. If this code is compiled with a C ++ compiler, then the problem is not reproduced. Can you explain why the precision is lost when saving to decimal?
Code:
#include <stdio.h> #include <decimal.h> #include <string.h> #include <xxcvt.h> int main() { unsigned char zoned[7]; memcpy(zoned, "0006200", 7); double d = QXXZTOD(zoned, 7, 4); printf("zoned to double = %f\n", d); decimal(7,4) dec = d; printf("double to decimal = %D(7,4)\n", dec); }
In this code, printing the d variable displays 0.620000. However, after storing this variable in a decimal variable (7,4) and printing it to the screen, the number 0.6199 is displayed. If this code is compiled with a C ++ compiler, then the problem is not reproduced. Can you explain why the precision is lost when saving to decimal?
Comment