Clone this source code at https://github.com/HuidaeCho/fperror and try it.
#include <stdio.h>
#include <math.h>
int main()
{
float init = 7 * 30;
float x = init, dx = 30 * sqrt(2);
float y = init;
float z = init;
int i;
printf("init=%.10f\n", init);
printf("x=%.10f\n", x);
printf("dx=30*sqrt(2)=%.10f\n", dx);
printf("y=%.10f\n", y);
printf("z=%.10f\n", z);
printf("\n");
printf("%2s %14s %14s %19s %13s %13s\n", "i", "x+=dx", "y+=30*sqrt(2)",
"z=init+i*30*sqrt(2)", "x-y", "x-z");
for (i = 1; i <= 10; i++) {
x += dx;
y += 30 * sqrt(2);
z = init + i * 30 * sqrt(2);
printf("%2d %14.10f %14.10f %14.10f %13.10f %13.10f\n", i, x, y,
z, x - y, x - z);
}
}
$ cc -o fperror_float fperror_float.c -Wall
$ ./fperror_float
init=210.0000000000
x=210.0000000000
dx=30*sqrt(2)=42.4264068604
y=210.0000000000
z=210.0000000000
i x+=dx y+=30*sqrt(2) z=init+i*30*sqrt(2) x-y x-z
1 252.4264068604 252.4264068604 252.4264068604 0.0000000000 0.0000000000
2 294.8528137207 294.8528137207 294.8528137207 0.0000000000 0.0000000000
3 337.2792358398 337.2792358398 337.2792358398 0.0000000000 0.0000000000
4 379.7056274414 379.7056579590 379.7056274414 -0.0000305176 0.0000000000
5 422.1320190430 422.1320800781 422.1320495605 -0.0000610352 -0.0000305176
6 464.5584106445 464.5585021973 464.5584411621 -0.0000915527 -0.0000305176
7 506.9848022461 506.9849243164 506.9848632812 -0.0001220703 -0.0000610352
8 549.4111938477 549.4113159180 549.4112548828 -0.0001220703 -0.0000610352
9 591.8375854492 591.8377075195 591.8376464844 -0.0001220703 -0.0000610352
10 634.2639770508 634.2640991211 634.2640991211 -0.0001220703 -0.0001220703
Leave a Reply