text.bend checks
raw source on the hub · import bend-float64@0.1.5.0/text.bend as Text
bend-float64's text: show prints the shortest decimal that reads back to the same float64, laid out as Python's repr (0.1, 1e+16, 1.5e-07, 123.0, -0.0, inf, nan); parse reads Python's float() grammar and rounds correctly (to nearest, ties to even), however many digits the text has. Both are exact: big-integer arithmetic, no approximation. Pure.
4 imports
import Base import ./wide.bend as W import ./big.bend as B import ./float64.bend as F
Types
type Digits source · line 13 · raw
Data
the digits d1 d2 .. dn and the decimal point's place: the value is 0.d1d2..dn * 10^decpt (decpt a signed word)
Digits@ds:List<&2, U32> -> @decpt:U32 -> Digits
type DR source · line 16 · raw
Data
DR@d:U32 -> @r:List<&2, U32> -> DR
type S4 source · line 20 · raw
Data
s, 2s, 4s and 8s, made once by doubling
S4@s1:List<&2, U32> -> @s2:List<&2, U32> -> @s4:List<&2, U32> -> @s8:List<&2, U32> -> S4
type BD source · line 73 · raw
Data
BD@r:List<&2, U32> -> @s:List<&2, U32> -> @mp:List<&2, U32> -> @mm:List<&2, U32> -> @k:U32 -> BD
type Form source · line 204 · raw
Data
0, 1: exponent form, a leading 0., digits then the point
FSciForm
FLeadForm
FPosForm
type PS source · line 283 · raw
Data
the number's text so far: the state, its digits (last first), how many came after the point, the exponent (clamped at 10^8) and its sign. States: 0 nothing, 1 an integer digit, 2 an integer _, 3 a point first, 4 a point after digits, 5 a fraction digit, 6 a fraction _, 7 e, 8 the exponent's sign, 9 an exponent digit, 10 an exponent _, 11 not a number
PS@st:U32 -> @ds:List<&2, U32> -> @nfrac:U32 -> @ex:U32 -> @exneg:Bool -> PS
type DK source · line 305 · raw
Data
where a digit goes, from the state it arrives in
DIntDK
DFracDK
DExpDK
DBadDK
type CC source · line 326 · raw
Data
a character's class
CDigit@d:U32 -> CC
CUnderCC
CPointCC
CECC
CSign@neg:Bool -> CC
COtherCC
type TZ source · line 369 · raw
Data
trailing zeros (at the front of the last-first list) dropped and counted
TZ@ds:List<&2, U32> -> @n:U32 -> TZ
type QW source · line 393 · raw
Data
QW@q:0xe8796d1f170a8cd1a32ab698bd6d4d3d/wide.W64 -> @r:List<&2, U32> -> QW
type Range source · line 440 · raw
Data
what the size of the number decides: zero digits, past the largest float64, below half the smallest
RZeroRange
RInfRange
RTinyRange
RExactRange
type Spelt source · line 516 · raw
Data
what the text after the sign spells: infinity, nan, or a number to read
WInfSpelt
WNanSpelt
WNumSpelt
type Sgn source · line 538 · raw
Data
a leading sign: 45 is -, 43 is +
SMinusSgn
SPlusSgn
SNoneSgn
Definitions
def s4_of source · line 23 · raw
@+s:List<&2, U32> -> S4
def dr_step source · line 29 · raw
@x:DR -> @+t:List<&2, U32> -> @+w:U32 -> DR
one step of r / s: t = s * w taken from r when it fits
def divrem source · line 36 · raw
@+r:List<&2, U32> -> @m:S4 -> DR
r / s and r mod s, for r < 16 s: four steps of binary long division
def bound_ok source · line 41 · raw
@ok:Bool -> @+c:Cmp -> Bool
def last_both source · line 49 · raw
@+d:U32 -> @+r:List<&2, U32> -> @+s:List<&2, U32> -> U32
both ends reachable: the nearer digit; halfway, the even one
def gen_pick source · line 53 · raw
@lo:Bool -> @hi:Bool -> @+d:U32 -> @+r:List<&2, U32> -> @+s:List<&2, U32> -> @more:(@_:Unit -> List<&2, U32>) -> List<&2, U32>
def gen source · line 65 · raw
@n:Nat -> @q:DR -> @+s:List<&2, U32> -> @+m:S4 -> @+mp:List<&2, U32> -> @+mm:List<&2, U32> -> @+ok:Bool -> List<&2, U32>
one digit a step: the next digit, and whether the digits so far, ending here, are within the rounding interval
def fixup source · line 77 · raw
@n:Nat -> @b:BD -> @+ok:Bool -> BD
raise k while r + m+ reaches s (past the decimal point's place)
def is_neg source · line 84 · raw
@+x:U32 -> Bool
def scale_go source · line 88 · raw
@neg:Bool -> @+r:List<&2, U32> -> @+s:List<&2, U32> -> @+mp:List<&2, U32> -> @+mm:List<&2, U32> -> @+k:U32 -> BD
scale by the estimate 10^k
def scale source · line 95 · raw
@b:BD -> BD
def ceil_log10_2 source · line 101 · raw
@+x:U32 -> U32
ceil(x * log10(2)) for a signed x, |x| < 1650
def digits_go source · line 105 · raw
@b:BD -> @+ok:Bool -> Digits
def setup_go source · line 111 · raw
@neg:Bool -> @asym:Bool -> @+f:List<&2, U32> -> @+be:U32 -> @+k:U32 -> BD
def setup source · line 124 · raw
@+m:0xe8796d1f170a8cd1a32ab698bd6d4d3d/wide.W64 -> @+e:U32 -> @+k:U32 -> BD
m * 2^be, be = e - 1075: r / s is the value, m+ / s and m- / s half the gaps to its neighbours (the lower gap is half as wide when m is a power of two above the smallest exponent)
def shortest source · line 131 · raw
@+e:U32 -> @+m:0xe8796d1f170a8cd1a32ab698bd6d4d3d/wide.W64 -> Digits
the shortest digits of the finite nonzero m * 2^(e - 1075) that read back to it
def digit_chars source · line 137 · raw
@ds:List<&2, U32> -> @rest:String -> String
def zero_chars source · line 144 · raw
@n:Nat -> @rest:String -> String
def take_d source · line 151 · raw
@n:Nat -> @xs:List<&2, U32> -> List<&2, U32>
def drop_d source · line 160 · raw
@n:Nat -> @xs:List<&2, U32> -> List<&2, U32>
def len_d source · line 169 · raw
@xs:List<&2, U32> -> U32
def exp_text source · line 176 · raw
@+x:U32 -> String
def frac_part source · line 181 · raw
@ds:List<&2, U32> -> String
def sci source · line 188 · raw
@ds:List<&2, U32> -> @+decpt:U32 -> String
def fixed_pos source · line 196 · raw
@inside:Bool -> @+ds:List<&2, U32> -> @+n:U32 -> @+decpt:U32 -> String
0 < decpt < 17
def form_of source · line 209 · raw
@+decpt:U32 -> Form
def layout_f source · line 213 · raw
@f:Form -> @+ds:List<&2, U32> -> @+decpt:U32 -> String
def layout source · line 222 · raw
@d:Digits -> String
def show_c source · line 227 · raw
@c:0xe8796d1f170a8cd1a32ab698bd6d4d3d/float64.Cls -> String
def show source · line 239 · raw
@x:0xe8796d1f170a8cd1a32ab698bd6d4d3d/float64.F64 -> String
the shortest text that reads back to x, as Python's repr(x)
def codes source · line 244 · raw
@s:String -> List<&2, U32>
def is_space source · line 252 · raw
@+c:U32 -> Bool
the whitespace Python's float() strips: str.isspace without 28 .. 31
def strip_lead source · line 257 · raw
@xs:List<&2, U32> -> List<&2, U32>
def strip source · line 264 · raw
@xs:List<&2, U32> -> List<&2, U32>
def lower source · line 267 · raw
@+c:U32 -> U32
def word_is source · line 270 · raw
@xs:List<&2, U32> -> @w:List<&2, U32> -> Bool
def st_digit source · line 286 · raw
@+st:U32 -> U32
def st_under source · line 289 · raw
@+st:U32 -> U32
def st_point source · line 292 · raw
@+st:U32 -> U32
def st_e source · line 295 · raw
@+st:U32 -> U32
def st_sign source · line 298 · raw
@+st:U32 -> U32
def is_dig source · line 301 · raw
@+c:U32 -> Bool
def digit_kind source · line 311 · raw
@+st:U32 -> DK
def push_digit source · line 314 · raw
@k:DK -> @ds:List<&2, U32> -> @+nfrac:U32 -> @+ex:U32 -> @+exneg:Bool -> @+d:U32 -> PS
def class_of source · line 334 · raw
@+c:U32 -> CC
def step_c source · line 341 · raw
@k:CC -> @p:PS -> PS
def step source · line 356 · raw
@p:PS -> @+c:U32 -> PS
def run source · line 359 · raw
@xs:List<&2, U32> -> @p:PS -> PS
def drop_tz source · line 372 · raw
@xs:List<&2, U32> -> @+n:U32 -> TZ
def drop_lz source · line 379 · raw
@xs:List<&2, U32> -> List<&2, U32>
def big_of source · line 386 · raw
@xs:List<&2, U32> -> @+acc:List<&2, U32> -> List<&2, U32>
def div_bits source · line 397 · raw
@n:Nat -> @+i:U32 -> @+num:List<&2, U32> -> @+den:List<&2, U32> -> @+q:0xe8796d1f170a8cd1a32ab698bd6d4d3d/wide.W64 -> QW
n steps of long division, quotient bit i first
def qw_sig source · line 406 · raw
@x:QW -> 0xe8796d1f170a8cd1a32ab698bd6d4d3d/wide.W64
def top_pack source · line 411 · raw
@+s:U32 -> @t:0xe8796d1f170a8cd1a32ab698bd6d4d3d/big.Top -> 0xe8796d1f170a8cd1a32ab698bd6d4d3d/float64.F64
def quot source · line 416 · raw
@neg:Bool -> @+d:List<&2, U32> -> @+m:List<&2, U32> -> @+k:U32 -> QW
def small_value source · line 424 · raw
@+s:U32 -> @+d:List<&2, U32> -> @+m:List<&2, U32> -> 0xe8796d1f170a8cd1a32ab698bd6d4d3d/float64.F64
d / m: a quotient of 63 or 64 bits by long division (d or m first shifted to line them up), the remainder sticky
def exact_go source · line 429 · raw
@neg:Bool -> @+s:U32 -> @d:List<&2, U32> -> @+e:U32 -> 0xe8796d1f170a8cd1a32ab698bd6d4d3d/float64.F64
the float64 nearest s * d * 10^e: d a nonzero big integer, e a signed word
def exact source · line 436 · raw
@+s:U32 -> @d:List<&2, U32> -> @+e:U32 -> 0xe8796d1f170a8cd1a32ab698bd6d4d3d/float64.F64
def range_of source · line 446 · raw
@+n:U32 -> @+t:U32 -> Range
def big_f64 source · line 452 · raw
@+d:List<&2, U32> -> 0xe8796d1f170a8cd1a32ab698bd6d4d3d/float64.F64
a big integer below 2^64 times 2^k as a float64 (exact when it fits in 53 bits)
def fast source · line 457 · raw
@neg:Bool -> @+d:List<&2, U32> -> @+e:U32 -> 0xe8796d1f170a8cd1a32ab698bd6d4d3d/float64.F64
Clinger's fast path: d < 10^15 and 10^|e| (|e| <= 22) are both exact float64s, so one correctly rounded multiply or divide is the correctly rounded answer
def exact_or_fast source · line 464 · raw
@quick:Bool -> @+s:U32 -> @d:List<&2, U32> -> @+e:U32 -> 0xe8796d1f170a8cd1a32ab698bd6d4d3d/float64.F64
def value_r source · line 471 · raw
@r:Range -> @+s:U32 -> @+n:U32 -> @ds:List<&2, U32> -> @+e:U32 -> 0xe8796d1f170a8cd1a32ab698bd6d4d3d/float64.F64
def value source · line 484 · raw
@+s:U32 -> @+ds:List<&2, U32> -> @+n:U32 -> @+e:U32 -> 0xe8796d1f170a8cd1a32ab698bd6d4d3d/float64.F64
digits (first first, no leading or trailing zeros, n of them) times 10^e: past 800 digits the rest is only "something nonzero", a final 1, which rounds the same
def number source · line 491 · raw
@+s:U32 -> @t:TZ -> @+nfrac:U32 -> @+ex:U32 -> @+exneg:Bool -> 0xe8796d1f170a8cd1a32ab698bd6d4d3d/float64.F64
def accepting source · line 497 · raw
@+st:U32 -> Bool
def bad source · line 500 · raw
@+text:String -> Result<&2, &2, String, 0xe8796d1f170a8cd1a32ab698bd6d4d3d/float64.F64>
def finish_ok source · line 503 · raw
@ok:Bool -> @+s:U32 -> @ds:List<&2, U32> -> @+nfrac:U32 -> @+ex:U32 -> @+exneg:Bool -> @+text:String -> Result<&2, &2, String, 0xe8796d1f170a8cd1a32ab698bd6d4d3d/float64.F64>
def finish source · line 510 · raw
@p:PS -> @+s:U32 -> @+text:String -> Result<&2, &2, String, 0xe8796d1f170a8cd1a32ab698bd6d4d3d/float64.F64>
def word_of source · line 521 · raw
@+xs:List<&2, U32> -> Spelt
def body_w source · line 525 · raw
@w:Spelt -> @+s:U32 -> @xs:List<&2, U32> -> @+text:String -> Result<&2, &2, String, 0xe8796d1f170a8cd1a32ab698bd6d4d3d/float64.F64>
def body source · line 534 · raw
@+s:U32 -> @+xs:List<&2, U32> -> @+text:String -> Result<&2, &2, String, 0xe8796d1f170a8cd1a32ab698bd6d4d3d/float64.F64>
def sgn_of source · line 543 · raw
@+c:U32 -> Sgn
def signed_c source · line 546 · raw
@g:Sgn -> @+c:U32 -> @t:List<&2, U32> -> @+text:String -> Result<&2, &2, String, 0xe8796d1f170a8cd1a32ab698bd6d4d3d/float64.F64>
def signed source · line 555 · raw
@xs:List<&2, U32> -> @+text:String -> Result<&2, &2, String, 0xe8796d1f170a8cd1a32ab698bd6d4d3d/float64.F64>
def parse source · line 563 · raw
@+text:String -> Result<&2, &2, String, 0xe8796d1f170a8cd1a32ab698bd6d4d3d/float64.F64>
the float64 that Python's float(text) gives (ASCII digits), or why the text is not a number