From: Ian Jackson Date: Sun, 4 Oct 2020 00:06:15 +0000 (+0100) Subject: wip, debugging bigfloat X-Git-Tag: otter-0.2.0~776 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=17986537abe4174bf2c45e883a19e70979cb4d04;p=otter.git wip, debugging bigfloat Signed-off-by: Ian Jackson --- diff --git a/Makefile b/Makefile index 095a7805..78b175f5 100644 --- a/Makefile +++ b/Makefile @@ -77,8 +77,8 @@ $(CARGO_TARGET_DIR)/debug/server: $(CARGO_TARGET_DIR)/release/server: $(CARGO) build --release -templates/script.js: templates/script.ts tsconfig.json - tsc --outfile $@.tmp 2>&1 \ +templates/%.js: templates/%.ts tsconfig.json + tsc --outfile $@.tmp $< 2>&1 \ | perl -pe 's/\((\d+),(\d+)\):/:$$1:$$2:/'; \ test "$${PIPESTATUS[*]}" = "0 0" mv -f $@.tmp $@ diff --git a/templates/bigfloat.ts b/templates/bigfloat.ts index a9ee2380..d7a07220 100644 --- a/templates/bigfloat.ts +++ b/templates/bigfloat.ts @@ -8,72 +8,76 @@ // operations available! class Bigfloat { - exponent: number, - limbs: number[], // BE, limbs are each in [ 0, 2^48 ) + exponent: number; + limbs: number[]; // BE, limbs are each in [ 0, 2^48 ) // binary point is just before limbs[0] // exponent is in limbs // sign bit is top bit of limbs[0] // always at least one limb - static type Iterator = function() => BigFlot; + static LIMB_BIT : number = 48; + private static LIMB_NEGATIVE : number = 1<<47; + private static LIMB_MODULUS : number = 1<<48; - static const LIMB_BIT : number = 48; - private const LIMB_NEGATIVE : number = 1<<47; - private const LIMB_MODULUS : number = 1<<48; - - function constructor(j: { e: number, l: number[] }) { - this.exponent = e; - this.limbs = l; + constructor(j: { e: number, l: number[] }) { + this.exponent = j.e; + this.limbs = j.l; } - private static l0_value(l0: number) -> number { + private static l0_value(l0: number): number { return l0 > Bigfloat.LIMB_NEGATIVE ? l0 - Bigfloat.LIMB_MODULUS : l0; } - private limb_lookup(i) -> number { + private limb_lookup(i): number { if (i >= this.limbs.length) return 0; if (i >= 0) return this.limbs[i]; let l0 = this.limbs[0]; return (l0 < Bigfloat.LIMB_NEGATIVE ? 0 : Bigfloat.LIMB_MODULUS-1); } - function cmp(other: Bigfloat): boolean { + cmp(other: Bigfloat): number { let de = other.exponent - this.exponent; if (de) { if ((de > 0 ? this : other).limbs[0] > Bigfloat.LIMB_NEGATIVE) de *= -1; return de; } - let lt0v = BigFloat.l0_value(this.limbs[0]); - let lo0v = BigFloat.l0_value(other.limbs[0]); - return lo0 - lt0; + let lt0v = Bigfloat.l0_value(this.limbs[0]); + let lo0v = Bigfloat.l0_value(other.limbs[0]); + return lo0v - lt0v; + } + + clone(): Bigfloat { + return new Bigfloat({ + e: this.exponent, + l: this.limbs.slice(), + }); } - function extend_left() { - this.limbs.unshift(l0_value(this.limbs[0])); + private extend_left() { + this.limbs.unshift(Bigfloat.l0_value(this.limbs[0])); this.exponent++; } - function iter_upto(endv: BigFloat, count: number): - { + iter_upto(endv: Bigfloat, count: number): () => Bigfloat { // next() can be called count times // to produce values > this, < endv - let e_out = Number.max(this.exponent, max.exponent); - for (e = e_out; + let e_out = Math.max(this.exponent, endv.exponent); + for (let e = e_out; e; e--) { let it = e - this.exponent; - let ie = e - endv exponent; + let ie = e - endv.exponent; if (it > this.limbs.length && ie > endv.limbs.length) { // Oh actually these numbers are equal! - return function(){ this; } + return function(){ return this.clone(); } } let lt = this.limb_lookup(it) let le = endv.limb_lookup(ie) if (lt == le) continue; - let avail = (le - lt) & (BIGFLOAT_LIMB_MODULUS-1); - let start = this.copy(); + let avail = (le - lt) & (Bigfloat.LIMB_MODULUS-1); + let start = this.clone(); while (it < 0) { start.extend_left(); it++; @@ -85,14 +89,14 @@ class Bigfloat { start[it] += (avail>>1); step = Bigfloat.LIMB_MODULUS / (count+1); it++; - start.length = it; + start.limbs.length = it; start[it] = 0; } return function() { start[it] += step; start[it] = Math.floor(start[it]); start[it] &= (Bigfloat.LIMB_MODULUS-1); - return start.copy(); + return start.clone(); } } }