??? 09/08/08 16:39 Read: times |
#158081 - Rounding instead of truncate Responding to: ???'s previous message |
Before shifting right 8 steps (dividing by 256) he is adding 0.5*256. Without this add, the division would just truncate instead of rounding to nearest integer.
0+128 = 128. 128 / 256 = 0. 100+128 = 228. 228 / 256 = 0. 127+128 = 255. 255 / 256 = 0. 128+128 = 256. 256 / 256 = 1. 200+128 = 328. 328 / 256 = 1. As you can see, the add of (1<<7) will result in a correctly rounded answer as long as you work with positive numbers. |
Topic | Author | Date |
Scale offset using ints by byte position location | 01/01/70 00:00 | |
Does type casting make sense here?... | 01/01/70 00:00 | |
Show us more......... | 01/01/70 00:00 | |
Here are defs and original function | 01/01/70 00:00 | |
ints, at leastin Keil, at 16 bits wide. | 01/01/70 00:00 | |
Christoph, | 01/01/70 00:00 | |
That makes things clearer. | 01/01/70 00:00 | |
A detail | 01/01/70 00:00 | |
Take a look at the ranges | 01/01/70 00:00 | |
Brett, that is helpful, I should add.... | 01/01/70 00:00 | |
No floating-point math required. | 01/01/70 00:00 | |
Its working but I have a question on theory | 01/01/70 00:00 | |
Rounding instead of truncate | 01/01/70 00:00 | |
oh, that is interesting | 01/01/70 00:00 | |
Russ' comment is right, though.![]() | 01/01/70 00:00 |