By outputting intermediate results, it can be found that it is caused by the loss of precision of float
#include <stdio.h>
#include <math.h>
double task1_double() {
double a = 1000;
double b = 0.0001;
double s1, s2, s3;
s1 = pow((a + b), 2);
s2 = pow(a, 2) + 2 * a * b;
s3 = pow(b, 2);
double result = (pow((a + b), 2) - (pow(a, 2) + 2 * a * b)) / (pow(b, 2));
printf("_double : %lf - %lf = %.15lf\n", s1, s2, s1 - s2);
printf("_double : (%lf - %lf) / %.10lf = %lf\n", s1, s2, s3, result);
return result;
}
float task1_float() {
float a = 1000;
float b = 0.0001f;
float s1, s2, s3;
s1 = powf((a + b), 2);
s2 = powf(a, 2) + 2 * a * b;
s3 = powf(b, 2);
float result = (powf((a + b), 2) - (powf(a, 2) + 2 * a * b)) / (powf(b, 2));
printf("_float : %lf - %lf = %.15lf\n", s1, s2, s1 - s2);
printf("_float : (%lf - %lf) / %.10lf = %lf\n", s1, s2, s3, result);
return result;
}
int main()
{
printf("%.10lf\n%.10lf\n", task1_double(), task1_float());
return 0;
}
Output:
_float : 1000000.250000 - 1000000.187500 = 0.062500000000000
_float : (1000000.250000 - 1000000.187500) / 0.0000000100 = 6250000.500000
_double : 1000000.200000 - 1000000.200000 = 0.000000010011718
_double : (1000000.200000 - 1000000.200000) / 0.0000000100 = 1.001172
1.0011717677
6250000.5000000000
As can be seen:
After the float is operated, a slight error occurs, but dividing by the small value of b will cause the error to be magnified many times