chiark / gitweb /
wip, debugging bigfloat
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 4 Oct 2020 00:06:15 +0000 (01:06 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 4 Oct 2020 00:06:15 +0000 (01:06 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
Makefile
templates/bigfloat.ts

index 095a78055fee0cf4e2e33743c6897a00b63df465..78b175f57c974145ee2b84aeb5c17bd7912f06bf 100644 (file)
--- 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 $@
index a9ee23803aba940583752062452c1500636ad8c1..d7a07220c9186877fa566d11103b8c2ee2babb68 100644 (file)
@@ -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();
       }
     }
   }