码迷,mamicode.com
首页 > 其他好文 > 详细

pow log 与 (int)

时间:2018-09-12 22:05:21      阅读:334      评论:0      收藏:0      [点我收藏+]

标签:cout   技术   out   ima   出现   \n   整数   bubuko   1.5   

 

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\n",a1);
13     printf("%d\n",a2);
14     printf("%d\n",a3);
15 
16     printf("%d\n",b1);
17     printf("%d\n",b2);
18     printf("%d\n",b3);
19 
20     printf("%d\n",c1);
21     printf("%d\n",c2);
22     printf("%d\n",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("\n");
 6 
 7     printf("%f\n",pow(10,0));
 8     printf("%f\n",pow(10,1));
 9     printf("%f\n",pow(10,2));
10 
11     printf("\n");
12 
13     printf("%d\n",pow(10,0));
14     printf("%d\n",pow(10,1));
15     printf("%d\n",pow(10,2));
16 
17     printf("\n");
18 
19     printf("%d\n",(int)pow(10,0));
20     printf("%d\n",(int)pow(10,1));
21     printf("%d\n",(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..9xxx。

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

 

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

 

技术分享图片

 

pow log 与 (int)

标签:cout   技术   out   ima   出现   \n   整数   bubuko   1.5   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!