2 Calculations in BASIC

2.1 Variables and numeric expressions

2.1.1 Numeric expressions

Four operations (addition, subtraction, multiplication and division) are expressed by +, -, *, / , respectively. An exponentiation ab is expressed by a^b using a circumflex accent, or a caret(^). For example, 23 is expressed as 2^3.
When an expression includes different operations, exponentiations are executed first, multiplications and divisions are executed next, and then additions and subtractions are executed. Operations of same priority are executed in order from left to right.

PRINT statement is used to display the result of a calculation.

10 PRINT 2+3*4^2
20 PRINT 3/4*5
30 PRINT 2^4^5
40 END

Line 10 is to calculate 2+3×42.
Line 20 is to calculate 3÷4×5. (Note that multiplication and division have same priority.)
Line 30 is to calculate (24)5. (Note that exponentiations are executed in order from left to right.)

2.1.2 Parenthesis

To change the priority of operations, you can use parentheses. Double, triple, … parentheses are permitted, but only round brackets '(' and ')' are used.
When a term starting with a negative sign is written in an expression, it must be parenthesized.

 
10 PRINT ((2+3)*4)^2
20 PRINT 3*(-4)
30 END

Line 10 is to calculate {(2+3)×4}2.
Line 20 is to calculate 3×(-4).

2.1.3 Line Numbers

The number at the left end of each line of the program is called the line number. Line numbers can be omitted on Decimal BASIC. In this article, line numbers are written for the purpose of maintaining consistency with the standard, but it is advisable to omit line numbers when you prepare programs using Decimal BASIC.

2.1.4 Variables

A storage location of a value in the computer is called a variable. Variables are named A, B, C, and so on. Long variable name, such as LEFT or RIGHT also can be used. For the details of naming rule, see the standard.
Either uppercase letters or lowercase letters can be used, but the differences in case are ignored. For example, both 'A' and 'a' stand for the same variable.

Storing a numerical value into a variable is called assignment. If a new numerical value is stored into a variable, numerical values held before will cease to exist and cannot be referred to.
A LET statement is used to assign a numerical value to a variable.

10 LET X=10
20 PRINT 2*X^2+3*X+4
30 END

If a variable name is used in an expression as shown above, the variable name indicates the numerical value stored in the variable.
The LET statement is written in the form as follows:
LET variable_name = numerical_expression
When a LET statement is executed, the right-hand value is calculated and substituted for the variable written on the left-hand.

[Note]
The symbol of multiplication cannot be omitted on BASIC. Even if 'xy' is written intending x*y, 'xy' is interpreted as one variable different from x or y.

Question 1. If the following program is executed, what is the result?

10 LET A=10
20 LET A=A+1
30 PRINT A
40 END

Decimal BASIC has capability of step execution.
Click to start the program in the mode of executing one line at a time.
On the dialog in the figure below, one line is executed at every time the Enter key is pressed or the OK button is clicked.

In the Debug window, the statement to be executed and the value of each variable are displayed. Every time one line is executed, watch how the value of the variable changes.
If you click the arrow at the lower part of the Debug window, you can see the history retrospectively.

2.1.5 INPUT statement

Use an INPUT statement to assign a value to a variable during execution.
The variables that shall be input on execution are written punctuated by commas following INPUT.

The following program finds the product of 2 input numbers.

10 INPUT A,B
20 PRINT A*B
30 END

When this program is executed, the dialog as shown in the figure will be displayed. Type 2 numbers punctuated by a comma and then press the Enter key.

2.1.6 Large Numbers and Small Numbers

When the following has been executed,

10 PRINT 1/7
20 END

the following is displayed.

 .142857142857143

If the absolute value of a calculation result is smaller than 1, it is displayed omitting a zero preceding the decimal point.

Furthermore, after the execution of the following,

10 PRINT 2^100,2^(-100)
20 END

the following shall be displayed.

 1.26765060022823E30   7.88860905221012E-31

These stand for 1.26765060022823×1030,7.88860905221012×10-31, respectively.

2.2 PRINT statement

2.2.1 Character String

A character string can be displayed using a PRINT statement.
A character string is written enclosed with quotation marks.

10 PRINT "ABC"
20 END

If Line 10 is PRINT ABC, it means that the value of a numerical variable ABC is displayed.

2.2.2 Item delimiters (comma and semicolon)

When calculation result is output, the format can be controlled in the PRINT statement.

Multiple items can be written in a PRINT statement by delimiting with commas or semicolons.
If a semicolon is used as an item delimiter, the items are displayed closely. If a comma is used for an item delimiter, the items are output from a certain column by outputting extra space characters.
For example, if the following is executed:

10 PRINT 3; 3^2, 1/3, 2*3
20 END

the display is made as follows.

 3  9                    .333333333333333        6 



An ordinary PRINT statement makes a line break at the last. If you want not to make a line break, write a comma or a semicolon at the tail of the PRINT statement.
For example,

10 PRINT 1;2;3;
20 PRINT 4;5
30 END

yields the following result.

1  2  3  4  5 


A PRINT statement with no output items only makes a line break.
For example,

10 PRINT 1/3
20 PRINT
30 PRINT 3^2
40 END

yields the following.

.333333333333333 

 9

2.3 FOR~NEXT

2.3.1 FOR~NEXT block

FOR~NEXT is a repetitive structure, which is often used in application programs of BASIC. FOR and NEXT are keywords always used as a pair.
A FOR~NEXT block repeatedly executes the lines put between the FOR line and the NEXT line while successively changing the value of the variable specified in the FOR statement.

The program shown below calculates n2 for n=1,2,3,…10.

Example 1.

10 FOR n=1 TO 10
20    PRINT n,n^2
30 NEXT n
40 END

On Example 1, line 20 is executed for n=1 first and then line 20 is executed for n=2. Then line 20 is executed for n=3. The same thing is sequentially repeated, and finally line 20 is executed for n=10.
Select the step execution and try to check how the value of variable changes and execution sequence.

To investigate the characteristic of FOR~NEXT in detail, try to execute the following program.

Example 2.

10 INPUT n
20 FOR k=1 TO n
30    PRINT k
40 NEXT k
50 PRINT "Last",k
60 END

If you execute this program and enter 10 for n, you will see that the value of k becomes 11 when the PRINT statement of line 50 is executed. In the above program, 1 is substituted for k first, and each time the NEXT statement is executed, 1 is added for the value of k, and when it becomes larger than n, the repetition finishes and the control proceeds to the next line of the NEXT statement.
And now, what happens if 0 is entered for n? In this case, line 30 is not executed and only line 50 is executed. The value of k in this example is 1. In this case, since the value of k is larger than n from the beginning, the control proceeds to line 50 without executing line 30 and the NEXT statement. This characteristic is important and should be remembered.

2.3.2 Applications of FOR~NEXT (the sum or product of a sequence)

On the following program, 12+22+32+…+n2 is calculated when a natural number n is entered through the keyboard.

Example 3

10 INPUT n
20 LET S=0
30 FOR k=1 TO n
40    LET S=S+k^2
50 NEXT k
60 PRINT S
70 END

The sum is calculated using variable S. First, 0 is substituted for S in line 20, and then k2 is added to S for k=1,2,3,…,n in lines 30 to 50.


The same method can be used for calculating the product of a sequence. The number of permutations nPr (=n(n-1)(n-2)…(n-r+1)) can be calculated as follows:
Example 4.

10 INPUT n,r
20 LET p=1
30 FOR k=(n-r+1) TO n
40    LET p=p*k
50 NEXT k
60 PRINT p
70 END


[Note] The initial value of a variable is 0 in Decimal BASIC, but the standard does not provide so. In order to make a compatible program, line 20 cannot be omitted in Example 3.

2.3.3 Applications of FOR~NEXT (sequence defined in recurrence equations)

Use the method of successively updating the values of a variable, and you can calculate the sequence defined in a recurrence equation.
The following program calculates the n-th term of a sequence {an} defined by a1=5,an+1=3an+2.
Example 5.

10 INPUT n
20 LET a=5
30 FOR k=2 TO n
40    LET a=3*a+2
50 NEXT k
60 PRINT a
70 END

With this program, the correct answer can be obtained even in the case of n=1. This is because the value of k exceeds n when 2 is substituted for k and line 40 is not executed at all.

2.3.4 STEP

In a FOR~NEXT block, the numerical value added to the control variable each time can be made other than 1.
In the following program, x2 is calculated while adding the value of x in units of 0.1 from 0 to 1.

Example 6.

10 FOR x=0 TO 1 STEP 0.1
20   PRINT x,x^2
30 NEXT x
40 END


This syntax can be used for the purpose of changing the numerical value from a large value to a small value.

Example 7.

10 FOR k=10 TO 1 STEP -1
20   PRINT k
30 NEXT k
40 END


Question 2.
In the following program, it seems that the input of an even number for n is assumed, but an odd number can also be entered. How does it operate?

10 INPUT n
20 FOR k=0 TO n STEP 2
30   PRINT k
40 NEXT k
50 END

2.4 DEF statement

Let’s make a table of function values using the capability of FOR~NEXT.
With the following program, the function value is calculated by changing the value of x in units of 0.1 from -4 to 4 for the function f ( x ) = x3 -3x +1.
Example 8.

10 DEF f(x)=x^3-3*x+1
20 FOR x=-4 TO 4 STEP 0.1
30    PRINT x,f(x)
40 NEXT x
50 END

If this program is executed, the results are displayed as shown in the figure, and the entire output can be seen by operating the scroll bar at the right end with the mouse.

The DEF statement of line 10 is an instruction to define the function. The rule of naming a function is the same as for a variable. But no identical name can be used for a function name and a variable name. In line 10, f is used as the function name. The function name is followed by parentheses and a variable is written in the parentheses. Following the equal sign, the formula to calculate is written using the variable written in the parentheses. The variable written in the parentheses on the left side is different from the variable used in other part of the program (that is, the storage location of the numerical value is different).

2.5 Supplied Functions (Built-in Functions)

2.5.1 Square roots and absolute values

In BASIC, is written as SQR(x). The absolute value |x| of x is expressed by ABS(x).
[Note] SQR and ABS are abbreviations of SQuare Root and ABSolute, respectively.

The length of the hypotenuse of a right triangle with sides of length a and b is .
The program to enter a and b and find is as follows:
Example 9.

10 INPUT a,b
20 PRINT SQR(a^2+b^2)
30 END

2.5.2 Trigonometric functions

The sine, cosine, and tangent of x can be found using SIN(x), COS(x) and TAN(x), respectively.
The unit of measuring angle is radian as default, but it is possible to change the angle measure to degrees by writing
OPTION ANGLE DEGREES
in the beginning of the program.
The following program finds the length of the rest side of a triangle using the cosine law, when the lengths of two sides a and b and the measure of the inclued angle C are entered.
Example 10.

10 OPTION ANGLE DEGREES
20 INPUT a,b,C
30 PRINT SQR(a^2+b^2-2*a*b*COS(C))
40 END

2.5.3 Inverse trigonometric functions

Since the formula of the cosine law can be solved for cos A as

the measure of the angle A can be found, given the lengths a,b,c of three sides of a triangle ABC.
To find the corresponding A from the value of cos A, the built-in function ACOS(x) of BASIC can be used.
ACOS(x) is a built-in function to find t to become cos t°= x in the range of 0≦t≦180.

Example 11.
A program to find A by entering a,b,c.

10 OPTION ANGLE DEGREES
20 INPUT a,b,c
30 PRINT ACOS((b^2+c^2-a^2)/(2*b*c))
40 END

The following shows the execution result when a=7, b=3, and c=5 are entered.

 ? 7,3,5
 120

As similar built-in functions, ASIN(x), ATN(x) and ANGLE(x,y) are available.

ASIN(x) finds t such that sin t゚= x and -90≦t≦90.

ATN(x) finds t such that tan t゚= x and -90<t<90.

ANGLE(x,y) finds the angle between the positive x-axis and the vector joining the origin and the point (x, y) in the range of -180<t≦180.

2.5.4 INT function and MOD function

INT(x) is the largest integer not exceeding x. If n is an integer, INT(n) agrees with n, but if it has a fraction part, it is truncated in the negative direction. For example, INT(2.3)=2, INT(-2.3)=-3.

MOD(a,b) is a remainder when a is divided by b. It is defined by MOD(a,b)=a-b*INT(a/b). For example, MOD(5,3)=2, MOD(-4,3)=2, MOD(1.3, 0.4)=0.1. For a positive number b, 0≦MOD(a,b)

2.5.5 PI function and so on

IN BASIC, some built-in functions are prepared to stand for special constants.
PI is the approximate value of the ratio of the circumference of a circle to its diameter (π).
MAXNUM is the largest number that can be handled with BASIC.


Example 12.

10 PRINT PI, MAXNUM
20 END

2.5.6 Random Numbers(RND function)

RND is a special function. This function has no parameter, but each time a calculation is made, a different value is returned. For example, try to execute the following program.

Example 13

10 FOR k=1 TO 10
20 PRINT RND
30 NEXT k
40 END

The numerical value returned by the RND function is known as the random number. The RND function generates random values without bias in the range of 0 to less than 1. The above program returns the same result each time the execution is made. If you want the random number of a different series each time the execution is made, execute the RANDOMIZE statement before executing the RND function as shown below.
To use the RND function in place of a die, do as follows. With this program, integers of 1 to 6 are obtained by multiplying the value of RND by 6, adding 1, and taking out the integer part.

10 RANDOMIZE
20 FOR n=1 TO 20
30     PRINT INT(RND*6+1)
40 NEXT n
50 END

2.6 Graphics

2.6.1 Graphs of functions

Use graphics to draw a graph of a function.

Example 14
A program that draws the graph of the function y=x3-3x+1 in the range of -4≤x≤4 and -4≤y≤4.

10 DEF f(x)=x^3-3*x+1 
20 SET WINDOW -4,4,-4,4
30 DRAW GRID
40 FOR x=-4 TO 4 STEP 0.1
50    PLOT LINES: x,f(x);
60 NEXT x
70 END

After the execution of the program above, the figure show below is obtained.
When the mouse cursor is moved on the window, the coordinates of the point are displayed at the bottom.

The shape of the drawing pane is square. The number of pixels is determined adequately from the size of the screen by BASIC system, but you can select another pixel size in the menu.

The SET WINDOW statement at line 20 sets the coordinate system in the drawing area such that the range of x coordinates is from -4 to 4, and the range of y coordinates is from -4 to 4.
The DRAW statement at line 30 draws a grid. This feature is not specified in the standard, but the same function can be realized by assembling the instruction specified in the standard.
The PLOT LINES statement at line 50 is used to draw a graph of the function by drawing line segments to connect points (x, x3-3x+1) successively.
Generally, when PLOT LINES: x,y; is written, the points indicated by this statement and the PLOT LINES statement to be executed next shall be connected with a line segment.

2.6.2 SET WINDOW

     SET WINDOW  x1 , x2 , y1 , y2

sets the coordinate system with the left end x1, right end x2, bottom end y1 and top end y2.
When a geometric figure is drawn, x2-x1=y2-y1 is normally required, but in the case of the function graph in which the vertical axis and horizontal axis express amounts different in property, it is not required.
For example, the graph of a sine function with the degree measure is drawn as follows:
Example 15

10 OPTION ANGLE DEGREES
20 DEF f(x)=sin(x)
30 SET WINDOW -360,360,-4,4
40 DRAW GRID(90,1)
50 FOR x=-360 TO 360
60    PLOT LINES: x,f(x);
70 NEXT x
80 END

OPTION ANGLE DEGREES at line 10 must be written before the lines where SIN(x) functions are written. The grid(a,b) used in line 40 is a grid of horizontal gaps a, vertical gaps b.

2.6.3 PLOT LINES

PLOT LINES is a statement to draw a polygonal line by connecting specified points with line segments. The points are specified by delimiting the x coordinates and y coordinates with a comma (,). It is also possible to specify multiple points for one PLOT LINES statement. In such a case, the points are delimited with a semicolon. It is also possible to write a semicolon at the end of the point list.
If the PLOT LINES statement executed just before ends with a semicolon, the point specified last by the PLOT LINES executed just before and the point specified first are also connected with a line segment.

For example, if PLOT LINES: x1, y1, x2, y2 is executed when the PLOT LINES statement has not been executed yet or the PLOT LINES executed just before does not end with a semicolon, a line segment connecting two points (x1, y1) and (x2, y2) is drawn.
This can also be written by dividing into 2 statements like below.

 PLOT LINES: x1 , y1 ;
 PLOT LINES: x2 , y2

For the PLOT LINES statement, A special form of PLOT LINES statement, which has no point list, is available. When the PLOT LINES statement executed just before ends with a semicolon, PLOT LINES statement with no point list is used to cancel the effect.
For example, it is used when two function graphs are drawn successively as in the following program.

Example 16.

100 DEF f(x)=x^2
110 DEF g(x)=x^3
120 SET WINDOW -4,4,-4,4
130 DRAW GRID
140 FOR x=-4 TO 4 STEP 0.1
150    PLOT LINES: x,f(x);
160 NEXT x
170 PLOT LINES
180 FOR x=-4 TO 4 STEP 0.1
190    PLOT LINES: x,g(x);
200 NEXT x
210 END

If the above program lacks line 170, the two points (4,f(40)) and (-4, g(-4)) shall be connected with a line segment.

2.6.4 Parametric Curve

It is easy to draw a curve with parametric equations. The following program draws a curve x=3cos ty=2sin t.

Example 17.

10 OPTION ANGLE DEGREES
20 DEF f(t)=3*COS(t)
30 DEF g(t)=2*SIN(t)
40 SET WINDOW -4,4,-4,4
50 DRAW grid
60 FOR t=0 TO 360
70    PLOT LINES: f(t),g(t);
80 NEXT t
90 END

2.6.5 Curves in Polar Coordinates

A polar equation r=f(θ) can be converted to a parametric form by means of x=f(θ) cosθ, y=f(θ)sinθ. The following program draws a rose curve r=sin2θ.

Example 18.

10 DEF f(t)=SIN(2*t)
20 SET WINDOW -1,1,-1,1
30 DRAW grid
40 FOR t=0 TO 2*PI STEP PI/360
50 PLOT LINES: f(t)*COS(t), f(t)*SIN(t);
60 NEXT t
70 END

2.6.6 Polar Coordinates

It is easy to convert the rectangular coordinates to the polar coordinates in BASIC.
The rectangular coordinates (x, y) can be converted to polar coordinates (r, θ) by r=SQR(x^2+y^2), θ=ANGLE(x,y). Provided -π<θ≦π.
The following program draws a locus of the point where the length of the radius vector of point P moving on a circle x=1+cos t, y=sin t is squared and the polar angle is doubled.
Example 19.

100 SET WINDOW -4,4,-4,4
110 DRAW grid
120 FOR t=0 TO 2*pi STEP pi/180
130    LET x=cos(t)+1
140    LET y=sin(t)
150    LET r=x^2+y^2
160    LET a=ANGLE(x,y)*2
170    PLOT LINES: r*cos(a),r*sin(a);
180 NEXT t
190 END

2.6.7 SET LINE COLOR

When multiple curves are drawn on the same plane of coordinates, it may be desired to change the color for each curve. The instruction to change the color of a line is SET LINE COLOR. The color is specified by number. On Decimal BASIC, colors are assigned to numbers as follows:
0 white, 1 black, 2 blue, 3 green, 4 red, 5 cyan, 6 yellow, 7 magenta, 8 gray, 9 navy, 10 dark green, 11 teal, 12 maroon, 13 olive, 14 purple, , 15 silver, ….
For example, If SET LINE COLOR 4 is executed, subsequent lines are drawn in red.

2.6.8 PLOT POINTS

When it is desired to draw a point, the shape of the point is specified by executing SET POINT STYLE. The shape of a point is specified using one of the following numbers. If it is not specified, the shape of No. 3 is used.

 1 ・    2 +      3 *     4 ○       5 ×

The color of the point can be changed with SET POINT COLOR. The instruction to dot a point is PLOT POINTS. It is written in the following form.

PLOT POINTS: x,y

2.7 Numerical Calculation

2.7.1 Square Root

We consider a method of finding an approximate value of the square root of a positive number using only the four operations.
is the length of one side of a square whose area is the same as that of a rectangle with length a and width 1 .
If the original rectangle is deformed with its area remaining in such a way that the width and the length approach each other, the length of this rectangle should approach . We adopt the average of the length and width of the former rectangle for the length of the deformed rectangle .
Denoting the length and width of a rectangle obtained at k-th time by xk and yk , respectively, we have

On the other hand, from the condition that the area remains in deforming, the width yk+1 is determined as follows.

From these two relations, the formula only concerning xk and xk+1 can be obtained as follows.

The following program finds the first 8 terms of this sequence {xk}.

Example 20.

10 INPUT a
20 LET x=a
30 FOR n=1 TO 8
40     LET x=(x+a/x)/2
50     PRINT n,x
60 NEXT n
70 END

2.7.2 Numerical values of BASIC

All numerical values are expressed as decimals in Decimal BASIC. And for such an operation that a correct value can be expressed in a 16-digit decimal, a correct result can be obtained (not guaranteed).
For example, the calculation result of SQR(36) will accurately become 6. If the angle measure is degree, the calculation result of SIN(30) will become 0.5. If the true value of the calculation result cannot be expressed in 16-digit decimals, however, the result will be an approximate value. For example, the calculation result of 1/3 contains an error.
A numerical variable has the precision of 15 digits in Decimal BASIC. If you substitute a numerical value that has over 15 significant digits for a variable, it is rounded so that it has just 15 significant digits.
On the other hand, calculation results of numerical expressions have precision of more than 16 digits. For example, the calculation result of 1/3 becomes 0.333333333333333333333333333, which has 27 significant digits.
Ordinarily, calculation results are displayed rounded to 15 digits, however, if “More Places Displayed” is checked with Option menu - Precision, all digits shall be displayed in the decimal mode.

It is due to the Full BASIC standard that number of significant digits of a numerical expression is larger than that of a numerical variable, but sometimes it causes phenomena difficult to understand. For example, the execution result of line 20 in the following program is not zero. This is because of difference in the number of digits to be rounded to.

10 LET A=1/3
20 PRINT 1/3-A
30 END

2.7.3 Cancellation of Significant Digits

10 LET A=1/3
20 PRINT A-0.33333333333333
30 END

When the above is executed, the value of A in line 20 becomes 0.333333333333333, and then the execution result becomes .000000000000003.
However, the above result may be considered that the number of significant digits dropped to 1 from the viewpoint in which the value assigned to A in 10 line is thought to be 1/3.

While the accuracy of the result of a single calculation is guaranteed by the standard, when two results of calculations at least one of whose true value can not be accurately expressed in 15 digits are operated under subtraction, such a phenomenon occurs that the number of significant digits considerably decreases if the two numbers are close to each other.
This phenomenon is cancellation of significant digits.

The derivative f' (a) of a function y=f(x) at x=a is defined by the limit of average rate of change as an increment h approaches 0 indefinitely.
The following program is to investigate how the average rate of change at x=2, for f(x)=, changes when the value of h is changed to 10-1, 10-2, 10-3, 10-4, …, 10-15.
The limit, as h approaches 0, is approximately 0.353553390593274 in theoretical calculation, but the execution result shows that it moves away from the correct value as h approaches 0. This is due to cancellation of significant digits accompanying subtraction as described above.


Example 21.

10 DEF f(x)=SQR(x)
20 FOR i=1 TO 15
30    LET h=10^(-i)
40    PRINT h, (f(2+h)-f(2))/h
50 NEXT i
60 END

Execution Result

 .1                      .349241122458488 
 .01                     .35311255026876 
 .001                    .3535092074646 
 .0001                   .353548971286 
 .00001                  .35355294866 
 .000001                 .3535533464 
 .0000001                .353553386 
 .00000001               .3535534 
 .000000001              .3535534 
 .0000000001             .353554 
 .00000000001            .35356 
 .000000000001           .3536 
 .0000000000001          .354 
 .00000000000001         .36 
 .000000000000001        .4

2.7.4 Pi (π)

Since the perimeter of the regular n-gon inscribed in a circle of diameter 1 is , denoting the perimeter of the regular 2k-gon by ak, we have

ak=
.
From the half-angle identity,
,
and so
cos90°,cos45°,cos 22.5°,…, ,…
can be calculated successively.


Even though the calculation of ak must be made if we use , actually, it is not so successful.

As k becomes large, becomes close to 0, and so becomes close to 1.
For this reason, cancellation of significant digits occurs in the calculation of 1-.

So, we choose
the relation

obtained from the double-angle identity, to calculate
sin 90°,sin 45°,…, .

The following program calculates these values successively and outputs 2k and , starting with cos 90°=0 and sin 90°=1.
Example 22.

10 LET c=0       ! cos90°
20 LET s=1       ! sin90° 
30 FOR k=2 TO 30
40    LET c=SQR((1+c)/2)
50    LET s=s/c/2
60    PRINT 2^k,2^k*s
70 NEXT k
80 END


2.7.5 PRINT USING

Format indication can be written in a PRINT statement. It is written in the following form.
PRINT USING format_string: expression,expression,…,expression
The format string includes the formats for all the expressions.
The gaps between the formats are filled with space characters.
And then the format string is written enclosed with double quotation marks.

The following form is recommended for a format. For the details of the format characters, see the standard.

  #######         for a positive integer.          
                  Write #'s of the number of places. (Right-aligned on display)
  -------%        for an integer (positive or negative). 
                  Write minus signs of the number of places, and % at the end.            
  #######.####    for a positive decimal. 
                  #'s are written for integral part and decimal part.
  -------%.####   for a decimal (positive or negative) 
     

The decimal part is rounded off to the places specified by the format.
When the format is made, care should be taken so that the number of digits of the integral part will not be short. Especially for the negative number, the place for outputting the minus sign must be secured.

Example 23

10 FOR i=0 TO 17
20    LET n=10^i
30    PRINT USING "###################  #.##############":n,(1+1/n)^n
40 NEXT i
50 END

Output result

                  1   2.00000000000000
                 10   2.59374246010000
                100   2.70481382942153
               1000   2.71692393223589
              10000   2.71814592682522
             100000   2.71826823717449
            1000000   2.71828046931938
           10000000   2.71828169254497
          100000000   2.71828181486764
         1000000000   2.71828182709990
        10000000000   2.71828182832313
       100000000000   2.71828182844545
      1000000000000   2.71828182845769
     10000000000000   2.71828182845891
    100000000000000   2.71828182845903
   1000000000000000   2.71828182845904
  10000000000000000   2.71828182845905
 100000000000000000   2.71828182845905

Example 24

10 FOR x=3.14159265 TO 3.14159266 STEP 0.000000001
20    PRINT USING "%.#########  ---%.###############":x,SIN(x)
30 NEXT x
40 END

Output result

3.141592650     0.000000003589793
3.141592651     0.000000002589793
3.141592652     0.000000001589793
3.141592653     0.000000000589793
3.141592654    -0.000000000410207
3.141592655    -0.000000001410207
3.141592656    -0.000000002410207
3.141592657    -0.000000003410207
3.141592658    -0.000000004410207
3.141592659    -0.000000005410207
3.141592660    -0.000000006410207


Next

Back