MODE_LAWS.bend source
MODE_LAWS.bend on the hub · documented module
# The laws of bend-float64's rounding modes in arithmetic (float64.bend); MODE_MATH_LAWS.bend has conversions, parsing# and the math functions. 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_PROOF.bend --verdictimport Baseimport ./wide.bend as Wimport ./float64.bend as Fimport ./text.bend as T# Guarantees: add, sub, mul, div, sqrt, fma, to_f32_bits, from_u64, from_i64 and parse without a mode are their mode# versions rounding to nearest, ties to even, at every input.# Why it matters: existing callers use the plain names and must keep IEEE's default rounding exactly; a separate# default implementation could drift from the mode versions that the mode tests check.law default_mode_arith: for +a: F.F64 for +b: F.F64 for +c: F.F64 for +w: W.W64 for +text: String {[F.add(a, b), F.sub(a, b), F.mul(a, b), F.div(a, b), F.sqrt(a), F.fma(a, b, c), F.from_u64(w), F.from_i64(w)] == [F.add_m(F.RNearEven{}, a, b), F.sub_m(F.RNearEven{}, a, b), F.mul_m(F.RNearEven{}, a, b), F.div_m(F.RNearEven{}, a, b), F.sqrt_m(F.RNearEven{}, a), F.fma_m(F.RNearEven{}, a, b, c), F.from_u64_m(F.RNearEven{}, w), F.from_i64_m(F.RNearEven{}, w)] : List<&2, F.F64>} & {F.to_f32_bits(a) == F.to_f32_bits_m(F.RNearEven{}, a) : U32} & {T.parse(text) == T.parse_m(F.RNearEven{}, text) : Result<&2, &2, String, F.F64>}# Guarantees: the five modes on one sum, 1 + 2^-60: up gives the next float64 above 1, the other four give 1; on the# tie 1 + 2^-53, nearest-even gives 1 and nearest-away the next float64 above 1; the square root of 2 rounds down# to 0x3FF6A09E667F3BCC down and to ...BCD up; 1 + 3 * 2^-54 rounds toward zero to 1 and to nearest to the next# float64 above 1; -1 - 2^-60 rounds up (toward zero) to -1 and down to the next float64 below -1.# Why it matters: each mode's direction is a separate rule in the rounding step; these inputs fail if any mode# rounds the wrong way, or two modes are swapped.law mode_directions: {[F.add_m(F.RNearEven{}, F.F64{1072693248, 0}, F.F64{1009778688, 0}), F.add_m(F.RNearAway{}, F.F64{1072693248, 0}, F.F64{1009778688, 0}), F.add_m(F.RZero{}, F.F64{1072693248, 0}, F.F64{1009778688, 0}), F.add_m(F.RUp{}, F.F64{1072693248, 0}, F.F64{1009778688, 0}), F.add_m(F.RDown{}, F.F64{1072693248, 0}, F.F64{1009778688, 0}), F.add_m(F.RNearEven{}, F.F64{1072693248, 0}, F.F64{1017118720, 0}), F.add_m(F.RNearAway{}, F.F64{1072693248, 0}, F.F64{1017118720, 0}), F.sqrt_m(F.RDown{}, F.F64{1073741824, 0}), F.sqrt_m(F.RUp{}, F.F64{1073741824, 0}), F.add_m(F.RZero{}, F.F64{1072693248, 0}, F.F64{1017643008, 0}), F.add_m(F.RNearEven{}, F.F64{1072693248, 0}, F.F64{1017643008, 0}), F.add_m(F.RUp{}, F.F64{3220176896, 0}, F.F64{3157262336, 0}), F.add_m(F.RDown{}, F.F64{3220176896, 0}, F.F64{3157262336, 0})] == [F.F64{1072693248, 0}, F.F64{1072693248, 0}, F.F64{1072693248, 0}, F.F64{1072693248, 1}, F.F64{1072693248, 0}, F.F64{1072693248, 0}, F.F64{1072693248, 1}, F.F64{1073127582, 1719614412}, F.F64{1073127582, 1719614413}, F.F64{1072693248, 0}, F.F64{1072693248, 1}, F.F64{3220176896, 0}, F.F64{3220176896, 1}] : List<&2, F.F64>}# Guarantees: an exact zero sum of opposite numbers is -0 rounding down and +0 in the other modes (1 + -1).# Why it matters: IEEE 754 fixes this sign; a +0 rounding down would let 1/x jump from -inf to +inf.law mode_zero_sum: {[F.add_m(F.RDown{}, F.F64{1072693248, 0}, F.F64{3220176896, 0}), F.add_m(F.RUp{}, F.F64{1072693248, 0}, F.F64{3220176896, 0}), F.add_m(F.RZero{}, F.F64{1072693248, 0}, F.F64{3220176896, 0}), F.sub_m(F.RDown{}, F.F64{1072693248, 0}, F.F64{1072693248, 0})] == [F.F64{2147483648, 0}, F.F64{0, 0}, F.F64{0, 0}, F.F64{2147483648, 0}] : List<&2, F.F64>}# Guarantees: past the largest float64 a result is infinity rounding to nearest, the largest finite number rounding# toward zero, and infinity or the largest finite number by its sign rounding up or down (max * 2 and -max * 2); below# half the smallest subnormal, nearest-even gives 0, nearest-away and up give the smallest subnormal (2^-1074 * 0.5).# Why it matters: IEEE 754 fixes overflow and underflow per mode; a directed mode that overflows to infinity, or# flushes to zero rounding up, breaks interval arithmetic built on these modes.law mode_range: {[F.mul_m(F.RNearEven{}, F.F64{2146435071, 4294967295}, F.F64{1073741824, 0}), F.mul_m(F.RZero{}, F.F64{2146435071, 4294967295}, F.F64{1073741824, 0}), F.mul_m(F.RUp{}, F.F64{2146435071, 4294967295}, F.F64{1073741824, 0}), F.mul_m(F.RDown{}, F.F64{2146435071, 4294967295}, F.F64{1073741824, 0}), F.mul_m(F.RUp{}, F.F64{4293918719, 4294967295}, F.F64{1073741824, 0}), F.mul_m(F.RDown{}, F.F64{4293918719, 4294967295}, F.F64{1073741824, 0}), F.mul_m(F.RNearEven{}, F.F64{0, 1}, F.F64{1071644672, 0}), F.mul_m(F.RNearAway{}, F.F64{0, 1}, F.F64{1071644672, 0}), F.mul_m(F.RUp{}, F.F64{0, 1}, F.F64{1071644672, 0}), F.mul_m(F.RDown{}, F.F64{0, 1}, F.F64{1071644672, 0})] == [F.F64{2146435072, 0}, F.F64{2146435071, 4294967295}, F.F64{2146435072, 0}, F.F64{2146435071, 4294967295}, F.F64{4293918719, 4294967295}, F.F64{4293918720, 0}, F.F64{0, 0}, F.F64{0, 1}, F.F64{0, 1}, F.F64{0, 0}] : List<&2, F.F64>}