~/bend-docscommunity

ball.bend checks

raw source on the hub · import bend-float64@0.1.2.0/ball.bend as Ball

bend-float64's balls: arbitrary-precision numbers with a rigorous error bound, for the correctly rounded math functions. A ball is (-1)^neg * m * 2^e with |true value - that| <= r: m a big integer, e a signed word, r an error bound kept as a 30-bit number times a power of two and always rounded up. Each operation keeps p bits and widens r by everything it drops, so the true result is always inside. decide answers the float64 every point of a ball rounds to, when they all round to the same one. Pure.

4 imports
import Base
import ./wide.bend as W
import ./big.bend as B
import ./float64.bend as F

Types

type Mag source · line 27 · raw

Data

type Ball source · line 133 · raw

Data

type Sh source · line 218 · raw

Data

Definitions

def s_lt source · line 13 · raw

@+a:U32 -> @+b:U32 -> Bool

def s_max source · line 16 · raw

@+a:U32 -> @+b:U32 -> U32

def s_min source · line 19 · raw

@+a:U32 -> @+b:U32 -> U32

def is_neg source · line 22 · raw

@+x:U32 -> Bool

def mzero source · line 30 · raw

Mag

def mag_is_zero source · line 33 · raw

@a:Mag -> Bool

def wbits source · line 38 · raw

@+w:0x3623a86835ccb58d523b8156ac4abb92/wide.W64 -> U32

def mag_w_n source · line 42 · raw

@+up:Bool -> @+w:0x3623a86835ccb58d523b8156ac4abb92/wide.W64 -> @+e:U32 -> @+n:U32 -> Mag

w * 2^e as a Mag, rounded up (up) or down

def mag_w source · line 47 · raw

@+w:0x3623a86835ccb58d523b8156ac4abb92/wide.W64 -> @+e:U32 -> Mag

def mag_w_low source · line 50 · raw

@+w:0x3623a86835ccb58d523b8156ac4abb92/wide.W64 -> @+e:U32 -> Mag

def pow2 source · line 53 · raw

@+e:U32 -> Mag

def ceil_shr source · line 57 · raw

@+m:U32 -> @+d:U32 -> 0x3623a86835ccb58d523b8156ac4abb92/wide.W64

m / 2^d rounded up, as a W64

def mag_add_go source · line 60 · raw

@a:Mag -> @b:Mag -> Mag

def mag_add source · line 67 · raw

@+a:Mag -> @+b:Mag -> Mag

a + b, rounded up

def mag_mul source · line 71 · raw

@a:Mag -> @b:Mag -> Mag

a * b, rounded up

def mag_shift source · line 76 · raw

@a:Mag -> @+k:U32 -> Mag

def mag_small source · line 81 · raw

@+n:U32 -> Mag

def mag_top source · line 84 · raw

@long:Bool -> @+xs:List<&2, U32> -> @+e:U32 -> @+n:U32 -> @+up:Bool -> Mag

def mag_big source · line 92 · raw

@+xs:List<&2, U32> -> @+e:U32 -> Mag

xs * 2^e rounded up (an upper bound), or down

def mag_big_low source · line 96 · raw

@+xs:List<&2, U32> -> @+e:U32 -> Mag

def mag_lt source · line 101 · raw

@a:Mag -> @b:Mag -> Bool

a < b (exactly)

def mag_sub_go source · line 112 · raw

@a:Mag -> @b:Mag -> Mag

a - b rounded down, 0 when b >= a (b not 0)

def mag_sub_low source · line 121 · raw

@+a:Mag -> @+b:Mag -> Mag

a - b rounded down, 0 when b >= a

def mag_div_up source · line 125 · raw

@a:Mag -> @b:Mag -> Mag

a / b rounded up, b > 0 (b's top 16 bits divide, so the result can only be too large)

def exact source · line 136 · raw

@+neg:Bool -> @+m:List<&2, U32> -> @+e:U32 -> Ball

def of_u32 source · line 139 · raw

@+n:U32 -> Ball

def one source · line 142 · raw

Ball

def of_cls source · line 146 · raw

@c:0x3623a86835ccb58d523b8156ac4abb92/float64.Cls -> Ball

a finite float64, exactly

def of_f64 source · line 155 · raw

@x:0x3623a86835ccb58d523b8156ac4abb92/float64.F64 -> Ball

def bad source · line 159 · raw

Ball

a ball no answer can come from: its error covers everything

def negate source · line 162 · raw

@b:Ball -> Ball

def with_neg source · line 167 · raw

@+n:Bool -> @b:Ball -> Ball

def abs source · line 172 · raw

@b:Ball -> Ball

def neg_of source · line 175 · raw

@b:Ball -> Bool

def top source · line 181 · raw

@b:Ball -> U32

the exponent just above the midpoint's top bit (signed; very low for 0)

def mag_of source · line 187 · raw

@b:Ball -> Mag

an upper bound on |true value|

def widen source · line 192 · raw

@b:Ball -> @+x:Mag -> Ball

def mul_pow2 source · line 197 · raw

@b:Ball -> @+k:U32 -> Ball

def trunc_go source · line 202 · raw

@long:Bool -> @+n:Bool -> @+m:List<&2, U32> -> @+e:U32 -> @+r:Mag -> @+s:U32 -> Ball

def trunc source · line 210 · raw

@b:Ball -> @+p:U32 -> Ball

at most p bits of midpoint

def shift_out source · line 221 · raw

@all:Bool -> @+m:List<&2, U32> -> @+d:U32 -> Sh

def shift_dir source · line 228 · raw

@down:Bool -> @+m:List<&2, U32> -> @+d:U32 -> Sh

def shift_to source · line 236 · raw

@+m:List<&2, U32> -> @+e:U32 -> @+t:U32 -> Sh

m * 2^e as a multiple of 2^t, truncated

def lost_mag source · line 239 · raw

@s:Sh -> @+t:U32 -> Mag

def sh_m source · line 244 · raw

@s:Sh -> List<&2, U32>

def signed_sum source · line 249 · raw

@same:Bool -> @+nx:Bool -> @+ny:Bool -> @+x:List<&2, U32> -> @+y:List<&2, U32> -> @+t:U32 -> @+r:Mag -> Ball

def add_go source · line 256 · raw

@x:Ball -> @y:Ball -> @+p:U32 -> Ball

def zero_mid source · line 264 · raw

@b:Ball -> Bool

def err_of source · line 269 · raw

@b:Ball -> Mag

def add_z source · line 274 · raw

@zx:Bool -> @zy:Bool -> @+x:Ball -> @+y:Ball -> @+p:U32 -> Ball

def add source · line 284 · raw

@+x:Ball -> @+y:Ball -> @+p:U32 -> Ball

x + y to p bits

def sub source · line 288 · raw

@+x:Ball -> @y:Ball -> @+p:U32 -> Ball

x - y to p bits

def mul source · line 294 · raw

@x:Ball -> @y:Ball -> @+p:U32 -> Ball

x * y to p bits

def mul_u source · line 302 · raw

@x:Ball -> @+n:U32 -> Ball

x * n, n a word

def ds_q source · line 307 · raw

@d:0x3623a86835ccb58d523b8156ac4abb92/big.DS -> List<&2, U32>

def qr_q source · line 312 · raw

@d:0x3623a86835ccb58d523b8156ac4abb92/big.QR -> List<&2, U32>

def lshift source · line 317 · raw

@pos:Bool -> @+m:List<&2, U32> -> @+s:U32 -> List<&2, U32>

def div_u source · line 325 · raw

@x:Ball -> @+n:U32 -> @+p:U32 -> Ball

x / n to p bits, 0 < n < 2^16

def round_top source · line 332 · raw

@+neg:Bool -> @t:0x3623a86835ccb58d523b8156ac4abb92/big.Top -> @+e:U32 -> 0x3623a86835ccb58d523b8156ac4abb92/float64.F64

def mid source · line 337 · raw

@b:Ball -> Ball

def ball_m source · line 342 · raw

@b:Ball -> List<&2, U32>

def bits_of source · line 347 · raw

@b:Ball -> U32

def exp_of source · line 352 · raw

@b:Ball -> U32

def newton_pick source · line 357 · raw

@done:Bool -> @+r:Ball -> @more:(@_:Unit -> Ball) -> Ball

def recip_go source · line 365 · raw

@n:Nat -> @+y:Ball -> @+r:Ball -> @+q:U32 -> @+p:U32 -> Ball

Newton steps for 1/y: r + r (1 - y r), the precision doubling from 50 bits to past p

def recip source · line 374 · raw

@+y:Ball -> @+p:U32 -> Ball

an approximation of 1/y to about p bits (no error bound: the residual bounds it), y nonzero

def div_ok source · line 380 · raw

@ok:Bool -> @+x:Ball -> @+y:Ball -> @+low:Mag -> @+p:U32 -> Ball

def div source · line 391 · raw

@+x:Ball -> @y:Ball -> @+p:U32 -> Ball

x / y to p bits (a bad ball when y's ball reaches 0): x times an approximate reciprocal R of y, widened by |x| |1 - y R| / |y|, the exact difference

def halve source · line 397 · raw

@+v:U32 -> U32

def rsqrt_go source · line 401 · raw

@n:Nat -> @+x:Ball -> @+r:Ball -> @+q:U32 -> @+p:U32 -> Ball

Newton steps for 1/sqrt(x): r + r (1 - x r^2) / 2

def sqrt_ok source · line 409 · raw

@ok:Bool -> @+x:Ball -> @+p:U32 -> Ball

def sqrt source · line 424 · raw

@x:Ball -> @+p:U32 -> Ball

sqrt(x) to p bits, x's ball positive and its error under a quarter of it (else a bad ball): s = x R for an approximate 1/sqrt(x) R, widened by |x - s^2| / s (sqrt(x) - s = (x - s^2) / (sqrt(x) + s))

def constant source · line 430 · raw

@+c:List<&2, U32> -> @+bits:U32 -> @+p:U32 -> Ball

a constant floor(c * 2^bits), as a ball of p bits

def round_big source · line 435 · raw

@+neg:Bool -> @+xs:List<&2, U32> -> @+e:U32 -> 0x3623a86835ccb58d523b8156ac4abb92/float64.F64

def same source · line 438 · raw

@a:0x3623a86835ccb58d523b8156ac4abb92/float64.F64 -> @b:0x3623a86835ccb58d523b8156ac4abb92/float64.F64 -> Bool

def decide_lohi source · line 443 · raw

@below:Bool -> @+neg:Bool -> @+m:List<&2, U32> -> @+rr:List<&2, U32> -> @+e:U32 -> Maybe<&2, 0x3623a86835ccb58d523b8156ac4abb92/float64.F64>

def err_dir source · line 451 · raw

@below:Bool -> @+rm:U32 -> @+re:U32 -> @+e:U32 -> List<&2, U32>

def err_units source · line 459 · raw

@r:Mag -> @+e:U32 -> List<&2, U32>

r in units of 2^e, rounded up

def decide source · line 465 · raw

@b:Ball -> Maybe<&2, 0x3623a86835ccb58d523b8156ac4abb92/float64.F64>

the float64 every point of b rounds to, if they all round to the same one