- 以心消业
-
有两个问题,
1. 第三个输出格式错误了!!!:printf(" 3 %%0.1f %0.3f %0.3f ",100*a3[i],b3[i],tx3[i]);,多了一个 %,正确格式:
printf(" 3 %0.1f %0.3f %0.3f ",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.
- bikbok
-
因为float类型变量表示的小数范围比较小,但是在
tx1[i]=(W[i]*f1[i]*f1q[i])/(u*p*E*a1p[i]*a1p[i]);
tx1[i]=pow(tx1[i],0.2);
a1[i+1]=tx1[i]*a1p[i];
tx2[i]=(W[i]*f2[i]*f2q[i])/(u*p*E*a2p[i]*a2p[i]);
tx2[i]=pow(tx2[i],0.2);
a2[i+1]=tx2[i]*a2p[i];
tx3[i]=(W[i]*f3[i]*f3q[i])/(u*p*E*a3p[i]*a3p[i]);
tx3[i]=pow(tx3[i],0.2);
a3[i+1]=tx3[i]*a3p[i];
这些运算以后,等号后面这个式子的运算结果超过了float所能表示的小数范围,所以会自动把结果转化为表示范围更大的double类型,但是你lixi这个变量还是float的,所以就会有这个警告,解决的方法就是把float改成double就行。希望能帮助你!
- kikcik
-
#define F 30000
#define u 0.03f
#define E 10000000
#define p 1
#define amin 1
#define L 100
#define N 50
#include<stdio.h>
#include<math.h>
int main()
{
float W[N],fm,f1[N],f2[N],f3[N],f1q[N],f2q[N],f3q[N],up[N],y[N],b1[N],b2[N],b3[N],bmax[N],rmax[N],a1[N],a2[N],a3[N],a1p[N],a2p[N],a3p[N];
int i;
float tx1[N],tx2[N],tx3[N];
a1[0]=a2[0]=a3[0]=1;
for(i=0;i<N;i++)
{
printf("%d %0.1f %0.1f %0.1f",i,100*a1[i],100*a2[i],100*a3[i]);
fm=a1[i]*a2[i]+sqrt(2.0f)*a1[i]*a3[i]+a2[i]*a3[i];
f1[i]=(a1[i]*a3[i]+sqrt(2.0f)*a1[i]*a2[i])*F/fm;
f2[i]=(a2[i]*a3[i]-a1[i]*a2[i])*F/fm;
f3[i]=0-(sqrt(2.0f)*a2[i]*a3[i]+a1[i]*a3[i])*F/fm;
f1q[i]=(a1[i]*a3[i]+sqrt(2.0f)*a1[i]*a2[i])/fm;
f2q[i]=(a2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0.3f %0.3f %0.2f %0.3f ",i,100*a1[i],b1[i],y[i],rmax[i],W[i],tx1[i]);
printf(" 2 %0.1f %0.3f %0.3f ",100*a2[i],b2[i],tx2[i]);
printf(" 3 %%0.1f %0.3f %0.3f ",100*a3[i],b3[i],tx3[i]);
a1[i+1]=(a1[i+1]>=1.000)? a1[i+1]:1.000f;
a2[i+1]=(a2[i+1]>=1.000)? a2[i+1]:1.000f;
a3[i+1]=(a3[i+1]>=1.000)? a3[i+1]:1.000f;
}
}
已修正所有编译错误和警告,至于逻辑上,就要看你具体要实现什么功能,不过还是有一个很明显的逻辑错误的,就是你的数组越界了,你访问了第51个数据,因此会导致运行中断
要想进一步修改,就得看看你的算法了
- 左迁
-
不是那个问题;
你检查一下tx1[i],
- 大鱼炖火锅
-
确实是的。惊奇ing。