For example:
int a = 10;
float b 1.5;
a*=b; //"warning C4244: '*=' : conversion from 'float' to 'int', possible loss of data"
I want to suppress this warning. Of course, one why is to do so is:
a = (int)(a*b);
So actually I have two question:
Is there a way to keep using the assignment by operator and casting it in between?
No. Anything on the RHS of a *= b
affects b
and not the product a*b
.
Is there a way I can suppress the warning w/o using casting?
Use a nearest integer function that handles the conversion. The 2 functions below round to nearest rather than truncate as in some_int = some_float
.
#include <math.h>
// long int lrintf(float x);
// round their argument to the nearest integer value
// rounding according to the current rounding direction.
int a = 10;
float b = 1.5;
a = lrintf(a * b);
// or
// long int lroundf(float x);
// The lround and llround functions round their argument to the nearest integer value,
// rounding halfway cases away from zero, regardless of the current rounding direction.