MATH_LAWS.bend open laws/TODOs
raw source on the hub · import bend-float64@0.1.6.0/MATH_LAWS.bend as MATH_LAWS
The laws of bend-float64's math functions (math.bend). Where a NaN goes is proved for every input. The special values (C99's Annex F, NumPy's NaN bits) and the exact answers (found before any ball is evaluated) are pinned at literal inputs; the checker cannot afford the arbitrary-precision evaluations themselves, so the correctly rounded values are tested instead (test/math_check.py: a million and a half against mpmath). Gate: ./bend-check MATH_PROOF.bend --verdict
3 imports
import Base import ./float64.bend as F import ./math.bend as M
Laws
law unary_nan provedin MATH_PROOF.bendsource · line 15 · raw
@+x:0x71d2c95108c3e65654506f5521343c81/float64.F64 -> @+h:{0x71d2c95108c3e65654506f5521343c81/float64.is_nan(x) == True{} : Bool} -> {[0x71d2c95108c3e65654506f5521343c81/math.exp(x), 0x71d2c95108c3e65654506f5521343c81/math.exp2(x), 0x71d2c95108c3e65654506f5521343c81/math.expm1(x), 0x71d2c95108c3e65654506f5521343c81/math.log(x), 0x71d2c95108c3e65654506f5521343c81/math.log2(x), 0x71d2c95108c3e65654506f5521343c81/math.log10(x), 0x71d2c95108c3e65654506f5521343c81/math.log1p(x), 0x71d2c95108c3e65654506f5521343c81/math.sin(x), 0x71d2c95108c3e65654506f5521343c81/math.cos(x), 0x71d2c95108c3e65654506f5521343c81/math.tan(x), 0x71d2c95108c3e65654506f5521343c81/math.arcsin(x), 0x71d2c95108c3e65654506f5521343c81/math.arccos(x), 0x71d2c95108c3e65654506f5521343c81/math.arctan(x), 0x71d2c95108c3e65654506f5521343c81/math.sinh(x), 0x71d2c95108c3e65654506f5521343c81/math.cosh(x), 0x71d2c95108c3e65654506f5521343c81/math.arcsinh(x), 0x71d2c95108c3e65654506f5521343c81/math.arccosh(x), 0x71d2c95108c3e65654506f5521343c81/math.arctanh(x), 0x71d2c95108c3e65654506f5521343c81/math.cbrt(x), 0x71d2c95108c3e65654506f5521343c81/math.tanh(x)] == [0x71d2c95108c3e65654506f5521343c81/float64.quiet(x), 0x71d2c95108c3e65654506f5521343c81/float64.quiet(x), 0x71d2c95108c3e65654506f5521343c81/float64.quiet(x), 0x71d2c95108c3e65654506f5521343c81/float64.quiet(x), 0x71d2c95108c3e65654506f5521343c81/float64.quiet(x), 0x71d2c95108c3e65654506f5521343c81/float64.quiet(x), 0x71d2c95108c3e65654506f5521343c81/float64.quiet(x), 0x71d2c95108c3e65654506f5521343c81/float64.quiet(x), 0x71d2c95108c3e65654506f5521343c81/float64.quiet(x), 0x71d2c95108c3e65654506f5521343c81/float64.quiet(x), 0x71d2c95108c3e65654506f5521343c81/float64.quiet(x), 0x71d2c95108c3e65654506f5521343c81/float64.quiet(x), 0x71d2c95108c3e65654506f5521343c81/float64.quiet(x), 0x71d2c95108c3e65654506f5521343c81/float64.quiet(x), 0x71d2c95108c3e65654506f5521343c81/float64.quiet(x), 0x71d2c95108c3e65654506f5521343c81/float64.quiet(x), 0x71d2c95108c3e65654506f5521343c81/float64.quiet(x), 0x71d2c95108c3e65654506f5521343c81/float64.quiet(x), 0x71d2c95108c3e65654506f5521343c81/float64.quiet(x), 0x71d2c95108c3e65654506f5521343c81/float64.F64{2146959360, 0}] : List<&2, 0x71d2c95108c3e65654506f5521343c81/float64.F64>}Guarantees: every one-argument function (exp, exp2, expm1, log, log2, log10, log1p, sin, cos, tan, arcsin, arccos, arctan, sinh, cosh, arcsinh, arccosh, arctanh, cbrt) of a NaN is that NaN with its quiet bit set; tanh of a NaN is the plain quiet NaN 0x7FF8000000000000. Why it matters: NumPy gives these bits on x86-64; a NaN must never turn into a number, and its payload survives exactly where NumPy keeps it.
law atan2_nan_x provedin MATH_PROOF.bendsource · line 26 · raw
@+y:0x71d2c95108c3e65654506f5521343c81/float64.F64 -> @+x:0x71d2c95108c3e65654506f5521343c81/float64.F64 -> @+h:{0x71d2c95108c3e65654506f5521343c81/float64.is_nan(x) == True{} : Bool} -> {0x71d2c95108c3e65654506f5521343c81/math.arctan2(y, x) == 0x71d2c95108c3e65654506f5521343c81/float64.quiet(x) : 0x71d2c95108c3e65654506f5521343c81/float64.F64}Guarantees: arctan2(y, x) with x NaN is x quieted, whatever y is. Why it matters: glibc's atan2 takes x's NaN first; the other order changes the NaN's bits.
law atan2_nan_y provedin MATH_PROOF.bendsource · line 34 · raw
@+y:0x71d2c95108c3e65654506f5521343c81/float64.F64 -> @+x:0x71d2c95108c3e65654506f5521343c81/float64.F64 -> @+hx:{0x71d2c95108c3e65654506f5521343c81/float64.is_nan(x) == False{} : Bool} -> @+hy:{0x71d2c95108c3e65654506f5521343c81/float64.is_nan(y) == True{} : Bool} -> {0x71d2c95108c3e65654506f5521343c81/math.arctan2(y, x) == 0x71d2c95108c3e65654506f5521343c81/float64.quiet(y) : 0x71d2c95108c3e65654506f5521343c81/float64.F64}Guarantees: arctan2(y, x) with x not NaN and y NaN is y quieted. Why it matters: the second step of the same order.
law special_values provedin MATH_PROOF.bendsource · line 46 · raw
{[0x71d2c95108c3e65654506f5521343c81/math.exp(0x71d2c95108c3e65654506f5521343c81/float64.F64{4293918720, 0}), 0x71d2c95108c3e65654506f5521343c81/math.exp(0x71d2c95108c3e65654506f5521343c81/float64.F64{2146435072, 0}), 0x71d2c95108c3e65654506f5521343c81/math.exp(0x71d2c95108c3e65654506f5521343c81/float64.F64{2147483648, 0}), 0x71d2c95108c3e65654506f5521343c81/math.expm1(0x71d2c95108c3e65654506f5521343c81/float64.F64{4293918720, 0}), 0x71d2c95108c3e65654506f5521343c81/math.log(0x71d2c95108c3e65654506f5521343c81/float64.F64{0, 0}), 0x71d2c95108c3e65654506f5521343c81/math.log(0x71d2c95108c3e65654506f5521343c81/float64.F64{3220176896, 0}), 0x71d2c95108c3e65654506f5521343c81/math.log10(0x71d2c95108c3e65654506f5521343c81/float64.F64{3220176896, 0}), 0x71d2c95108c3e65654506f5521343c81/math.log(0x71d2c95108c3e65654506f5521343c81/float64.F64{1072693248, 0}), 0x71d2c95108c3e65654506f5521343c81/math.log1p(0x71d2c95108c3e65654506f5521343c81/float64.F64{3220176896, 0}), 0x71d2c95108c3e65654506f5521343c81/math.sin(0x71d2c95108c3e65654506f5521343c81/float64.F64{2146435072, 0}), 0x71d2c95108c3e65654506f5521343c81/math.cos(0x71d2c95108c3e65654506f5521343c81/float64.F64{2147483648, 0}), 0x71d2c95108c3e65654506f5521343c81/math.tan(0x71d2c95108c3e65654506f5521343c81/float64.F64{2147483648, 0}), 0x71d2c95108c3e65654506f5521343c81/math.arcsin(0x71d2c95108c3e65654506f5521343c81/float64.F64{1073741824, 0}), 0x71d2c95108c3e65654506f5521343c81/math.arccos(0x71d2c95108c3e65654506f5521343c81/float64.F64{1072693248, 0}), 0x71d2c95108c3e65654506f5521343c81/math.arccosh(0x71d2c95108c3e65654506f5521343c81/float64.F64{1071644672, 0}), 0x71d2c95108c3e65654506f5521343c81/math.arctanh(0x71d2c95108c3e65654506f5521343c81/float64.F64{3220176896, 0}), 0x71d2c95108c3e65654506f5521343c81/math.sinh(0x71d2c95108c3e65654506f5521343c81/float64.F64{4293918720, 0}), 0x71d2c95108c3e65654506f5521343c81/math.cosh(0x71d2c95108c3e65654506f5521343c81/float64.F64{4293918720, 0}), 0x71d2c95108c3e65654506f5521343c81/math.tanh(0x71d2c95108c3e65654506f5521343c81/float64.from_i32(30)), 0x71d2c95108c3e65654506f5521343c81/math.tanh(0x71d2c95108c3e65654506f5521343c81/float64.F64{4293918720, 0}), 0x71d2c95108c3e65654506f5521343c81/math.cbrt(0x71d2c95108c3e65654506f5521343c81/float64.F64{2147483648, 0})] == [0x71d2c95108c3e65654506f5521343c81/float64.F64{0, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{2146435072, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{1072693248, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{3220176896, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{4293918720, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{4294443008, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{2146959360, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{0, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{4293918720, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{4294443008, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{1072693248, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{2147483648, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{2146959360, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{0, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{4294443008, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{4293918720, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{4293918720, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{2146435072, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{1072693248, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{3220176896, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{2147483648, 0}] : List<&2, 0x71d2c95108c3e65654506f5521343c81/float64.F64>}Guarantees: exp(-inf) = +0, exp(inf) = inf, exp(-0) = 1, expm1(-inf) = -1, log(+0) = -inf, log(-1) is x86's default NaN 0xFFF8..., log10(-1) is 0x7FF8... (glibc's log10 differs), log(1) = +0, log1p(-1) = -inf, sin(inf) = 0xFFF8..., cos(-0) = 1, tan(-0) = -0, arcsin(2) = 0x7FF8..., arccos(1) = +0, arccosh(0.5) = 0xFFF8..., arctanh(-1) = -inf, sinh(-inf) = -inf, cosh(-inf) = inf, tanh(30) = 1, tanh(-inf) = -1, cbrt(-0) = -0. Why it matters: these are the values C99 and NumPy fix by rule, not by rounding; each is a branch of its own.
law exact_exp_log provedin MATH_PROOF.bendsource · line 65 · raw
{[0x71d2c95108c3e65654506f5521343c81/math.exp2(0x71d2c95108c3e65654506f5521343c81/float64.from_i32(10)), 0x71d2c95108c3e65654506f5521343c81/math.exp2(0x71d2c95108c3e65654506f5521343c81/float64.from_i32(4294966222)), 0x71d2c95108c3e65654506f5521343c81/math.exp2(0x71d2c95108c3e65654506f5521343c81/float64.from_i32(4294966221)), 0x71d2c95108c3e65654506f5521343c81/math.log2(0x71d2c95108c3e65654506f5521343c81/float64.from_i32(1024)), 0x71d2c95108c3e65654506f5521343c81/math.log2(0x71d2c95108c3e65654506f5521343c81/float64.F64{0, 1}), 0x71d2c95108c3e65654506f5521343c81/math.log10(0x71d2c95108c3e65654506f5521343c81/float64.from_i32(1000)), 0x71d2c95108c3e65654506f5521343c81/math.log10(0x71d2c95108c3e65654506f5521343c81/float64.F64{1149300943, 105764242})] == [0x71d2c95108c3e65654506f5521343c81/float64.F64{1083179008, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{0, 1}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{0, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{1076101120, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{3230713856, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{1074266112, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{1077280768, 0}] : List<&2, 0x71d2c95108c3e65654506f5521343c81/float64.F64>}Guarantees: the exp and log answers that are exact come out exact: exp2(10) = 1024, exp2(-1074) is the smallest subnormal and exp2(-1075), exactly halfway to 0, rounds to 0 (ties to even); log2(1024) = 10, log2(2^-1074) = -1074, log10(1000) = 3, log10(1e22) = 22. Why it matters: an exact or halfway answer never settles by refining a ball, so each is found and computed exactly first; without that the evaluation would loop, and a halfway case could round the wrong way.
law exact_hypot provedin MATH_PROOF.bendsource · line 74 · raw
{[0x71d2c95108c3e65654506f5521343c81/math.hypot(0x71d2c95108c3e65654506f5521343c81/float64.from_i32(3), 0x71d2c95108c3e65654506f5521343c81/float64.from_i32(4)), 0x71d2c95108c3e65654506f5521343c81/math.hypot(0x71d2c95108c3e65654506f5521343c81/float64.from_i32(5), 0x71d2c95108c3e65654506f5521343c81/float64.from_i32(12))] == [0x71d2c95108c3e65654506f5521343c81/float64.F64{1075052544, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{1076494336, 0}] : List<&2, 0x71d2c95108c3e65654506f5521343c81/float64.F64>}Guarantees: hypot comes out exact when the answer is: hypot(3, 4) = 5, hypot(5, 12) = 13. Why it matters: hypot is always computed exactly (the integer square root of the exact sum of squares); a wrong shift there would be off for every input.
law atan2_zeros provedin MATH_PROOF.bendsource · line 81 · raw
{[0x71d2c95108c3e65654506f5521343c81/math.arctan2(0x71d2c95108c3e65654506f5521343c81/float64.F64{0, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{0, 0}), 0x71d2c95108c3e65654506f5521343c81/math.arctan2(0x71d2c95108c3e65654506f5521343c81/float64.F64{2147483648, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{0, 0}), 0x71d2c95108c3e65654506f5521343c81/math.arctan2(0x71d2c95108c3e65654506f5521343c81/float64.F64{0, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.from_i32(5)), 0x71d2c95108c3e65654506f5521343c81/math.arctan2(0x71d2c95108c3e65654506f5521343c81/float64.from_i32(1), 0x71d2c95108c3e65654506f5521343c81/float64.F64{2146435072, 0}), 0x71d2c95108c3e65654506f5521343c81/math.arctan2(0x71d2c95108c3e65654506f5521343c81/float64.F64{3220176896, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{2146435072, 0})] == [0x71d2c95108c3e65654506f5521343c81/float64.F64{0, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{2147483648, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{0, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{0, 0}, 0x71d2c95108c3e65654506f5521343c81/float64.F64{2147483648, 0}] : List<&2, 0x71d2c95108c3e65654506f5521343c81/float64.F64>}Guarantees: arctan2 of zeros and infinities that C99 fixes without pi: atan2(+0, +0) = +0, atan2(-0, +0) = -0, atan2(+0, 5) = +0, atan2(1, +inf) = +0, atan2(-1, +inf) = -0. Why it matters: the sign of these zeros is what tells the half-planes apart; a +0 for a -0 moves an angle by 2 pi in the next atan2.