2021-10-14 09:15 PM
Hello.
At the next double type calculation,
double a = 0.7;
char b = 12;
double c;
c = a * b;
c is saved to 8.39999 in the register
But 0.7*12 is 8.4
How can I save 8.4 to the 'c' register?
Thanks.
2021-10-15 02:29 AM
8.4 is not representable by binary floating-point (there's no N so that 8.4*2^N is integer).
Any rational with a denominator that has a prime factor other than 2 will have an infinite binary expansion. This means that numbers that appear to be short and exact when written in decimal format may need to be approximated when converted to binary floating-point.
JW
2021-10-16 09:28 PM
Always be careful when calculating using different variable type format. Cast to make sure.