TRIG_LAWS.bend source
TRIG_LAWS.bend on the hub · documented module
# The laws of bend-float64's large-argument reduction for sin, cos and tan (fast.bend), at literal inputs: the# window of 2/pi's bits taken for an exponent, and the quadrant and fraction found for an argument. The expected# values come from test/gen_trig_laws.py, which computes them from mpmath's 2/pi with Python integers and checks each# fraction against |x| 2/pi computed by mpmath. The functions' results are tested instead (test/trig_check.py).# Gate: ./bend-check TRIG_PROOF.bend --verdictimport Baseimport ./wide.bend as Wimport ./float64.bend as Fimport ./fb.bend as Qimport ./fast.bend as FSimport ./big.bend as Bimport ./math.bend as M# Guarantees: the window of 2/pi's bits the fast path takes for an argument with biased exponent 1053 (|x| near# 2^30, the smallest that uses it), 1600 and 2046 (the largest finite) is exactly the 256 bits that matter there.# Why it matters: a window one bit off still gives a plausible-looking fraction, wrong for every argument with that# exponent; the error bounds cannot catch it, since they assume the right bits.law trig_window: {[FS.window(1053), FS.window(1600), FS.window(2046)] == [FS.R4{W.W64{162, 4186140238}, W.W64{1142237692, 660066805}, W.W64{886948059, 1653971260}, W.W64{1133527550, 1365486558}}, FS.R4{W.W64{3816895906, 4226943436}, W.W64{2394016794, 2580522212}, W.W64{586964463, 3376535807}, W.W64{3300916976, 750813049}}, FS.R4{W.W64{1333104599, 3420649082}, W.W64{242478868, 2770719959}, W.W64{4139737663, 448401580}, W.W64{106991096, 4141307873}}] : List<&2, FS.R4>}# Guarantees: the quadrant and fraction found for 6381956970095103 * 2^797 (the float64 closest to a multiple of# pi/2, 2^-60.9 of a quadrant away), for the largest power of two 2^1023, for 2^30 (1 + 7 * 2^-52) and for# -2^30 (1 + 2^-52), and two arguments whose product with the window carries from its first and from its second# 64-bit word: quadrant (counted past 3 when a fraction from 1/2 up moves to the next), the fraction's sign,# its 128 bits, its exponent and its error count.# Why it matters: these pin the product with the window (its carries), the split into quadrant and fraction, the# switch to the next quadrant from 1/2 up, and the normalization of a fraction 61 bits below 1.law trig_far: {[FS.far(F.F64{1963371611, 640459263}), FS.far(F.F64{2145386496, 0}), FS.far(F.F64{1104150528, 7}), FS.far(F.F64{3251634176, 1}), FS.far(F.F64{1107126453, 3591395173}), FS.far(F.F64{1279188277, 1146862456})] == [FS.KF{1, Q.FB{False{}, W.W128{W.W64{2955153520, 1083003905}, W.W64{1775977343, 1447033185}}, 4294967107, 2}}, FS.KF{2, Q.FB{True{}, W.W128{W.W64{3271083528, 1974299925}, W.W64{2483331150, 3765609207}}, 4294967167, 2}}, FS.KF{4, Q.FB{True{}, W.W128{W.W64{3638415812, 1831613176}, W.W64{549741046, 4145596303}}, 4294967167, 2}}, FS.KF{4, Q.FB{True{}, W.W128{W.W64{3638423635, 902881700}, W.W64{3837542718, 1469064585}}, 4294967167, 2}}, FS.KF{1, Q.FB{False{}, W.W128{W.W64{2461211933, 1465160887}, W.W64{2915416632, 4029660304}}, 4294967167, 2}}, FS.KF{1, Q.FB{False{}, W.W128{W.W64{2272790719, 2815997824}, W.W64{5751004, 2178804378}}, 4294967165, 2}}] : List<&2, FS.KF>}# Guarantees: the slow path's windowed reduction puts 6381956970095103 * 2^797 in quadrant 1, as the fast path does.# Why it matters: the slow path takes only a window of 2/pi's bits now; a window starting one word too low would# drop bits that are not whole turns and change the quadrant.law trig_slow_quadrant: {M.red_k(M.reduce(B.of_w64(W.W64{1485915, 640459263}), 797, 128)) == 1 : U32}