简体   繁体   中英

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. Any suggestions?

Thanks

I think you are only casting the output of the first log(..) function. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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