casting - Float and double rounding in C -


i went upon strange behavior (to me) :

    int generate_scenario_one_pass(file *out, double freq_mhz) {         unsigned int d_freq, d_freq_test;         d_freq              = (int)(freq_mhz * 20);         d_freq_test         = (int)(float)(freq_mhz * 20);         printf("when freq_mhz = %.1f, d_freq = 0x%04x, d_freq_test = 0x%04x\n", freq_mhz, d_freq, d_freq_test);     } 

the whole code not here, it's not relevant. function called several times increasing values, starting 2110.0 increment of 0.1.

when freq_mhz = 2110.0, d_freq = 0xa4d8, d_freq_test = 0xa4d8 when freq_mhz = 2110.1, d_freq = 0xa4da, d_freq_test = 0xa4da when freq_mhz = 2110.2, d_freq = 0xa4dc, d_freq_test = 0xa4dc when freq_mhz = 2110.3, d_freq = 0xa4dd, d_freq_test = 0xa4de 

at last iteration, d_freq wrong! d_freq_test has correct value. issue solved casting double float, float int. wanted know why.

this compiled using msvc++ 6.0 on x86 cpu.

there many numbers cannot represented floating-point number - , 0.1 among them (it rounded closest number can represented - along lines of 0.0999999999999998). when using double, 2110.3 happens represented number smaller 2110.3, giving "wrong" result when multiply 20 , cast int (which round down), while 2110.3 float represented number bigger 2110.3, giving expected outcome.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -