简体   繁体   English

当我输入输出时,一个有趣的结果

[英]An interesting result when I typecast te output

I am observing an interesting result when I typecast an output: 输入输出时,我观察到一个有趣的结果:

Here is the code snippet: 这是代码片段:

int bitSize = (int)log10(1.0*16)/log10(2.0);   //bistsize = 3  it should be 4
int temp = log10(1.0*16)/log10(2.0);           //temp = 4   

Basically I want to take log2(16) which should be 4. I think my understanding of typecasting is wrong. 基本上我想采用应该为4的log2(16)。我认为我对类型转换的理解是错误的。 Any suggestions? 有什么建议么?

Thanks 谢谢

I think you are only casting the output of the first log(..) function. 我认为您只是在转换第一个log(..)函数的输出。 Put parenthesis around the entire expression: 在整个表达式周围加上括号:

int bitSize = (int)(log10(1.0*16)/log10(2.0));

Try: 尝试:

int bitSize = static_cast<int>(log10(1.0*16)/log10(2.0));

One of the niceties of the new C++ casts is that they parenthesize the argument, so it's clear exactly what you're casting. 新的C ++强制转换的好处之一是,它们用括号括起了参数,因此很清楚您要强制转换的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM