I need to multiply integer value with a factor and want to do an overflow check with INT_MAX, INT_MIN for this.
Integer contains 4 bytes on my machine an this is the max variable-size.
I need to do this check for signed integer and unsigned integer. Only integer math/variables allowed.
The factor always comes as fraction with a nominator and denominator. The denominator always is 10
Factor might be greater 1 or less than 1
So for example fraction is 25/10, varible a is signed integer
a = (a * 25)/10 -> may cause overflow, because (a * 25) can be > INT_MAX ( or < INT_MIN )
if (a > (INT_MAX * 10)/25 -> not possible because of overflow, INT_MAX is the maximum number the compiler can store.
if (a > INT_MAX * (10/25) -> not possible because 10/25 is 0.4 which will be 0 in integer math.
So any idea how to solve my problem?
Thanks for any hint!