大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
double r,n,p;// 定义为 int n 对应下面%d
成都创新互联公司-专业网站定制、快速模板网站建设、高性价比青田网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式青田网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖青田地区。费用合理售后完善,10年实体公司更值得信赖。
scanf("%f%d", r,n);//double 型 格式输入 %lf
r=r+1;
p=pow(r,n);
printf("%f\n", p);//改为%lf
这里你 用float型 输入输出double型数据 会造成数据缺失
函数没有问题,这是浮点数精度的问题。就是你所计算的数超过了浮点数的精度范围(不是表示范围)。双精度浮点数小数最后几位有时候会是9996和0004
有两个问题,
1. 第三个输出格式错误了!!!:printf(" 3 %%0.1f %0.3f %0.3f\n",100*a3[i],b3[i],tx3[i]);,多了一个 %,正确格式:
printf(" 3 %0.1f %0.3f %0.3f\n",100*a3[i],b3[i],tx3[i]);
2. 编译不会自动链接math的库,
gcc strange.c
/tmp/ccMYdgve.o: In function `main':
strange.c:(.text+0x87e): undefined reference to `pow'
strange.c:(.text+0x947): undefined reference to `pow'
strange.c:(.text+0xa0a): undefined reference to `pow'
strange.c:(.text+0xa9a): undefined reference to `pow'
collect2: ld returned 1 exit status
解决办法,加上 -lm 编译选项。
qunengrong@qunengrong-laptop ~/test
$gcc strange.c -lm
qunengrong@qunengrong-laptop ~/test
编译成功了,运行可得结果:
./a.out
0 100.0 100.0 100.00 1 100.0 1.061 14.142 14.142 5414.21 1.062
2 100.0 0.707 0.000
3 100.0 0.000 1.062
1 1502.5 100.0 1502.51 1 1502.5 0.071 0.941 0.941 4094.12 1.005
2 100.0 0.047 0.000
3 1502.5 -1.755 1.005
2 1420.8 100.0 1420.82 1 1420.8 0.075 0.995 0.995 4099.54 1.005
2 100.0 0.050 0.000
3 1420.8 0.000 1.005
3 1421.2 100.0 1421.23 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 -0.000 1.005
4 1421.2 100.0 1421.24 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
5 1421.2 100.0 1421.25 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 -0.000 1.005
6 1421.2 100.0 1421.26 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 -0.000 1.005
7 1421.2 100.0 1421.27 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
8 1421.2 100.0 1421.28 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
9 1421.2 100.0 1421.29 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 -0.000 1.005
10 1421.2 100.0 1421.210 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
11 1421.2 100.0 1421.211 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
12 1421.2 100.0 1421.212 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 -0.000 1.005
13 1421.2 100.0 1421.213 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 -0.000 1.005
14 1421.2 100.0 1421.214 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
15 1421.2 100.0 1421.215 1 1421.2 0.075 0.995 13073.374 53858472.00 1.000
2 100.0 0.050 1.000
3 1421.2 13073.374 1.000
16 18579631.2 1307337.4 18579631.216 1 18579631.2 0.000 0.000 0.000 4099.51 1.005
2 1307337.4 0.000 0.000
3 18579631.2 0.000 1.005
17 1421.2 100.0 1421.217 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
18 1421.2 100.0 1421.218 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
19 1421.2 100.0 1421.219 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
20 1421.2 100.0 1421.220 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
21 1421.2 100.0 1421.221 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
22 1421.2 100.0 1421.222 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
23 1421.2 100.0 1421.223 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
24 1421.2 100.0 1421.224 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
25 1421.2 100.0 1421.225 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
26 1421.2 100.0 1421.226 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
27 1421.2 100.0 1421.227 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
28 1421.2 100.0 1421.228 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
29 1421.2 100.0 1421.229 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
30 1421.2 100.0 1421.230 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
31 1421.2 100.0 1421.231 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
32 1421.2 100.0 1421.232 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
33 1421.2 100.0 1421.233 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
34 1421.2 100.0 1421.234 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
35 1421.2 100.0 1421.235 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
36 1421.2 100.0 1421.236 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
37 1421.2 100.0 1421.237 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
38 1421.2 100.0 1421.238 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
39 1421.2 100.0 1421.239 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
40 1421.2 100.0 1421.240 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
41 1421.2 100.0 1421.241 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
42 1421.2 100.0 1421.242 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
43 1421.2 100.0 1421.243 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
44 1421.2 100.0 1421.244 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
45 1421.2 100.0 1421.245 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
46 1421.2 100.0 1421.246 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
47 1421.2 100.0 1421.247 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
48 1421.2 100.0 1421.248 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
49 1421.2 100.0 1421.249 1 1421.2 0.075 0.995 0.995 4099.51 1.005
2 100.0 0.050 0.000
3 1421.2 0.000 1.005
后记: pow 函数,计算时默认对double类型进行运算,float也可以会自动转换的,小数也没问题,在linux下你可以参看原型:
$man 3 pow
SYNOPSIS
#include math.h
double pow(double x, double y);
float powf(float x, float y);
long double powl(long double x, long double y);
Link with -lm.
看错误就知道了,pow函数第一个参数不支持int型的,要么double,要么float要么long double,改成p=pow(2.0,i);吧