[Tinymux] Rounding and workarounds
Stephen Dennis
brazilofmux at gmail.com
Tue Mar 22 13:38:25 EST 2005
David Stone reports two issues with floating-point. The first issue is
related to rounding:
think round(4.5,0) => 4
think round(5.5,0) => 6
The strtod code that TinyMUX uses rounds to nearest with ties broken
by the IEEE round-even rule. Why does such a rule exist? Always
rounding 0.5 up introduces a slight bias which can build up over the
course of a lengthy calculation. There are several standard rounding
rules. I wouldn't have picked this one except that I needed to use
David M. Gay's strtod code for other reasons.
The second issue is related to inexact subtraction:
think ladd(4.1 -4 -0.1) => -3.608224830031759E-16
We think in base 10 (10, 1, 0.1, 0.01), but floating-point numbers are
represented in base 2 (4, 2, 1, 0.5, 0.25). Some numbers can be
represented exactly in both, but most cannot, and some error is always
introduced by addition, subtraction, multiplication, division,
intrinsic functions, etc. Also, errors always add.
X = x + dx
Y = y + dy
Z = X + Y = (x + dx) + (y + dy) = (x + y) + (dx + dy) = z + dz
i.e., z = x + y and dz = dx + dy
Z = X - Y = (x + dx) - (y + dy) = (x - y) + (dx + dy) = z + dz
i.e., z = x - y and dz = dx + dy
Relative error is dz/z, so for X - Y, we have (dx + dy)/(x - y).
Basically, if you subtract numbers that are very close to each other,
the relative error can be larger than the expected result.
TinyMUX goes to a lot of trouble to insure the resulting error does
not accumulate even faster than described above, but it cannot
overcome the error from approximate representation without switching
to a different representation entirely. For example, you can add
positive numbers all day long, and TinyMUX will return a pleasant
result.
TinyMUX is exposing the raw power and limitations of the internal
floating-point data type. Other codebases are built on top of this
same data type, but the result is rounded to an arbitrary decimal
place. PennMUSH rounds to a configurable decimal place (all
calculations in the game rounded to the same position). TinyMUSH 3.x
rounds to the 6th decimal place. I'm not sure about RhostMUSH, but it
would probably be configurable like PennMUSH.
Brazil
More information about the Tinymux
mailing list