~/bend-docscommunity

MODE_MATH_LAWS.bend source

MODE_MATH_LAWS.bend on the hub · documented module

# The laws of bend-float64's rounding modes in conversions, parsing and the math functions (float64.bend, text.bend,# math.bend); MODE_LAWS.bend has arithmetic. The default functions are proved for every input to be their mode# versions rounding to nearest (ties to even). The five modes' answers are pinned at literal inputs that tell the# modes apart; the exhaustive comparisons are tests (test/mode_check.py against exact fractions and the CPU's own# modes, test/parse_mode_check.py, test/math_mode_check.py against mpmath).# Gate: ./bend-check MODE_MATH_PROOF.bend --verdictimport Baseimport ./wide.bend as Wimport ./float64.bend as Fimport ./text.bend as Timport ./math.bend as M# Guarantees: each math function without a mode is its mode version rounding to nearest, ties to even, at every input.# Why it matters: the plain names are the ones most callers use; they must be exactly IEEE's default rounding, not a#   second implementation that could drift from the mode versions.law default_mode_math:  for +x: F.F64  for +y: F.F64  {[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.tanh(x), M.arcsinh(x), M.arccosh(x), M.arctanh(x), M.cbrt(x),    M.arctan2(y, x), M.power(x, y), M.hypot(x, y)]    == [M.exp_m(F.RNearEven{}, x), M.exp2_m(F.RNearEven{}, x), M.expm1_m(F.RNearEven{}, x), M.log_m(F.RNearEven{}, x),        M.log2_m(F.RNearEven{}, x), M.log10_m(F.RNearEven{}, x), M.log1p_m(F.RNearEven{}, x), M.sin_m(F.RNearEven{}, x),        M.cos_m(F.RNearEven{}, x), M.tan_m(F.RNearEven{}, x), M.arcsin_m(F.RNearEven{}, x), M.arccos_m(F.RNearEven{}, x),        M.arctan_m(F.RNearEven{}, x), M.sinh_m(F.RNearEven{}, x), M.cosh_m(F.RNearEven{}, x), M.tanh_m(F.RNearEven{}, x),        M.arcsinh_m(F.RNearEven{}, x), M.arccosh_m(F.RNearEven{}, x), M.arctanh_m(F.RNearEven{}, x), M.cbrt_m(F.RNearEven{}, x),        M.arctan2_m(F.RNearEven{}, y, x), M.power_m(F.RNearEven{}, x, y), M.hypot_m(F.RNearEven{}, x, y)] : List<&2, F.F64>}# Guarantees: conversions and parsing round by the mode too: 1 + 2^-30 to float32 is 0x3F800001 rounding up and#   0x3F800000 rounding down, and -(1 + 2^-30) rounding up is -1 (0xBF800000); "0.1" parses to 0x3FB999999999999A#   rounding up and ...99 rounding down; "1e400" to the largest finite number toward zero; "1e-400" to the smallest#   subnormal rounding up.# Why it matters: a value read or narrowed in a directed mode must stay on the mode's side of the true value, or a#   bound computed from it is wrong.law mode_convert:  {[F.to_f32_bits_m(F.RUp{}, F.F64{1072693248, 4}), F.to_f32_bits_m(F.RDown{}, F.F64{1072693248, 4}), F.to_f32_bits_m(F.RUp{}, F.F64{3220176896, 4})]    == [1065353217, 1065353216, 3212836864] : List<&2, U32>}  & {[T.parse_m(F.RUp{}, "0.1"), T.parse_m(F.RDown{}, "0.1"), T.parse_m(F.RZero{}, "1e400"), T.parse_m(F.RUp{}, "1e-400")]    == [Done{F.F64{1069128089, 2576980378}}, Done{F.F64{1069128089, 2576980377}}, Done{F.F64{2146435071, 4294967295}}, Done{F.F64{0, 1}}] : List<&2, Result<&2, &2, String, F.F64>>}# Guarantees: a math answer just off a float64 rounds by the mode: exp(2^-100) up is the next float64 above 1;#   exp(-2^-100) down is the one below 1; sin(2^-1074) toward zero is +0 and up is 2^-1074; tan(2^-1074) up is 2^-1073;#   cos(2^-100) down is just below 1; tanh(30) toward zero is just below 1 and up is 1; tanh(-30) up is just above -1;#   expm1(-50) up is just above -1; power(3, 2^-100) down is 1 and up just above; power(0.5, 2^-100) down is just#   below 1; hypot(1, 2^-100) up is just above 1; arctan2(2^-100, 1) toward zero is just below 2^-100.# Why it matters: these answers are never computed by refining a ball (no precision settles them), so each is a rule#   of its own; a rule pointing the wrong way is wrong in every directed mode for a whole range of inputs.law mode_math_near:  {[M.exp_m(F.RUp{}, F.F64{967835648, 0}), M.exp_m(F.RDown{}, F.F64{3115319296, 0}), M.sin_m(F.RZero{}, F.F64{0, 1}), M.sin_m(F.RUp{}, F.F64{0, 1}), M.tan_m(F.RUp{}, F.F64{0, 1}),    M.cos_m(F.RDown{}, F.F64{967835648, 0}), M.tanh_m(F.RZero{}, F.from_i32(30)), M.tanh_m(F.RUp{}, F.from_i32(30)), M.tanh_m(F.RUp{}, F.from_i32(4294967266)),    M.expm1_m(F.RUp{}, F.from_i32(4294967246)), M.power_m(F.RDown{}, F.from_i32(3), F.F64{967835648, 0}), M.power_m(F.RUp{}, F.from_i32(3), F.F64{967835648, 0}),    M.power_m(F.RDown{}, F.F64{1071644672, 0}, F.F64{967835648, 0}), M.hypot_m(F.RUp{}, F.F64{1072693248, 0}, F.F64{967835648, 0}), M.arctan2_m(F.RZero{}, F.F64{967835648, 0}, F.F64{1072693248, 0})]    == [F.F64{1072693248, 1}, F.F64{1072693247, 4294967295}, F.F64{0, 0}, F.F64{0, 1}, F.F64{0, 2}, F.F64{1072693247, 4294967295}, F.F64{1072693247, 4294967295}, F.F64{1072693248, 0}, F.F64{3220176895, 4294967295}, F.F64{3220176895, 4294967295}, F.F64{1072693248, 0}, F.F64{1072693248, 1}, F.F64{1072693247, 4294967295}, F.F64{1072693248, 1}, F.F64{967835647, 4294967295}] : List<&2, F.F64>}# Guarantees: math answers past the range follow the mode: exp(1000) toward zero is the largest finite number and up#   is infinity; exp(-1000) up is the smallest subnormal; exp2(-1075), exactly half of it, is 0 to nearest-even and#   the smallest subnormal to nearest-away.# Why it matters: overflow and underflow in the math functions are found before any evaluation, so the mode must be#   applied there by hand.law mode_math_range:  {[M.exp_m(F.RZero{}, F.from_i32(1000)), M.exp_m(F.RUp{}, F.from_i32(1000)), M.exp_m(F.RUp{}, F.from_i32(4294966296)),    M.exp2_m(F.RNearEven{}, F.from_i32(4294966221)), M.exp2_m(F.RNearAway{}, F.from_i32(4294966221))]    == [F.F64{2146435071, 4294967295}, F.F64{2146435072, 0}, F.F64{0, 1}, F.F64{0, 0}, F.F64{0, 1}] : List<&2, F.F64>}# Guarantees: an exact answer is exact in every mode: hypot(5, 12) rounding up is 13 and exp2(10) rounding down is#   1024.# Why it matters: exact answers are computed by their own paths, never by a ball (which could not settle on them in#   a directed mode); a mode applied there must not move a value that needs no rounding.law mode_exact:  {[M.hypot_m(F.RUp{}, F.from_i32(5), F.from_i32(12)), M.exp2_m(F.RDown{}, F.from_i32(10))] == [F.F64{1076494336, 0}, F.F64{1083179008, 0}] : List<&2, F.F64>}