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\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..9xxxd,用(int)后结果为1。
可以采用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)));