diff --git a/arith.modal b/arith.modal index 892f174..9f0b115 100644 --- a/arith.modal +++ b/arith.modal @@ -2,10 +2,15 @@ -- ( N: little-endian natural numbers ) +-- ( NOTE: this file requires a patched modal which increases some of the limits ) +-- ( otherwise you'll get a Segmentation Fault ) + -- ( constants ) <> zero ((0 ())) <> one ((1 ())) <> two ((0 (1 ()))) +<> three ((1 (1 ()))) +<> four ((0 (0 (1 ())))) <> eight ((0 (0 (0 (1 ()))))) <> ten ((0 (1 (0 (1 ()))))) <> sixteen ((0 (0 (0 (0 (1 ())))))) @@ -338,6 +343,8 @@ <> ((tostr1 (N (0 ())) ?a)) (?a) <> ((tostr1 (N (?h ?t)) ?a)) ((tostr2 (divmod (N (?h ?t)) (N ten)) ?a)) <> ((tostr2 ((N ?q) (N ?r)) ?a)) ((tostr1 (N ?q) ((decimal ?r) ?a))) +<> ((tostr (Z (+ ?x)))) ((tostr (N ?x))) +<> ((tostr (Z (- ?x)))) ((concat (- ()) (tostr (N ?x)))) -- ( concatenate lists ) <> ((concat (?h ?t) ?r)) ((?h (concat ?t ?r))) @@ -376,7 +383,7 @@ <> (force (x ?t)) ((x force ?t)) -- ( emit ) -<> (emit force/r ?^) (?^) +<> (emit force/r ?^) ((S ?^)) -- ( to binary string ) <> ((bstr (N ?x))) ((bstr1 force ?x ())) @@ -414,4 +421,59 @@ <> ((hdigit 0 1 1 1)) (e) <> ((hdigit 1 1 1 1)) (f) -(str (nat 999)) + +-- approximate decimal expansion of rational +<> ((approx (Q (?s ?n ?d)) (N ?k))) (emit force (astr ?s (N ?k) (div (mul (pow (N ten) (N ?k)) (N ?n)) (N ?d)))) +<> ((astr - (N ?k) (N ?x))) ((- (astr1 (N ?k) (N ?x) ()))) +<> ((astr + (N ?k) (N ?x))) ((astr1 (N ?k) (N ?x) ())) +<> ((astr1 (N (0 ())) (N ?x) ?a)) ((concat (tostr (N ?x)) (. ?a))) +<> ((astr1 (N ?k) (N (0 ())) ?a)) ((astr1 (dec (N ?k)) (N (0 ())) (0 ?a))) +<> ((astr1 (N ?k) (N ?x) ?a)) ((astr2 (N ?k) (divmod (N ?x) (N ten)) ?a)) +<> ((astr2 (N ?k) ((N ?q) (N ?r)) ?a)) ((astr1 (dec (N ?k)) (N ?q) ((decimal ?r) ?a))) + +-- ( first 16 convergents of the continued fraction. the 14th term provides more precision than 64-bit floating point +<> (pi/cf) (((nat 3) ((nat 7) ((nat 15) ((nat 1) ((nat 292) ((nat 1) ((nat 1) ((nat 1) ((nat 2) ((nat 1) ((nat 3) ((nat 1) ((nat 14) ((nat 2) ((nat 1) ((nat 1) ()))))))))))))))))) + +<> ((pi/rat (N ?k))) ((pi/rat1 (N ?k) pi/cf)) +<> ((pi/rat1 (N (0 ())) ((N ?h) ?t))) ((Q (+ ?h one))) +<> ((pi/rat1 (N ?k) ())) (#err) +<> ((pi/rat1 (N ?k) ((N ?h) ?t))) ((add (Q (+ ?h one)) (div (Q (+ one one)) (pi/rat1 (dec (N ?k)) ?t)))) + +<> ((pi/rat-x (N ?k))) ((pi/finish (pi/approx (N ?k) (N one) (N three)))) +<> ((pi/approx (N (0 ())) (N ?n) (N ?d))) ((ratify + (N ?n) (inc (N ?d)))) +<> ((pi/approx (N ?k) (N ?n) (N ?d))) ((div (Q (+ ?n one)) (add (Q (+ ?d one)) (pi/approx (dec (N ?k)) (add (N ?n) (N ?d)) (add (N ?d) (N two)))))) +<> ((pi/finish (Q ?q))) ((div (Q (+ four one)) (add (Q (+ one one)) (Q ?q)))) + +<> (print (S ?:)) (?:) + +-- ( print (str (mul (add (rat 3/4) (rat 19/21)) (rat 135/136))) ) +print (str (pi/rat (nat 0))) +print (S \n) +print (str (pi/rat (nat 1))) +print (S \n) +print (str (pi/rat (nat 2))) +print (S \n) +print (str (pi/rat (nat 3))) +print (S \n) +print (str (pi/rat (nat 4))) +print (S \n) +print (str (pi/rat (nat 5))) +print (S \n) +print (str (pi/rat (nat 6))) +print (S \n) +print (str (pi/rat (nat 7))) +print (S \n) +print (str (pi/rat (nat 8))) +print (S \n) +print (str (pi/rat (nat 9))) +print (S \n) +print (str (pi/rat (nat 10))) +print (S \n) +print (str (pi/rat (nat 11))) +print (S \n) +print (str (pi/rat (nat 12))) +print (S \n) +print (str (pi/rat (nat 13))) +print (S \n) +print (str (pi/rat (nat 14))) +print (S \n)