Talk:Damage: Difference between revisions

Line 134: Line 134:
Now for the interesting bit. What they have with the 0x800 here is actually to make the end result round properly, as opposed to simply truncating it as the normal method would. This is equivalent to adding 1/8192 to your final result; this number is used because the resolution of a Q3.12 number is 1/4096. If the bit in position 0x800 (i.e. the 12th bit from the right) is 0, adding 0x800 will do nothing, and this extra bit will be truncated off, thus rounding down, when the right shift occurs. If this bit is 1, adding 0x800 would have the effect of rounding up, adding 1/4096 to the truncated value, as the addition would cause a carry into position 0x1000.
Now for the interesting bit. What they have with the 0x800 here is actually to make the end result round properly, as opposed to simply truncating it as the normal method would. This is equivalent to adding 1/8192 to your final result; this number is used because the resolution of a Q3.12 number is 1/4096. If the bit in position 0x800 (i.e. the 12th bit from the right) is 0, adding 0x800 will do nothing, and this extra bit will be truncated off, thus rounding down, when the right shift occurs. If this bit is 1, adding 0x800 would have the effect of rounding up, adding 1/4096 to the truncated value, as the addition would cause a carry into position 0x1000.


Thus, the formula M<nowiki>'' = ((M * M') + 0x800) >> 12 is exactly the way to multiply two Q3.12 numbers or indeed any two Qn.12 numbers, while rounding in the conventional manner to be as accurate as possible.
Thus, the formula M<nowiki>''</nowiki> = ((M * M') + 0x800) >> 12 is exactly the way to multiply two Q3.12 numbers or indeed any two Qn.12 numbers, while rounding in the conventional manner to be as accurate as possible.


Sorry for the impromptu lesson in fixed-point arithmetic; I wanted to prove this result rigorously so that we can be absolutely certain that it holds up! In fact, this also holds for 2's-complement signed fixed-point numbers, but I won't bother proving that as the "other" value is never negative. --[[User:Felthry|Felthry]] ([[User talk:Felthry|talk]]) 16:24, 28 February 2017 (UTC)
Sorry for the impromptu lesson in fixed-point arithmetic; I wanted to prove this result rigorously so that we can be absolutely certain that it holds up! In fact, this also holds for 2's-complement signed fixed-point numbers, but I won't bother proving that as the "other" value is never negative. --[[User:Felthry|Felthry]] ([[User talk:Felthry|talk]]) 16:24, 28 February 2017 (UTC)
314

edits