?? bc
字號:
.RP.TLBC \- An Arbitrary Precision Desk-Calculator Language.AULorinda Cherry.AURobert Morris.AI.MH.ABBC is a language and a compiler for doing arbitrary precision arithmeticon the PDP-11 under the.UXtime-sharingsystem. The output of the compiler is interpreted and executed bya collection of routines which can input, output, and doarithmetic on indefinitely large integers and on scaled fixed-pointnumbers..PPThese routines are themselves based on a dynamic storage allocator.Overflow does not occur until all available core storageis exhausted..PPThe language has a complete control structure as well as immediate-modeoperation. Functions can be defined and saved for later execution..PPTwo five hundred-digit numbers can be multiplied to give athousand digit result in about ten seconds..PPA small collection of library functions is also available,including sin, cos, arctan, log, exponential, and Bessel functions ofinteger order..PPSome of the uses of this compiler are.IP \-to do computation with large integers,.IP \-to do computation accurate to many decimal places,.IP \-conversion of numbers from one base to another base..AE.PP.SHIntroduction.PPBC is a language and a compiler for doing arbitrary precisionarithmetic on the.UXtime-sharing system [1].The compiler was written to make conveniently available acollection of routines (called DC [5]) which are capable of doingarithmetic on integers of arbitrary size. The compileris by no means intended to provide a complete programminglanguage.It is a minimal language facility..PPThere is a scaling provision that permits theuse of decimal point notation.Provision is made for input and output in bases other thandecimal. Numbers can be converted from decimal to octal bysimply setting the output base to equal 8..PPThe actual limit on the number of digits that canbe handled depends on the amount of storage available on the machine.Manipulation of numbers with many hundreds of digitsis possible even on the smallest versions of.UX ..PPThe syntax of BC has been deliberately selected to agreesubstantially with the C language [2]. Those whoare familiar with C will find few surprises in this language..SHSimple Computations with Integers.PPThe simplest kind of statement is an arithmetic expressionon a line by itself.For instance, if you type in the line:.DS142857 + 285714.DEthe program responds immediately with the line.DS428571.DEThe operators \-, *, /, %, and ^ can also be used; theyindicate subtraction, multiplication, division, remaindering, andexponentiation, respectively. Division of integers produces aninteger result truncated toward zero.Division by zero produces an errorcomment..PPAny term in an expression may be prefixed by a minus sign toindicate that it is to be negated (the `unary' minus sign).The expression.DS7+\-3.DEis interpreted to mean that \-3 is to be added to 7..PPMore complex expressions with several operators and withparentheses are interpreted just as inFortran, with ^ having the greatest bindingpower, then * and % and /, and finally + and \-.Contents of parentheses are evaluated before materialoutside the parentheses.Exponentiations areperformed from right to left and the other operatorsfrom left to right.The two expressions.DSa^b^c and a^(b^c).DEare equivalent, as are the two expressions.DSa*b*c and (a*b)*c.DEBC shares with Fortran and C the undesirable convention that.DSa/b*c is equivalent to (a/b)*c.DE.PPInternal storage registers to hold numbers have single lower-caseletter names. The value of an expression can be assigned toa register in the usual way. The statement.DSx = x + 3.DEhas the effect of increasing by three the value of the contents of theregister named x.When, as in this case, the outermost operator is an =, theassignment is performed but the result is not printed.Only 26 of these named storage registers are available..PPThere is a built-in square root function whoseresult is truncated to an integer (but see scaling below).The lines.DSx = sqrt(191)x.DEproduce the printed result.DS13.DE.SHBases.PPThere are special internal quantities, called `ibase' and `obase'.The contents of `ibase', initially set to 10,determines the base used for interpreting numbers read in.For example, the lines.DSibase = 811.DEwill produce the output line.DS9.DEand you are all set up to do octal to decimal conversions.Beware, however of trying to change the input base backto decimal by typing.DSibase = 10.DEBecause the number 10 is interpreted as octal, this statement willhave no effect.For those who deal in hexadecimal notation,the characters A\-F are permitted in numbers(no matter what base is in effect)and areinterpreted as digits having values 10\-15 respectively.The statement.DSibase = A.DEwill change you back to decimal input base no matter what thecurrent input base is.Negative and large positive input bases arepermitted but useless.No mechanism has been provided for the input of arbitrarynumbers in bases less than 1 and greater than 16..PPThe contents of `obase', initially set to 10, are used as the base for outputnumbers. The lines.DSobase = 161000.DEwill produce the output line.DS3E8.DEwhich is to be interpreted as a 3-digit hexadecimal number.Very large output bases are permitted, and they are sometimes useful.For example, large numbers can be output in groups of five digitsby setting `obase' to 100000.Strange (i.e. 1, 0, or negative) output bases arehandled appropriately..PPVery large numbers are split across lines with 70 characters per line.Lines which are continued end with \\.Decimal output conversion is practically instantaneous, but outputof very large numbers (i.e., more than 100 digits) with other basesis rather slow.Non-decimal output conversion ofa one hundred digit number takes aboutthree seconds..PPIt is best to remember that `ibase' and `obase' have no effectwhatever on the course of internal computation oron the evaluation of expressions, but only affect input andoutput conversion, respectively..SHScaling.PPA third special internal quantity called `scale' isused to determine the scale of calculatedquantities.Numbers may haveup to 99 decimal digits after the decimal point.This fractional part is retained in further computations.We refer to the number of digits after the decimal point ofa number as its scale..PPWhen two scaled numbers are combined bymeans of one of the arithmetic operations, the resulthas a scale determined by the following rules. Foraddition and subtraction, the scale of the result is the largerof the scales of the two operands. In this case,there is never any truncation of the result.For multiplications, the scale of the result is neverless than the maximum of the two scales of the operands,never more than the sum of the scales of the operandsand, subject to those two restrictions,the scale of the result is set equal to the contents of the internalquantity `scale'.The scale of a quotient is the contents of the internalquantity `scale'. The scale of a remainder isthe sum of the scales of the quotient and the divisor.The result of an exponentiation is scaled as ifthe implied multiplications were performed.An exponent must be an integer.The scale of a square root is set to the maximum of the scaleof the argument and the contents of `scale'..PPAll of the internal operations are actually carried out in termsof integers, with digits being discarded when necessary.In every case where digits are discarded, truncation andnot rounding is performed..PPThe contents of`scale' must be no greater than99 and no less than 0. It is initially set to 0.In case you need more than 99 fraction digits, you may arrangeyour own scaling..PPThe internal quantities `scale', `ibase', and `obase' can beused in expressions just like other variables.The line.DSscale = scale + 1.DEincreases the value of `scale' by one, and the line.DSscale.DEcauses the current value of `scale' to be printed..PPThe value of `scale' retains its meaning as anumber of decimal digits to be retained in internalcomputation even when `ibase' or `obase' are not equal to 10.The internal computations (which are still conducted in decimal,regardless of the bases) are performed to the specified numberof decimal digits, never hexadecimal or octal or anyother kind of digits..SHFunctions.PPThe name of a function is a single lower-case letter.Function names are permitted to collide with simplevariable names.Twenty-six different defined functions are permittedin addition to the twenty-six variable names.The line.DS define a(x){.DEbegins the definition of a function with one argument.This line must be followed by one or more statements,which make up the body of the function, endingwith a right brace }.Return of control from a function occurs when a returnstatement is executed or when the end of the function is reached.The return statement can take eitherof the two forms.DSreturnreturn(x).DEIn the first case, the value of the function is 0, and inthe second, the value of the expression in parentheses..PPVariables used in the function can be declared as automaticby a statement of the form.DSauto x,y,z.DEThere can be only one `auto' statement in a function and it mustbe the first statement in the definition.These automatic variables are allocated space and initializedto zero on entry to the function and thrown away on return. Thevalues of any variables with the same names outside the functionare not disturbed.Functions may be called recursively and the automatic variablesat each level of call are protected.The parameters named in a function definition are treated inthe same way as the automatic variables of that functionwith the single exception that they are given a valueon entry to the function.An example of a function definition is.DS define a(x,y){ auto z z = x*y return(z) }.DEThe value of this function, when called, will be theproduct of itstwo arguments..PPA function is called by the appearance of its namefollowed by a string of arguments enclosed inparentheses and separated by commas.The resultis unpredictable if the wrong number of arguments is used..PPFunctions with no arguments are defined and called usingparentheses with nothing between them: b()..PPIf the function.ft Ia.ftabove has been defined, then the line.DSa(7,3.14).DEwould cause the result 21.98 to be printed and the line.DSx = a(a(3,4),5).DEwould cause the value of x to become 60..SHSubscripted Variables.PPA single lower-case letter variable namefollowed by an expression in brackets is called a subscriptedvariable (an array element).The variable name is called the array name and the expressionin brackets is called the subscript.Only one-dimensional arrays arepermitted. The names of arrays are permitted tocollide with the names of simple variables and function names.Any fractionalpart of a subscript is discarded before use.Subscripts must be greater than or equal to zero and less than or equal to 2047..PPSubscripted variables may be freely used in expressions, infunction calls, and in return statements..PPAn array name may be used as an argument to a function,or may be declared as automatic ina function definition by the use of empty brackets:.DSf(a[\|])define f(a[\|])auto a[\|].DEWhen an array name is so used, the whole contents of the arrayare copied for the use of the function, and thrown away on exitfrom the function.Array names which refer to whole arrays cannot be usedin any other contexts..SHControl Statements.PPThe `if', the `while', and the `for' statementsmay be used to alter the flow within programs or to cause iteration.The range of each of them is a statement ora compound statement consisting of a collection ofstatements enclosed in braces.They are written in the following way.DSif(relation) statementwhile(relation) statementfor(expression1; relation; expression2) statement.DEor.DSif(relation) {statements}while(relation) {statements}for(expression1; relation; expression2) {statements}.DE.PPA relation in one of the control statements is an expression of the form.DSx>y.DEwhere two expressions are related by one of the six relationaloperators <, >, <=, >=, ==, or !=.The relation ==stands for `equal to' and != stands for `not equal to'.The meaning of the remaining relational operators isclear..PPBEWARE of using = instead of == in a relational. Unfortunately,both of them are legal, so you will not get a diagnosticmessage, but = really will not do a comparison..PPThe `if' statement causes execution of its rangeif and only if the relation is true.Then control passes to the next statement in sequence..PPThe `while' statement causes execution of its rangerepeatedly as long as the relationis true. The relation is tested before each executionof its range and if the relationis false, control passes to the next statement beyond the rangeof the while..PPThe `for' statement beginsby executing `expression1'. Then the relation is testedand, if true, the statements in the range of the `for' are executed.Then `expression2' is executed. The relation is tested, and so on.The typical use of the `for' statement is for a controlled iteration,as in the statement.DSfor(i=1; i<=10; i=i+1) i.DEwhich will print the integers from 1 to 10.Here are some examples of the use of the control statements..DSdefine f(n){auto i, xx=1for(i=1; i<=n; i=i+1) x=x*ireturn(x)}.DEThe line.DS f(a).DEwill print.ft Ia.ftfactorial if.ft Ia.ftis a positive integer.Here is the definition of a function which willcompute values of the binomial coefficient(m and n are assumed to be positive integers)..DSdefine b(n,m){auto x, jx=1for(j=1; j<=m; j=j+1) x=x*(n\-j+1)/jreturn(x)}.DEThe following function computes values of the exponential functionby summing the appropriate serieswithout regard for possible truncation errors:.DSscale = 20define e(x){ auto a, b, c, d, n a = 1 b = 1 c = 1 d = 0 n = 1 while(1==1){ a = a*x b = b*n c = c + a/b n = n + 1 if(c==d) return(c) d = c }}.DE.SHSome Details.PPThere are some language features that every user should knowabout even if he will not use them..PPNormally statements are typed one to a line. It is also permissibleto type several statements on a line separated by semicolons..PPIf an assignment statement is parenthesized, it then hasa value and it can be used anywhere that an expression can.For example, the line.DS(x=y+17).DEnot only makes the indicated assignment, but also prints theresulting value..PPHere is an example of a use of the value of anassignment statement even when it is not parenthesized..DSx = a[i=i+1].DEcauses a value to be assigned to x and also increments ibefore it is used as a subscript..PPThe following constructs work in BC in exactly the same manneras they do in the C language. Consult the appendix or theC manuals [2] for their exact workings..DS.ta 2ix=y=z is the same as x=(y=z)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -