#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include "../apue.2e/include/apue.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <assert.h>
#include <string.h>
#include <limits.h>
void test_1(void)
{
/*
四舍六入五成双规则:
对于位数很多的近似数,当有效位数确定后,其后面多余的数字应该舍去,只保留有效数字最末一位,
这种修约(舍入)规则是“四舍六入五成双”,也即“4舍6入5凑偶”这里“四”是指≤4 时舍去,
"六"是指≥6时进上,"五"指的是根据5后面的数字来定,当5后有有效数时(cc_1/cc_2),舍5入1;当5后无有效数字
时(bb_1/bb_2),需要分两种情况来讲:①5前为 奇数,舍5入1;②5前为偶数,舍5不进。(0是 偶数)
下面举例,用上述规则对下列数据保留3位有效数字:
*/
float aa_1 = 9.8249; // 9.82
float aa_2 = 9.82671; // 9.83
float bb_1 = 9.8250; // 9.82
float bb_2 = 9.8350; // 9.84
float cc_1 = 9.8351; // 9.84
float cc_2 = 9.82501; // 9.83
printf("%.2f\n", aa_1);
printf("%.2f\n", aa_2);
printf("%.2f\n", bb_1);
printf("%.2f\n", bb_2);
printf("%.2f\n", cc_1);
printf("%.2f\n", cc_2);
printf("===================\n");
char xx_1[8];
char xx_2[8];
char yy_1[8];
char yy_2[8];
char zz_1[8];
char zz_2[8];
sprintf(xx_1, "%.2f\n", aa_1);
sprintf(xx_2, "%.2f\n", aa_2);
sprintf(yy_1, "%.2f\n", bb_1);
sprintf(yy_2, "%.2f\n", bb_2);
sprintf(zz_1, "%.2f\n", cc_1);
sprintf(zz_2, "%.2f\n", cc_2);
printf(xx_1);
printf(xx_2);
printf(yy_1);
printf(yy_2);
printf(zz_1);
printf(zz_2);
}
int main(int argc, char* argv[])
{
test_1();
return;
}