MATH_LAWS.bend source
MATH_LAWS.bend on the hub · documented module
# 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 --verdictimport Baseimport ./float64.bend as Fimport ./math.bend as M# 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 unary_nan: for +x: F.F64 for +h: {F.is_nan(x) == True{} : Bool} {[M.exp(x), M.exp2(x), M.expm1(x), M.log(x), M.log2(x), M.log10(x), M.log1p(x), M.sin(x), M.cos(x), M.tan(x), M.arcsin(x), M.arccos(x), M.arctan(x), M.sinh(x), M.cosh(x), M.arcsinh(x), M.arccosh(x), M.arctanh(x), M.cbrt(x), M.tanh(x)] == [F.quiet(x), F.quiet(x), F.quiet(x), F.quiet(x), F.quiet(x), F.quiet(x), F.quiet(x), F.quiet(x), F.quiet(x), F.quiet(x), F.quiet(x), F.quiet(x), F.quiet(x), F.quiet(x), F.quiet(x), F.quiet(x), F.quiet(x), F.quiet(x), F.quiet(x), F.F64{2146959360, 0}] : List<&2, F.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_x: for +y: F.F64 for +x: F.F64 for +h: {F.is_nan(x) == True{} : Bool} {M.arctan2(y, x) == F.quiet(x) : F.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 atan2_nan_y: for +y: F.F64 for +x: F.F64 for +hx: {F.is_nan(x) == False{} : Bool} for +hy: {F.is_nan(y) == True{} : Bool} {M.arctan2(y, x) == F.quiet(y) : F.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 special_values: {[M.exp(F.F64{4293918720, 0}), M.exp(F.F64{2146435072, 0}), M.exp(F.F64{2147483648, 0}), M.expm1(F.F64{4293918720, 0}), M.log(F.F64{0, 0}), M.log(F.F64{3220176896, 0}), M.log10(F.F64{3220176896, 0}), M.log(F.F64{1072693248, 0}), M.log1p(F.F64{3220176896, 0}), M.sin(F.F64{2146435072, 0}), M.cos(F.F64{2147483648, 0}), M.tan(F.F64{2147483648, 0}), M.arcsin(F.F64{1073741824, 0}), M.arccos(F.F64{1072693248, 0}), M.arccosh(F.F64{1071644672, 0}), M.arctanh(F.F64{3220176896, 0}), M.sinh(F.F64{4293918720, 0}), M.cosh(F.F64{4293918720, 0}), M.tanh(F.from_i32(30)), M.tanh(F.F64{4293918720, 0}), M.cbrt(F.F64{2147483648, 0})] == [F.F64{0, 0}, F.F64{2146435072, 0}, F.F64{1072693248, 0}, F.F64{3220176896, 0}, F.F64{4293918720, 0}, F.F64{4294443008, 0}, F.F64{2146959360, 0}, F.F64{0, 0}, F.F64{4293918720, 0}, F.F64{4294443008, 0}, F.F64{1072693248, 0}, F.F64{2147483648, 0}, F.F64{2146959360, 0}, F.F64{0, 0}, F.F64{4294443008, 0}, F.F64{4293918720, 0}, F.F64{4293918720, 0}, F.F64{2146435072, 0}, F.F64{1072693248, 0}, F.F64{3220176896, 0}, F.F64{2147483648, 0}] : List<&2, F.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_exp_log: {[M.exp2(F.from_i32(10)), M.exp2(F.from_i32(4294966222)), M.exp2(F.from_i32(4294966221)), M.log2(F.from_i32(1024)), M.log2(F.F64{0, 1}), M.log10(F.from_i32(1000)), M.log10(F.F64{1149300943, 105764242})] == [F.F64{1083179008, 0}, F.F64{0, 1}, F.F64{0, 0}, F.F64{1076101120, 0}, F.F64{3230713856, 0}, F.F64{1074266112, 0}, F.F64{1077280768, 0}] : List<&2, F.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 exact_hypot: {[M.hypot(F.from_i32(3), F.from_i32(4)), M.hypot(F.from_i32(5), F.from_i32(12))] == [F.F64{1075052544, 0}, F.F64{1076494336, 0}] : List<&2, F.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.law atan2_zeros: {[M.arctan2(F.F64{0, 0}, F.F64{0, 0}), M.arctan2(F.F64{2147483648, 0}, F.F64{0, 0}), M.arctan2(F.F64{0, 0}, F.from_i32(5)), M.arctan2(F.from_i32(1), F.F64{2146435072, 0}), M.arctan2(F.F64{3220176896, 0}, F.F64{2146435072, 0})] == [F.F64{0, 0}, F.F64{2147483648, 0}, F.F64{0, 0}, F.F64{0, 0}, F.F64{2147483648, 0}] : List<&2, F.F64>}