~/bend-docscommunity

TEXT_LAWS.bend open laws/TODOs

raw source on the hub · import bend-float64@0.1.6.0/TEXT_LAWS.bend as TEXT_LAWS

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 --verdict

3 imports
import Base
import ./float64.bend as F
import ./text.bend as T

Laws

law show_layout provedin TEXT_PROOF.bendsource · line 13 · raw

{[0x71d2c95108c3e65654506f5521343c81/text.show(0x71d2c95108c3e65654506f5521343c81/float64.of_bits(1069128089, 2576980378)), 0x71d2c95108c3e65654506f5521343c81/text.show(0x71d2c95108c3e65654506f5521343c81/float64.of_bits(1079951360, 0)), 0x71d2c95108c3e65654506f5521343c81/text.show(0x71d2c95108c3e65654506f5521343c81/float64.of_bits(1058682594, 3944497965)), 0x71d2c95108c3e65654506f5521343c81/text.show(0x71d2c95108c3e65654506f5521343c81/float64.of_bits(1055193269, 2296604913)), 0x71d2c95108c3e65654506f5521343c81/text.show(0x71d2c95108c3e65654506f5521343c81/float64.of_bits(1124887541, 640942080)), 0x71d2c95108c3e65654506f5521343c81/text.show(0x71d2c95108c3e65654506f5521343c81/float64.of_bits(1128383353, 937459712)), 0x71d2c95108c3e65654506f5521343c81/text.show(0x71d2c95108c3e65654506f5521343c81/float64.of_bits(3221487616, 0)), 0x71d2c95108c3e65654506f5521343c81/text.show(0x71d2c95108c3e65654506f5521343c81/float64.of_bits(2147483648, 0)), 0x71d2c95108c3e65654506f5521343c81/text.show(0x71d2c95108c3e65654506f5521343c81/float64.of_bits(2146435072, 0)), 0x71d2c95108c3e65654506f5521343c81/text.show(0x71d2c95108c3e65654506f5521343c81/float64.of_bits(4293918720, 0)), 0x71d2c95108c3e65654506f5521343c81/text.show(0x71d2c95108c3e65654506f5521343c81/float64.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 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_shortest provedin TEXT_PROOF.bendsource · line 26 · raw

{[0x71d2c95108c3e65654506f5521343c81/text.show(0x71d2c95108c3e65654506f5521343c81/float64.of_bits(1070945621, 1431655765)), 0x71d2c95108c3e65654506f5521343c81/text.show(0x71d2c95108c3e65654506f5521343c81/float64.of_bits(1070805811, 858993460)), 0x71d2c95108c3e65654506f5521343c81/text.show(0x71d2c95108c3e65654506f5521343c81/float64.of_bits(1049675511, 2696277389)), 0x71d2c95108c3e65654506f5521343c81/text.show(0x71d2c95108c3e65654506f5521343c81/float64.of_bits(1152724226, 3353430774))] == ["0.3333333333333333", "0.30000000000000004", "2.5e-07", "1e+23"] : 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 parse_values provedin TEXT_PROOF.bendsource · line 37 · raw

{[0x71d2c95108c3e65654506f5521343c81/text.parse("0.1"), 0x71d2c95108c3e65654506f5521343c81/text.parse(" -1_000.5\t"), 0x71d2c95108c3e65654506f5521343c81/text.parse("2.5e-7"), 0x71d2c95108c3e65654506f5521343c81/text.parse("+inf"), 0x71d2c95108c3e65654506f5521343c81/text.parse("-Infinity"), 0x71d2c95108c3e65654506f5521343c81/text.parse("nan"), 0x71d2c95108c3e65654506f5521343c81/text.parse("-nan"), 0x71d2c95108c3e65654506f5521343c81/text.parse("9007199254740993"), 0x71d2c95108c3e65654506f5521343c81/text.parse("9007199254740995"), 0x71d2c95108c3e65654506f5521343c81/text.parse("1e400"), 0x71d2c95108c3e65654506f5521343c81/text.parse("1e-400"), 0x71d2c95108c3e65654506f5521343c81/text.parse("4503599627370496.5"), 0x71d2c95108c3e65654506f5521343c81/text.parse("4503599627370497.5")] == [Done{0x71d2c95108c3e65654506f5521343c81/float64.F64{1069128089, 2576980378}}, Done{0x71d2c95108c3e65654506f5521343c81/float64.F64{3230614528, 0}}, Done{0x71d2c95108c3e65654506f5521343c81/float64.F64{1049675511, 2696277389}}, Done{0x71d2c95108c3e65654506f5521343c81/float64.F64{2146435072, 0}}, Done{0x71d2c95108c3e65654506f5521343c81/float64.F64{4293918720, 0}}, Done{0x71d2c95108c3e65654506f5521343c81/float64.F64{2146959360, 0}}, Done{0x71d2c95108c3e65654506f5521343c81/float64.F64{4294443008, 0}}, Done{0x71d2c95108c3e65654506f5521343c81/float64.F64{1128267776, 0}}, Done{0x71d2c95108c3e65654506f5521343c81/float64.F64{1128267776, 2}}, Done{0x71d2c95108c3e65654506f5521343c81/float64.F64{2146435072, 0}}, Done{0x71d2c95108c3e65654506f5521343c81/float64.F64{0, 0}}, Done{0x71d2c95108c3e65654506f5521343c81/float64.F64{1127219200, 0}}, Done{0x71d2c95108c3e65654506f5521343c81/float64.F64{1127219200, 2}}] : List<&2, Result<&2, &2, String, 0x71d2c95108c3e65654506f5521343c81/float64.F64>>}

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_refuses provedin TEXT_PROOF.bendsource · line 49 · raw

{[0x71d2c95108c3e65654506f5521343c81/text.parse("1__0"), 0x71d2c95108c3e65654506f5521343c81/text.parse("_1"), 0x71d2c95108c3e65654506f5521343c81/text.parse("1_"), 0x71d2c95108c3e65654506f5521343c81/text.parse("1e"), 0x71d2c95108c3e65654506f5521343c81/text.parse("."), 0x71d2c95108c3e65654506f5521343c81/text.parse(""), 0x71d2c95108c3e65654506f5521343c81/text.parse("0x10"), 0x71d2c95108c3e65654506f5521343c81/text.parse("infinit"), 0x71d2c95108c3e65654506f5521343c81/text.parse("1.2.3"), 0x71d2c95108c3e65654506f5521343c81/text.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, 0x71d2c95108c3e65654506f5521343c81/float64.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.