diff options
Diffstat (limited to 'gen/lib2.bc')
-rw-r--r-- | gen/lib2.bc | 79 |
1 files changed, 64 insertions, 15 deletions
diff --git a/gen/lib2.bc b/gen/lib2.bc index 93df1889..ba3f76b1 100644 --- a/gen/lib2.bc +++ b/gen/lib2.bc @@ -3,7 +3,7 @@ * * SPDX-License-Identifier: BSD-2-Clause * - * Copyright (c) 2018-2021 Gavin D. Howard and contributors. + * Copyright (c) 2018-2023 Gavin D. Howard and contributors. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -36,7 +36,7 @@ define p(x,y){ auto a a=y$ - if(y==a)return (x^a)@scale + if(y==a)return(x^a)@scale return e(y*l(x)) } define r(x,p){ @@ -93,6 +93,18 @@ define comb(n,r){ scale=s return f } +define fib(n){ + auto i,t,p,r + if(!n)return 0 + n=abs(n)$ + t=1 + for (i=1;i<n;++i){ + r=p + p=t + t+=r + } + return t +} define log(x,b){ auto p,s s=scale @@ -106,7 +118,7 @@ define log(x,b){ define l2(x){return log(x,2)} define l10(x){return log(x,A)} define root(x,n){ - auto s,m,r,q,p + auto s,t,m,r,q,p if(n<0)sqrt(n) n=n$ if(n==0)x/n @@ -114,13 +126,17 @@ define root(x,n){ if(n==2)return sqrt(x) s=scale scale=0 - if(x<0&&n%2==0)sqrt(x) - scale=s+2 + if(x<0&&n%2==0){ + scale=s + sqrt(x) + } + scale=s+scale(x)+5 + t=s+5 m=(x<0) x=abs(x) p=n-1 q=A^ceil((length(x$)/n)$,0) - while(r!=q){ + while(r@t!=q@t){ r=q q=(p*r+x/r^p)/n } @@ -250,8 +266,7 @@ define ubytes(x){ define sbytes(x){ auto p,n,z z=(x<0) - x=abs(x) - x=x$ + x=abs(x)$ n=ubytes(x) p=2^(n*8-1) if(x>p||(!z&&x==p))n*=2 @@ -274,22 +289,56 @@ define s2un(x,n){ return x } define s2u(x){return s2un(x,sbytes(x))} +define void plz(x){ + if(leading_zero())print x + else{ + if(x>-1&&x<1&&x!=0){ + if(x<0)print"-" + print 0,abs(x) + } + else print x + } +} +define void plznl(x){ + plz(x) + print"\n" +} +define void pnlz(x){ + auto s,i + if(leading_zero()){ + if(x>-1&&x<1&&x!=0){ + s=scale(x) + if(x<0)print"-" + print"." + x=abs(x) + for(i=0;i<s;++i){ + x<<=1 + print x$ + x-=x$ + } + return + } + } + print x +} +define void pnlznl(x){ + pnlz(x) + print"\n" +} define void output_byte(x,i){ - auto j,p,y,b - j=ibase - ibase=A + auto j,p,y,b,s s=scale scale=0 x=abs(x)$ b=x/(2^(i*8)) - b%=256 - y=log(256,obase) + j=2^8 + b%=j + y=log(j,obase) if(b>1)p=log(b,obase)+1 else p=b for(i=y-p;i>0;--i)print 0 if(b)print b scale=s - ibase=j } define void output_uint(x,n){ auto i @@ -441,7 +490,7 @@ define bxor(a,b){ return bunrev(t) } define bshl(a,b){return abs(a)$*2^abs(b)$} -define bshr(a,b){return (abs(a)$/2^abs(b)$)$} +define bshr(a,b){return(abs(a)$/2^abs(b)$)$} define bnotn(x,n){ auto s,t,m[] s=scale |