Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

A Point in the Plane (BigFloat)

hypot(x, y, precision, mode) is the length of the point and arctan2(y, x, precision, mode) is its angle, with x.hypot(other=y) and y.arctan2(other=x) over them. Together they turn a pair of coordinates into polar form.

hypot forms x*x + y*y exactly, in integers, and never as two roundings that could each be wrong. Both squares are integers — a significand is one — so the sum over their common exponent is an integer too, and the integer square root of it brackets the answer with its remainder as the sticky bit. So hypot(3, 4) is 5, in every mode, which a pair of rounded squares and a rounded root could not promise.

A leg far below the other contributes nothing but a sticky bit, and the answer is then the larger leg moved by a hair — not the larger leg itself, which is the mistake that would make hypot(x, tiny) wrong in the directed modes.

arctan2 is where signed zeros earn their keep. arctan2(0, -1) is pi and arctan2(-0, -1) is -pi: the two zeros lie on opposite shores of the cut along the negative axis, and the sign is the only thing that says which one a point on the cut came from. The argument order is the one every other language uses — the second coordinate first, because the function inverts a tangent and a tangent is a rise over a run.

from decimo.bigfloat import arctan2, hypot
from decimo.bigfloat.bigfloat import BigFloat

def main() raises:
    var x = BigFloat.from_int(3, 53)
    var y = BigFloat.from_int(4, 53)
    print(hypot(x, y, 53))            # 5
    print(arctan2(y, x, 53))          # 0.92729521800161219