#10536 - regularkid - Mon Sep 08, 2003 5:03 am
I'm trying to round up some fixed point numbers (12.20). Since I can have positive or negative values, here is what I want to happen:
If the decimal part of the number is greater than 0.5 (2048), then:
If the number is positive, then add 2048
If the number is negative, then subtract 2048
Here is the direct translation of the above that I am currently using in my code:
So, the above works and gives me what I want, but I would like to do it without the 'if's. I'm stumped...anyone have any good simple ways of rounding up fixed point numbers both negative and positive? Thanks!
_________________
- RegularKid
If the decimal part of the number is greater than 0.5 (2048), then:
If the number is positive, then add 2048
If the number is negative, then subtract 2048
Here is the direct translation of the above that I am currently using in my code:
Code: |
if(num & 0x00000800) { if(num > 0) { num += 2048; } else { num -= 2048; } } num >>= 12; |
So, the above works and gives me what I want, but I would like to do it without the 'if's. I'm stumped...anyone have any good simple ways of rounding up fixed point numbers both negative and positive? Thanks!
_________________
- RegularKid