14 lines
818 B
Plaintext
14 lines
818 B
Plaintext
// MinimumImage: the displacement Dx brought to its nearest periodic image in
|
|
// a box of length Box, the minimum image convention: Dx minus the whole
|
|
// number of box lengths that brings it into [-Box / 2, Box / 2). FLOOR of
|
|
// Dx / Box + 0.5 rounds halves upward, so exactly half a box maps to -Box / 2.
|
|
// That range holds over exact numbers (the Dafny proof). In FLOAT arithmetic
|
|
// Dx / Box and the product are rounded, so the result can land a rounding
|
|
// error outside it (MinimumImage(1.35, 0.1) is -0.050000000000000044), and
|
|
// within a rounding error of half a box it can give the farther of the two
|
|
// closest images (MinimumImage(0.49999999999999994, 1.0) is -0.5).
|
|
FUNCTION MinimumImage(Dx : FLOAT, Box : FLOAT) RETURNS FLOAT
|
|
REQUIRE Box > 0.0
|
|
RETURN Dx - Box * FLOOR(Dx / Box + 0.5)
|
|
ENDFUNCTION
|