pow log 与 (int)

时间:2022-01-01 17:00:34

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

     double a1=5.3;
double a2=1234.1234;
double a3=3412341.12341234; double b1=1.5;
double b2=123.5;
double b3=23412.5; double c1=10.7;
double c2=2.9;
double c3=3241324.56251;
printf("%d\n",a1);
printf("%d\n",a2);
printf("%d\n",a3); printf("%d\n",b1);
printf("%d\n",b2);
printf("%d\n",b3); printf("%d\n",c1);
printf("%d\n",c2);
printf("%d\n",c3);

输出的值不会变

pow log 与 (int)

2.

float pow(float x, float y)

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

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

可以使用(int)。

     cout<<pow(,)<<endl;
cout<<pow(,)<<endl;
cout<<pow(,)<<endl; printf("\n"); printf("%f\n",pow(,));
printf("%f\n",pow(,));
printf("%f\n",pow(,)); printf("\n"); printf("%d\n",pow(,));
printf("%d\n",pow(,));
printf("%d\n",pow(,)); printf("\n"); printf("%d\n",(int)pow(,));
printf("%d\n",(int)pow(,));
printf("%d\n",(int)pow(,));

pow log 与 (int)

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(或其它)。

     printf("%.15f\n",log()/log());
printf("%.15f\n",log()/log());
printf("%.15f\n",log()/log());
printf("%.15f\n",log()/log());
printf("%.15f\n",log()/log()); printf("\n"); printf("%f\n",(int)log()/log());
printf("%f\n",(int)log()/log());
printf("%f\n",(int)log()/log());
printf("%f\n",(int)log()/log());
printf("%f\n",(int)log()/log()); printf("\n"); printf("%d\n",(int)(log()/log()));
printf("%d\n",(int)(log()/log()));
printf("%d\n",(int)(log()/log()));
printf("%d\n",(int)(log()/log()));
printf("%d\n",(int)(log()/log()));
printf("%d\n",(int)(log()/log()));
printf("%d\n",(int)(log()/log())); printf("\n"); printf("%d\n",(int)(log(+minv)/log()));
printf("%d\n",(int)(log(+minv)/log()));
printf("%d\n",(int)(log(+minv)/log()));
printf("%d\n",(int)(log(+minv)/log()));
printf("%d\n",(int)(log(+minv)/log()));
printf("%d\n",(int)(log(+minv)/log()));
printf("%d\n",(int)(log(+minv)/log()));

pow log 与 (int)