编写一个C语言小程序,验证对于多个数的加(减)运算,定点数的运算结果与运算顺序无关(即使是含义运算溢出),而浮点数的运算结果可能与运算顺序有关
时间: 2024-06-01 10:11:26 浏览: 52
C语言浮点数运算
5星 · 资源好评率100%
#include <stdio.h>
int main() {
// 定点数的运算
int a = 10, b = 20, c = 30, d = 40;
int result1 = a + b + c + d;
int result2 = a + c + b + d;
printf("Fixed-point addition result1: %d\n", result1);
printf("Fixed-point addition result2: %d\n", result2);
int result3 = a - b - c - d;
int result4 = a - c - b - d;
printf("Fixed-point subtraction result3: %d\n", result3);
printf("Fixed-point subtraction result4: %d\n", result4);
// 浮点数的运算
float x = 1.0, y = 2.0, z = 3.0, w = 4.0;
float result5 = x + y + z + w;
float result6 = x + z + y + w;
printf("Floating-point addition result5: %f\n", result5);
printf("Floating-point addition result6: %f\n", result6);
float result7 = x - y - z - w;
float result8 = x - z - y - w;
printf("Floating-point subtraction result7: %f\n", result7);
printf("Floating-point subtraction result8: %f\n", result8);
return 0;
}
阅读全文