Monday, May 24, 2010

// Visual c++ int n = 2.05 * 100; Why is n == 204 ???

int n = 2.05 * 100;





Execute the above and tell me why MS VC++ works this way.


Thanks,


Brian....





Typecasting doesn't improve anything..


I've used the following work around.





int integer(double dbl)


{ double smn = 0.00000000000005;


return((int)(dbl + (dbl %26lt; 0 ? -smn : smn)));


}





Is this a good idea?


Thanks again,


Brian...

// Visual c++ int n = 2.05 * 100; Why is n == 204 ???
its because "int" is only a whole number, no fractions allowed. you need to change "int n = 2.05*100;" to


"float n = 2.05*100;".
Reply:y sure 2.05 is not actually 2.05 it will be in fraction 2.046789 or 2.049999 when it will in register so when we mul with 100 we gat int 204
Reply:I belive this is due to what is known as bankers rounding in the long standing Microsoft Visual Studio Languages.





try to round off the number to zero places behind the decimal.





That is usually a good workaround.

pink

No comments:

Post a Comment