Skip to main content
GameDev.net gamedev.net
🔒 Locked

Strange arithmetic problem

Started by enmaniac Feb 24, 2006 at 7:12 AM 1 replies 800+ views
Original Post
enmaniac
enmaniac
Hello, Recently, I am developing some application. When it came to implement OnMouseWheel function I came accros this problem. Here is the code:

// get scrollbar info
si.cbSize = sizeof( SCROLLINFO );
si.fMask  = SIF_ALL;
GetScrollInfo( SB_VERT, &si );

// get change ration
si.fMask = SIF_POS;

// THIS IS THE LINE WHICH PRODUCES THE WRONG RESULT!!!
si.nPos  -= ( zDelta/WHEEL_DELTA )*si.nPage/2;

SetScrollInfo( SB_VERT, &si, TRUE );
When: zDelta = -120 WHEEL_DELTA = 120 si.nPage = 263 I got result = -2147483516 but when I change that line into:
si.nPos  -= ( zDelta/WHEEL_DELTA )*( si.nPage/2 );
everything is ok. Why is it so ? I guess there is sth to do with arithmetic operator priorites, Am I right ? Thx
yadango
yadango
yeah, but i think it's about type conversion between int and unsigned int since nPage is unsigned. betcha casting (int)si.nPage in the first case also solves your problem too.

first case
(int/int)*uint/int = (int)*uint/int -> uint/int (the -1 is converted to a uint)

second case
(int/int)*(si.nPage/2) = (int/int)*(uint/int) = (int)*(int) after calc is done

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.