music
OSdata.com: programming text book 

OSdata.com

addition

summary

    This subchapter looks at addition.

free computer programming text book project

table of contents
If you like the idea of this project,
then please donate some money.
more information on donating

Google

addition

    This subchapter looks at addition.

COBOL

    The ADD statement is used for addition.

    In a simple version of the ADD statement, you name two variable identifiers that are to be added and the variable identifier where the resulting answer is to be stored:

005010     ADD OLD-SUBTOTAL, CURRENT-COUNT
005020           GIVING NEW-SUBTOTAL.

before execution
OLD-SUBTOTAL   CURRENT-COUNT   WORK AREA   NEW-SUBTOTAL
1 1 1 1 1   2 2 4 4 4                        
partial computation
OLD-SUBTOTAL   CURRENT-COUNT   WORK AREA   NEW-SUBTOTAL
1 1 1 1 1   2 2 4 4 4   3 3 5 5 5            
after execution
OLD-SUBTOTAL   CURRENT-COUNT   WORK AREA   NEW-SUBTOTAL
1 1 1 1 1   2 2 4 4 4   3 3 5 5 5   3 3 5 5 5

    This example adds whatever number is stored in OLD-SUBTOTAL (11,111) with whatever number is stored in CURRENT-COUNT (22,444) and places the answer into NEW-SUBTOTAL (33,555).

    The word ADD must appear in Area B of the COBOL card. The statement is terminated wiith a period.

    There can be any number of fields (items named by identifiers) added together (possibly a huge list). The commas between identifiers are optional, but should be used to make your program more readable.

    It is possible to place the answer into more than one variable (again the commas are optional).

005010     ADD OLD-SUBTOTAL, CURRENT-COUNT
005020           GIVING NEW-SUBTOTAL, MASTER-COUNT.

before execution
OLD-SUBTOTAL   CURRENT-COUNT   WORK AREA   NEW-SUBTOTAL   MASTER-COUNT
1 1 1 1 1   2 2 4 4 4                                    
partial computation
OLD-SUBTOTAL   CURRENT-COUNT   WORK AREA   NEW-SUBTOTAL   MASTER-COUNT
1 1 1 1 1   2 2 4 4 4   3 3 5 5 5                        
after execution
OLD-SUBTOTAL   CURRENT-COUNT   WORK AREA   NEW-SUBTOTAL   MASTER-COUNT
1 1 1 1 1   2 2 4 4 4   3 3 5 5 5   3 3 5 5 5   3 3 5 5 5

    In the example, OLD-SUBTOTAL and CURRENT-COUNT are added together in a temporary storage area created by the compiler, and then the same answer is placed into both NEW-SUBTOTAL and MASTER-COUNT.

    It is possible to use literals in the ADD statement.

005010     ADD 58, CURRENT-COUNT
005020           GIVING NEW-COUNT.

    The literal 58 (the actual number) is added to the contents of the variable CURRENT-COUNT and the answer is stored in the vaariable NEW-COUNT.

005010     ADD 20, 30, 40
005020           GIVING SUB-TOTAL.

    The literal numbers 20, 30, and 40 are added together and the answer (90) is stored in the variable SUB-TOTAL.

005010     ADD 30.00, 40.00
005020           GIVING SUB-TOTAL.

    The literal numbers 20.00 and 40.00 are added together and the answer (70.00) is stored in the variable SUB-TOTAL.

    Numeric literals must have at least one digit and not more than 18 digits. Numeric literals may contain the dedimal digits 0 through 9, inclusive, the plus sign, the minus sign, and/or the decmal point. The decimal point may not be the right most character. An unsigned numeric literal is assumed to be pisitive. A numeric literal without a decimal point is assumed to be a whole number.

    The TO option may be used instead of the GIVING option.

005010     ADD CURRENT-COUNT TO MASTER-COUNT.

before execution
CURRENT-COUNT   WORK AREA   MASTER-COUNT
1 1 2 2 2               2 2 4 4 4
partial computation
CURRENT-COUNT   WORK AREA   MASTER-COUNT
1 1 2 2 2   3 3 6 6 6   2 2 4 4 4
after execution
CURRENT-COUNT   WORK AREA   MASTER-COUNT
1 1 2 2 2   3 3 6 6 6   3 3 6 6 6

    The number stored in the variable CURRENT-COUNT (11,222) is added to the number stored in the variable MASTER-COUNT (22,444) and the answer (33,666) is stored in MASTER-COUNT.

    It is possible to use numeric literals with this option also.

005010     ADD 1 TO LINE-COUNT.

    The example increments (adds one to) the variable LINE-COUNT.

    A more complicated version of the ADD statement with the TO option:

005010     ADD HERB-COUNT, SPICE-COUNT TO INGREDIENT-COUNT, FLAVORING-COUNT.

before execution
HERB-COUNT   SPICE-COUNT   WORK AREA   INGREDIENT-COUNT   FLAVORING-COUNT
0 0 1 1 1   0 0 2 2 2               1 2 3 4 5   0 0 1 2 3
first partial computation
HERB-COUNT   SPICE-COUNT   WORK AREA   INGREDIENT-COUNT   FLAVORING-COUNT
0 0 1 1 1   0 0 2 2 2   0 0 3 3 3   1 2 3 4 5   0 0 1 2 3
first computation complete
HERB-COUNT   SPICE-COUNT   WORK AREA   INGREDIENT-COUNT   FLAVORING-COUNT
0 0 1 1 1   0 0 2 2 2   0 0 3 3 3   1 2 6 7 8   0 0 1 2 3
after execution
HERB-COUNT   SPICE-COUNT   WORK AREA   INGREDIENT-COUNT   FLAVORING-COUNT
0 0 1 1 1   0 0 2 2 2   0 0 3 3 3   1 2 6 7 8   0 0 4 5 6

    In the example, the numbers stored in HERB-COUNT (111) and SPICE-COUNT (222) are added together and temporarily stored in a compiler generated work area (333). The subtotal in the work area (333) is then added to the number stored in the variable INGREDIENT-COUNT (12,345), with the result stored in INGREDIENT-COUNT (12,678). The subtotal in the work area (333) is then added separately to the number stored in the variable FLAVORING-COUNT (123) and this (in this case, a different answer) is stored in the variable FLAVORING-COUNT (456).

    The ROUNDED option may be placed after any identifier that follows the GIVING option or the TO option. This rounds off the results of the ADD before the answer is stored.

005010     ADD OLD-SUBTOTAL, CURRENT-COUNT
005020           GIVING NEW-SUBTOTAL ROUNDED, MASTER-COUNT.

    The addition of OLD-SUBTOTAL and CURRENT-COUNT is stored into both NEW-SUBTOTAL and MASTER-COUNT, but the answer stored in NEW-SUBTOTAL is rounded, while the answer stored in MASTER-COUNT is not rounded.

005010     ADD CURRENT-COUNT TO MASTER-COUNT ROUNDED.

    The numbers stored in the variables CURRENT-COUNT and MASTER-COUNT are added together, and the rounded off answer is tored in MASTER-COUNT.

    All fields added toegtehr with the ADD statement are aligned to the decimal point in each field. All additions are algebraiac, so the resulting answer will have the correct sign according to the rules of elementary algebra.

    The programmer needs to be careful about overflow. If the variable receiving the answer does not have enough digits to hold the entire answer (the variable is to small or the answer is too large, depending on how you view it), then significant digits will be lost and the variable will hold an incorrect answer. It is the responsibility of the programmer to make sure that the receiving field is large enough to hold all of the digits for the largest possible answer.

PHP

    An integer variable may be incremented by either of the following assignment statements:

    $i = $i + 1;
      -OR-
    $i++;

number systems

    Here is a little bit of college level work on addition.

    A system of numbers os considered to be closed under an operation if it reproduces itself.

    S is a set of numbers. S is closed under addition if for any two numbers a and b in the set S, the sum of a + b is also a number in the set S. It follows that any sum of two, three, four, or any finite number of elements in S also belong to S.

    The set of all natural numbers {1, 2, 3, …} is closed under addition.

    The set of all even integers {0, ±2, ±4, ±6, …} is closed under addition. The set of all odd integers is not closed under addition.

    The set of all positive real numbers is closed under addition.

    The ste of all integers is closed under addition.

    The set of all rational numbers is closed under addition.

    The set of all real numbers is closed under addition.

    The set of all complex numbers is closed under addition.

functions

APL    +(x) — APL dyadic function that returns the sum (addition) of a scalar, vector, or matrix. Form is A+B.
LISP    ADD1(x) — LISP function that takes one argument of type fixed or float and increments the value.
PL/I    ADD(x,y,p[.q]) — Pl/I built-in function that returns the sum of two variables (x plus y) in a field of p digits with the option of q fractional digits.
PostScript    add(x) — PosScript arithmetic operator that places on the top of the stack the sum of the two numbers previously on the top of stack. Returns an integer if both operands are integers and the result is within the integer range, otherwise returns a real.

PL/I    SUM(x) — Pl/I built-in function that returns the total sum of all of the values of the elements in an arithmetic array x. Note that the IBM PL/I-F compiler converts all array values to floating point to perform the operation and returns a result in floating point. This may create small rounding errors for commercial programmers needing dollar and cents accuracy.

assembly language instructions

    For most processors, integer arithmetic is faster than floating point arithmetic. This can be reversed in special cases such digital signal processors.

    On many processors, floating point arithmetic is in an optional unit or optional coprocessor rather than being included on the main processor. This allows the manufacturer to charge less for the business machines that don’t need floating point arithmetic.

    The basic four integer arithmetic operations are addition, subtraction, multiplication, and division. Arithmetic operations can be signed or unsigned (unsigned is useful for effective address computations). Some older processors don’t include hardware multiplication and division. Some processors don’t include actual multiplication or division hardware, instead looking up the answer in a massive table of results embedded in the processor.

    A specialized, but common, form of addition is an increment instruction, which adds one to the contents of a register or memory location. For address computations, “increment” may mean the addition of a constant other than one. Some processors have “short” or “quick” addition instructions that extend increment to include a small range of positive values.

See also Integer Arithmetic Instructions in Assembly Language and Floating Point Arithmetic Instructions in Assembly Language


free music player coding example

    Coding example: I am making heavily documented and explained open source code for a method to play music for free — almost any song, no subscription fees, no download costs, no advertisements, all completely legal. This is done by building a front-end to YouTube (which checks the copyright permissions for you).

    View music player in action: www.musicinpublic.com/.

    Create your own copy from the original source code/ (presented for learning programming).


return to table of contents
free downloadable college text book

view text book
HTML file

Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.

previous page next page
previous page next page

free computer programming text book project

Building a free downloadable text book on computer programming for university, college, community college, and high school classes in computer programming.

If you like the idea of this project,
then please donate some money.

send donations to:
Milo
PO Box 1361
Tustin, California 92781

Supporting the entire project:

    If you have a business or organization that can support the entire cost of this project, please contact Pr Ntr Kmt (my church)

more information on donating

Some or all of the material on this web page appears in the
free downloadable college text book on computer programming.


Google


Made with Macintosh

    This web site handcrafted on Macintosh computers using Tom Bender’s Tex-Edit Plus and served using FreeBSD .

Viewable With Any Browser


    †UNIX used as a generic term unless specifically used as a trademark (such as in the phrase “UNIX certified”). UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd.

    Names and logos of various OSs are trademarks of their respective owners.

    Copyright © 2010 Milo

    Created: October 31, 2010

    Last Updated: December 29, 2010


return to table of contents
free downloadable college text book

previous page next page
previous page next page