pow log 与 (int)

1.不能用%d输出double类型的数

 1     double a1=5.3;
 2     double a2=1234.1234;
 3     double a3=3412341.12341234;
 4 
 5     double b1=1.5;
 6     double b2=123.5;
 7     double b3=23412.5;
 8 
 9     double c1=10.7;
10     double c2=2.9;
11     double c3=3241324.56251;
12     printf("%d
",a1);
13     printf("%d
",a2);
14     printf("%d
",a3);
15 
16     printf("%d
",b1);
17     printf("%d
",b2);
18     printf("%d
",b3);
19 
20     printf("%d
",c1);
21     printf("%d
",c2);
22     printf("%d
",c3);

输出的值不会变

2.

float pow(float x, float y)

x,y可以为整形,因为进行时会自动转换类型,

但是用%d输出double类型的数则不行。

可以使用(int)。

 1     cout<<pow(10,0)<<endl;
 2     cout<<pow(10,1)<<endl;
 3     cout<<pow(10,2)<<endl;
 4 
 5     printf("
");
 6 
 7     printf("%f
",pow(10,0));
 8     printf("%f
",pow(10,1));
 9     printf("%f
",pow(10,2));
10 
11     printf("
");
12 
13     printf("%d
",pow(10,0));
14     printf("%d
",pow(10,1));
15     printf("%d
",pow(10,2));
16 
17     printf("
");
18 
19     printf("%d
",(int)pow(10,0));
20     printf("%d
",(int)pow(10,1));
21     printf("%d
",(int)pow(10,2));

3.

float log(float x)

(int)使用在在(int)右边的第一个整数。

所以不能使用(int)log(s)/log(2),而是(int)(log(s)/log(2))。

而求log(2^k)/log(2)时,出现问题,因为是精度不准的问题,如结果为1.99..9xxxd,用(int)后结果为1。

可以采用log(2^k+minv)/log(2)解决,minv=1e-12(或其它)。

 1     printf("%.15f
",log(2)/log(2));
 2     printf("%.15f
",log(4)/log(2));
 3     printf("%.15f
",log(8)/log(2));
 4     printf("%.15f
",log(16)/log(2));
 5     printf("%.15f
",log(32)/log(2));
 6 
 7     printf("
");
 8 
 9     printf("%f
",(int)log(2)/log(2));
10     printf("%f
",(int)log(4)/log(2));
11     printf("%f
",(int)log(8)/log(2));
12     printf("%f
",(int)log(16)/log(2));
13     printf("%f
",(int)log(32)/log(2));
14 
15     printf("
");
16 
17     printf("%d
",(int)(log(2)/log(2)));
18     printf("%d
",(int)(log(4)/log(2)));
19     printf("%d
",(int)(log(8)/log(2)));
20     printf("%d
",(int)(log(16)/log(2)));
21     printf("%d
",(int)(log(32)/log(2)));
22     printf("%d
",(int)(log(64)/log(2)));
23     printf("%d
",(int)(log(128)/log(2)));
24 
25     printf("
");
26 
27     printf("%d
",(int)(log(2+minv)/log(2)));
28     printf("%d
",(int)(log(4+minv)/log(2)));
29     printf("%d
",(int)(log(8+minv)/log(2)));
30     printf("%d
",(int)(log(16+minv)/log(2)));
31     printf("%d
",(int)(log(32+minv)/log(2)));
32     printf("%d
",(int)(log(64+minv)/log(2)));
33     printf("%d
",(int)(log(128+minv)/log(2)));

原文地址:https://www.cnblogs.com/cmyg/p/9637583.html