TEXT_LAWS.bend source
TEXT_LAWS.bend on the hub · documented module
# The laws of bend-float64's text (text.bend), at literal inputs: the checker computes them (big-integer arithmetic# in the checker is slow, so the cases are chosen cheap; test/text_check.py compares a million each way with Python).# Gate: ./bend-check TEXT_PROOF.bend --verdictimport Baseimport ./float64.bend as Fimport ./text.bend as T# Guarantees: show lays numbers out as Python's repr: 0.1, 123.0 (a whole number keeps ".0"), 0.0001 and 1e-05 (the# switch to an exponent below 1e-4), 1000000000000000.0 and 1e+16 (the switch above 1e16), -2.5, -0.0, inf, -inf,# nan.# Why it matters: text written by show is read by people and by other programs next to Python's own output; a# different layout breaks comparisons and diffs.law show_layout: {[T.show(F.of_bits(1069128089, 2576980378)), T.show(F.of_bits(1079951360, 0)), T.show(F.of_bits(1058682594, 3944497965)), T.show(F.of_bits(1055193269, 2296604913)), T.show(F.of_bits(1124887541, 640942080)), T.show(F.of_bits(1128383353, 937459712)), T.show(F.of_bits(3221487616, 0)), T.show(F.of_bits(2147483648, 0)), T.show(F.of_bits(2146435072, 0)), T.show(F.of_bits(4293918720, 0)), T.show(F.of_bits(2146959360, 0))] == ["0.1", "123.0", "0.0001", "1e-05", "1000000000000000.0", "1e+16", "-2.5", "-0.0", "inf", "-inf", "nan"] : List<&2, String>}# Guarantees: show gives the fewest digits that read back to the same float64: 1/3 needs 16, 0.1 + 0.2 needs 17# (0.30000000000000004), 2.5e-07 needs 2; 1e+23 needs 1 because# 1e23 lies exactly halfway to the next float64 and this one's significand is even, so the halfway point reads back# to it.# Why it matters: fewer digits would read back as a different number; more would print 0.1 as# 0.1000000000000000055511151231257827.law show_shortest: {[T.show(F.of_bits(1070945621, 1431655765)), T.show(F.of_bits(1070805811, 858993460)), T.show(F.of_bits(1049675511, 2696277389)), T.show(F.of_bits(1152724226, 3353430774))] == ["0.3333333333333333", "0.30000000000000004", "2.5e-07", "1e+23"] : List<&2, String>}# Guarantees: parse reads "0.1", " -1_000.5\t" (whitespace, a sign, an underscore), "2.5e-7", "+inf", "-Infinity",# "nan" and "-nan" (x86's quiet NaN, with the sign), rounds "9007199254740993" (2^53 + 1, halfway) down to 2^53# and "9007199254740995" up to 2^53 + 4, "4503599627370496.5" down# to 2^52 and "4503599627370497.5" up to 2^52 + 2 (ties to even, through the division path), and gives inf for "1e400" and 0.0 for "1e-400".# Why it matters: these are Python's float() answers; a parser that rounded a halfway case up, or refused 1e400, would# read data differently from the program that wrote it.law parse_values: {[T.parse("0.1"), T.parse(" -1_000.5\t"), T.parse("2.5e-7"), T.parse("+inf"), T.parse("-Infinity"), T.parse("nan"), T.parse("-nan"), T.parse("9007199254740993"), T.parse("9007199254740995"), T.parse("1e400"), T.parse("1e-400"), T.parse("4503599627370496.5"), T.parse("4503599627370497.5")] == [Done{F.F64{1069128089, 2576980378}}, Done{F.F64{3230614528, 0}}, Done{F.F64{1049675511, 2696277389}}, Done{F.F64{2146435072, 0}}, Done{F.F64{4293918720, 0}}, Done{F.F64{2146959360, 0}}, Done{F.F64{4294443008, 0}}, Done{F.F64{1128267776, 0}}, Done{F.F64{1128267776, 2}}, Done{F.F64{2146435072, 0}}, Done{F.F64{0, 0}}, Done{F.F64{1127219200, 0}}, Done{F.F64{1127219200, 2}}] : List<&2, Result<&2, &2, String, F.F64>>}# Guarantees: parse refuses what float() refuses, with float()'s message: "1__0", "_1", "1_", "1e", ".", "", "0x10",# "infinit", "1.2.3", "--1".# Why it matters: a typo must come back as an error the caller can report, never as a number.law parse_refuses: {[T.parse("1__0"), T.parse("_1"), T.parse("1_"), T.parse("1e"), T.parse("."), T.parse(""), T.parse("0x10"), T.parse("infinit"), T.parse("1.2.3"), T.parse("--1")] == [Fail{"could not convert string to float: '1__0'"}, Fail{"could not convert string to float: '_1'"}, Fail{"could not convert string to float: '1_'"}, Fail{"could not convert string to float: '1e'"}, Fail{"could not convert string to float: '.'"}, Fail{"could not convert string to float: ''"}, Fail{"could not convert string to float: '0x10'"}, Fail{"could not convert string to float: 'infinit'"}, Fail{"could not convert string to float: '1.2.3'"}, Fail{"could not convert string to float: '--1'"}] : List<&2, Result<&2, &2, String, F.F64>>}