diff options
-rw-r--r-- | miscutils/bc.c | 370 |
1 files changed, 186 insertions, 184 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c index 6f2606c79..fd9e31cc5 100644 --- a/miscutils/bc.c +++ b/miscutils/bc.c @@ -915,185 +915,6 @@ static const BcNumBinaryOp bc_program_ops[] = { static const char bc_program_stdin_name[] = "<stdin>"; -#if ENABLE_BC -static const char *bc_lib_name = "gen/lib.bc"; - -static const char bc_lib[] = { - "scale=20" -"\n" "define e(x){" -"\n" "auto b,s,n,r,d,i,p,f,v" -"\n" "b=ibase" -"\n" "ibase=A" -"\n" "if(x<0){" -"\n" "n=1" -"\n" "x=-x" -"\n" "}" -"\n" "s=scale" -"\n" "r=6+s+0.44*x" -"\n" "scale=scale(x)+1" -"\n" "while(x>1){" -"\n" "d+=1" -"\n" "x/=2" -"\n" "scale+=1" -"\n" "}" -"\n" "scale=r" -"\n" "r=x+1" -"\n" "p=x" -"\n" "f=v=1" -"\n" "for(i=2;v!=0;++i){" -"\n" "p*=x" -"\n" "f*=i" -"\n" "v=p/f" -"\n" "r+=v" -"\n" "}" -"\n" "while((d--)!=0)r*=r" -"\n" "scale=s" -"\n" "ibase=b" -"\n" "if(n!=0)return(1/r)" -"\n" "return(r/1)" -"\n" "}" -"\n" "define l(x){" -"\n" "auto b,s,r,p,a,q,i,v" -"\n" "b=ibase" -"\n" "ibase=A" -"\n" "if(x<=0){" -"\n" "r=(1-10^scale)/1" -"\n" "ibase=b" -"\n" "return(r)" -"\n" "}" -"\n" "s=scale" -"\n" "scale+=6" -"\n" "p=2" -"\n" "while(x>=2){" -"\n" "p*=2" -"\n" "x=sqrt(x)" -"\n" "}" -"\n" "while(x<=0.5){" -"\n" "p*=2" -"\n" "x=sqrt(x)" -"\n" "}" -"\n" "r=a=(x-1)/(x+1)" -"\n" "q=a*a" -"\n" "v=1" -"\n" "for(i=3;v!=0;i+=2){" -"\n" "a*=q" -"\n" "v=a/i" -"\n" "r+=v" -"\n" "}" -"\n" "r*=p" -"\n" "scale=s" -"\n" "ibase=b" -"\n" "return(r/1)" -"\n" "}" -"\n" "define s(x){" -"\n" "auto b,s,r,n,a,q,i" -"\n" "b=ibase" -"\n" "ibase=A" -"\n" "s=scale" -"\n" "scale=1.1*s+2" -"\n" "a=a(1)" -"\n" "if(x<0){" -"\n" "n=1" -"\n" "x=-x" -"\n" "}" -"\n" "scale=0" -"\n" "q=(x/a+2)/4" -"\n" "x=x-4*q*a" -"\n" "if(q%2!=0)x=-x" -"\n" "scale=s+2" -"\n" "r=a=x" -"\n" "q=-x*x" -"\n" "for(i=3;a!=0;i+=2){" -"\n" "a*=q/(i*(i-1))" -"\n" "r+=a" -"\n" "}" -"\n" "scale=s" -"\n" "ibase=b" -"\n" "if(n!=0)return(-r/1)" -"\n" "return(r/1)" -"\n" "}" -"\n" "define c(x){" -"\n" "auto b,s" -"\n" "b=ibase" -"\n" "ibase=A" -"\n" "s=scale" -"\n" "scale*=1.2" -"\n" "x=s(2*a(1)+x)" -"\n" "scale=s" -"\n" "ibase=b" -"\n" "return(x/1)" -"\n" "}" -"\n" "define a(x){" -"\n" "auto b,s,r,n,a,m,t,f,i,u" -"\n" "b=ibase" -"\n" "ibase=A" -"\n" "n=1" -"\n" "if(x<0){" -"\n" "n=-1" -"\n" "x=-x" -"\n" "}" -"\n" "if(x==1){" -"\n" "if(scale<65){" -"\n" "return(.7853981633974483096156608458198757210492923498437764552437361480/n)" -"\n" "}" -"\n" "}" -"\n" "if(x==.2){" -"\n" "if(scale<65){" -"\n" "return(.1973955598498807583700497651947902934475851037878521015176889402/n)" -"\n" "}" -"\n" "}" -"\n" "s=scale" -"\n" "if(x>.2){" -"\n" "scale+=5" -"\n" "a=a(.2)" -"\n" "}" -"\n" "scale=s+3" -"\n" "while(x>.2){" -"\n" "m+=1" -"\n" "x=(x-.2)/(1+.2*x)" -"\n" "}" -"\n" "r=u=x" -"\n" "f=-x*x" -"\n" "t=1" -"\n" "for(i=3;t!=0;i+=2){" -"\n" "u*=f" -"\n" "t=u/i" -"\n" "r+=t" -"\n" "}" -"\n" "scale=s" -"\n" "ibase=b" -"\n" "return((m*a+r)/n)" -"\n" "}" -"\n" "define j(n,x){" -"\n" "auto b,s,o,a,i,v,f" -"\n" "b=ibase" -"\n" "ibase=A" -"\n" "s=scale" -"\n" "scale=0" -"\n" "n/=1" -"\n" "if(n<0){" -"\n" "n=-n" -"\n" "if(n%2==1)o=1" -"\n" "}" -"\n" "a=1" -"\n" "for(i=2;i<=n;++i)a*=i" -"\n" "scale=1.5*s" -"\n" "a=(x^n)/2^n/a" -"\n" "r=v=1" -"\n" "f=-x*x/4" -"\n" "scale=scale+length(a)-scale(a)" -"\n" "for(i=1;v!=0;++i){" -"\n" "v=v*f/i/(n+i)" -"\n" "r+=v" -"\n" "}" -"\n" "scale=s" -"\n" "ibase=b" -"\n" "if(o!=0)a=-a" -"\n" "return(a*r/1)" -"\n" "}" -}; -#endif // ENABLE_BC - static void fflush_and_check(void) { fflush_all(); @@ -7147,6 +6968,183 @@ static BcStatus bc_vm_stdin(void) return s; } +#if ENABLE_BC +static const char bc_lib[] = { + "scale=20" +"\n" "define e(x){" +"\n" "auto b,s,n,r,d,i,p,f,v" +"\n" "b=ibase" +"\n" "ibase=A" +"\n" "if(x<0){" +"\n" "n=1" +"\n" "x=-x" +"\n" "}" +"\n" "s=scale" +"\n" "r=6+s+0.44*x" +"\n" "scale=scale(x)+1" +"\n" "while(x>1){" +"\n" "d+=1" +"\n" "x/=2" +"\n" "scale+=1" +"\n" "}" +"\n" "scale=r" +"\n" "r=x+1" +"\n" "p=x" +"\n" "f=v=1" +"\n" "for(i=2;v!=0;++i){" +"\n" "p*=x" +"\n" "f*=i" +"\n" "v=p/f" +"\n" "r+=v" +"\n" "}" +"\n" "while((d--)!=0)r*=r" +"\n" "scale=s" +"\n" "ibase=b" +"\n" "if(n!=0)return(1/r)" +"\n" "return(r/1)" +"\n" "}" +"\n" "define l(x){" +"\n" "auto b,s,r,p,a,q,i,v" +"\n" "b=ibase" +"\n" "ibase=A" +"\n" "if(x<=0){" +"\n" "r=(1-10^scale)/1" +"\n" "ibase=b" +"\n" "return(r)" +"\n" "}" +"\n" "s=scale" +"\n" "scale+=6" +"\n" "p=2" +"\n" "while(x>=2){" +"\n" "p*=2" +"\n" "x=sqrt(x)" +"\n" "}" +"\n" "while(x<=0.5){" +"\n" "p*=2" +"\n" "x=sqrt(x)" +"\n" "}" +"\n" "r=a=(x-1)/(x+1)" +"\n" "q=a*a" +"\n" "v=1" +"\n" "for(i=3;v!=0;i+=2){" +"\n" "a*=q" +"\n" "v=a/i" +"\n" "r+=v" +"\n" "}" +"\n" "r*=p" +"\n" "scale=s" +"\n" "ibase=b" +"\n" "return(r/1)" +"\n" "}" +"\n" "define s(x){" +"\n" "auto b,s,r,n,a,q,i" +"\n" "b=ibase" +"\n" "ibase=A" +"\n" "s=scale" +"\n" "scale=1.1*s+2" +"\n" "a=a(1)" +"\n" "if(x<0){" +"\n" "n=1" +"\n" "x=-x" +"\n" "}" +"\n" "scale=0" +"\n" "q=(x/a+2)/4" +"\n" "x=x-4*q*a" +"\n" "if(q%2!=0)x=-x" +"\n" "scale=s+2" +"\n" "r=a=x" +"\n" "q=-x*x" +"\n" "for(i=3;a!=0;i+=2){" +"\n" "a*=q/(i*(i-1))" +"\n" "r+=a" +"\n" "}" +"\n" "scale=s" +"\n" "ibase=b" +"\n" "if(n!=0)return(-r/1)" +"\n" "return(r/1)" +"\n" "}" +"\n" "define c(x){" +"\n" "auto b,s" +"\n" "b=ibase" +"\n" "ibase=A" +"\n" "s=scale" +"\n" "scale*=1.2" +"\n" "x=s(2*a(1)+x)" +"\n" "scale=s" +"\n" "ibase=b" +"\n" "return(x/1)" +"\n" "}" +"\n" "define a(x){" +"\n" "auto b,s,r,n,a,m,t,f,i,u" +"\n" "b=ibase" +"\n" "ibase=A" +"\n" "n=1" +"\n" "if(x<0){" +"\n" "n=-1" +"\n" "x=-x" +"\n" "}" +"\n" "if(x==1){" +"\n" "if(scale<65){" +"\n" "return(.7853981633974483096156608458198757210492923498437764552437361480/n)" +"\n" "}" +"\n" "}" +"\n" "if(x==.2){" +"\n" "if(scale<65){" +"\n" "return(.1973955598498807583700497651947902934475851037878521015176889402/n)" +"\n" "}" +"\n" "}" +"\n" "s=scale" +"\n" "if(x>.2){" +"\n" "scale+=5" +"\n" "a=a(.2)" +"\n" "}" +"\n" "scale=s+3" +"\n" "while(x>.2){" +"\n" "m+=1" +"\n" "x=(x-.2)/(1+.2*x)" +"\n" "}" +"\n" "r=u=x" +"\n" "f=-x*x" +"\n" "t=1" +"\n" "for(i=3;t!=0;i+=2){" +"\n" "u*=f" +"\n" "t=u/i" +"\n" "r+=t" +"\n" "}" +"\n" "scale=s" +"\n" "ibase=b" +"\n" "return((m*a+r)/n)" +"\n" "}" +"\n" "define j(n,x){" +"\n" "auto b,s,o,a,i,v,f" +"\n" "b=ibase" +"\n" "ibase=A" +"\n" "s=scale" +"\n" "scale=0" +"\n" "n/=1" +"\n" "if(n<0){" +"\n" "n=-n" +"\n" "if(n%2==1)o=1" +"\n" "}" +"\n" "a=1" +"\n" "for(i=2;i<=n;++i)a*=i" +"\n" "scale=1.5*s" +"\n" "a=(x^n)/2^n/a" +"\n" "r=v=1" +"\n" "f=-x*x/4" +"\n" "scale=scale+length(a)-scale(a)" +"\n" "for(i=1;v!=0;++i){" +"\n" "v=v*f/i/(n+i)" +"\n" "r+=v" +"\n" "}" +"\n" "scale=s" +"\n" "ibase=b" +"\n" "if(o!=0)a=-a" +"\n" "return(a*r/1)" +"\n" "}" +}; +#endif // ENABLE_BC + static BcStatus bc_vm_exec(void) { BcStatus s = BC_STATUS_SUCCESS; @@ -7155,15 +7153,19 @@ static BcStatus bc_vm_exec(void) #if ENABLE_BC if (option_mask32 & BC_FLAG_L) { - bc_lex_file(&G.prs.l, bc_lib_name); + // We know that internal library is not buggy, + // thus error checking is normally disabled. +# define DEBUG_LIB 0 + bc_lex_file(&G.prs.l, ""); s = bc_parse_text(&G.prs, bc_lib); + if (DEBUG_LIB && s) return s; - while (!s && G.prs.l.t.t != BC_LEX_EOF) + while (G.prs.l.t.t != BC_LEX_EOF) { s = G.prs.parse(&G.prs); - - if (s) return s; + if (DEBUG_LIB && s) return s; + } s = bc_program_exec(); - if (s) return s; + if (DEBUG_LIB && s) return s; } #endif |