简体   繁体   中英

C type cast for typedef defined in preprocessor block

I am trying to fix this compiler warning:

warning C4244: '=' : conversion from 'double' to 'myRealVar', possible loss of data

myRealVar is defined in a preprocessor block:

#ifdef SINGLE_PRECISION
typedef float myRealVar;
#else
typedef double myRealVar;
#endif

The warning occurs on the line that basically looks like this:

someVar[a][b] = 1.0/sqrt(someVar[a][b]);

I have tried C-style type casting: (myRealVar) but this doesn't seem to resolve the warning. How should I properly resolve this issue?

Edit: This did the trick. Thanks Weather Vane and Olaf. Not sure why my previous attempts at C-casting didn't working.

someVar[a][b] = (myRealVar)(1.0/sqrt(someVar[a][b]));

You need to cast the 1.0 as well as the return value from sqrt , to remove the warning. Both have the type double .

someVar[a][b] = (myRealVar)1.0 / (myRealVar)sqrt(someVar[a][b]);

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