DIVIDE

This document covers the following topics:

Related Statements: ADD | COMPRESS | COMPUTE | EXAMINE | MOVE | MOVE ALL | MULTIPLY | RESET | SEPARATE | SUBTRACT

Belongs to Function Group: Arithmetic and Data Movement Operations


Function

The DIVIDE statement is used to divide an arithmetic expression or operand into two operands.

Note:
Concerning Division by Zero: If an attempt is made to use a divisor (operand1) which is zero, either an error message or a result equal to zero will be returned; this depends on the setting of the session parameter ZD (described in the Parameter Reference documentation).

Syntax 1 - DIVIDE Statement without GIVING Clause

DIVIDE [ROUNDED]

../graphics/cbo2.gif

(arithmetic-expression)
operand1

../graphics/cbc2.gif

INTO operand2

For explanations of the symbols used in the syntax diagrams, see Syntax Symbols.

Operand Definition Table:

Operand Possible Structure Possible Formats Referencing Permitted Dynamic Definition
operand1 C S A   N   N P I F               yes no
operand2   S A   M   N P I F               yes no

Syntax Element Description:

Syntax Element Description
arithmetic-expression See Arithmetic Expression in the COMPUTE statement.
operand1 INTO operand2
Operands:

operand1 is the divisor, operand2 is the dividend. The result is stored in operand2 (result field), hence the statement is equivalent to:

operand2 := operand2 / operand1

If an arithmetic-expression is used, operand2 must not be an array range.

The number of decimal positions for the result of the division is evaluated from the result field (that is, operand2).

For the precision of the result, see Rules for Arithmetic Assignments, Precision of Results of Arithmetic Operations in the Programming Guide.

ROUNDED
ROUNDED Option:

If you specify the keyword ROUNDED, the result will be rounded.

For information on rounding, see Rules for Arithmetic Assignment, Field Truncation and Field Rounding in the Programming Guide.

Syntax 2 - DIVIDE Statement with GIVING Clause

DIVIDE [ROUNDED]

../graphics/cbo2.gif

(arithmetic-expression)
operand1

../graphics/cbc2.gif

INTO

../graphics/cbo2.gif

(arithmetic-expression)
operand2

../graphics/cbc2.gif

GIVING operand3

For explanations of the symbols used in the syntax diagram, see Syntax Symbols.

Operand Definition Table:

Operand Possible Structure Possible Formats Referencing Permitted Dynamic Definition
operand1 C S A   N     N P I F               yes no
operand2 C S A   N     N P I F               yes no
operand3   S A     A U N P I F B*             yes yes

* Format B of operand3 may be used only with a length of less than or equal to 4.

Syntax Element Description:

Syntax Element Description
arithmetic-expression See Arithmetic Expression in the COMPUTE statement.
operand1 INTO operand2 GIVING operand3
Operands:

operand1 is the divisor, operand2 is the dividend.

The result of the division is stored in operand3, hence the statement is equivalent to:

operand3 := operand2 / operand1

The number of decimal positions for the result of the division is evaluated from the result field (that is, operand3).

For the precision of the result, see Rules for Arithmetic Assignments, Precision of Results of Arithmetic Operations in the Programming Guide.

ROUNDED
ROUNDED Option:

If you specify the keyword ROUNDED, the result will be rounded.

For information on rounding, see Rules for Arithmetic Assignment, Field Truncation and Field Rounding in the Programming Guide.

Syntax 3 - DIVIDE Statement with REMAINDER Clause

DIVIDE

../graphics/cbo2.gif

(arithmetic-expression)
operand1

../graphics/cbc2.gif

INTO

../graphics/cbo2.gif

(arithmetic-expression)
operand2

../graphics/cbc2.gif

[GIVING operand3] REMAINDER operand4

For explanations of the symbols used in the syntax diagrams, see Syntax Symbols.

Operand Definition Table:

Operand Possible Structure Possible Formats Referencing Permitted Dynamic Definition
operand1 C S A   N     N P I                 yes no
operand2 C S A   N     N P I                 yes no
operand3   S A     A U N P I F B*    T         yes yes
operand4   S A     A U N P I F B*    T         yes yes

* Format B of operand3 and operand4 may be used only with a length of less than or equal to 4.

Syntax Element Description:

Syntax Element Description
arithmetic-expression See Arithmetic Expression in the COMPUTE statement.

operand1
operand2

Operands:

operand1 is the divisor, operand2 is the dividend.

If the GIVING clause is not used, the result is stored in operand2.

If operand2 is a constant or a non-modifiable Natural system variable, the GIVING clause is required.

GIVING operand3
GIVING Clause:

If this clause is used, operand2 will not be modified and the result will be stored in operand3.

The number of decimal positions for the result of the division is evaluated from the result field (that is, operand2 if no GIVING clause is used, or operand3 if the GIVING clause is used).

For the precision of the result, see Rules for Arithmetic Assignments, Precision of Results of Arithmetic Operations (in the Programming Guide).

REMAINDER operand4
REMAINDER Clause:

The remainder of the division is placed into the field specified in operand4.

  • If the GIVING clause is used, the statement is equivalent to:

    operand3 := operand2 / operand1
    operand4 := operand2 - (operand3 * operand1)

    None of the four operands may be an array range.

  • If the GIVING clause is not used, the statement is equivalent to:

    temporary := operand2
    operand2 := operand2 / operand1
    operand4 := temporary - (operand2 * operand1)

    where temporary is a temporary field with the same format/length as operand2.

For each of these steps, the rules described in Precision of Results of Arithmetic Operations in the Programming Guide apply.

Example

** Example 'DIVEX1': DIVIDE                                             
************************************************************************
DEFINE DATA LOCAL                                                       
1 #A (N7) INIT <20>                                                     
1 #B (N7)                                                               
1 #C (N3.2)                                                             
1 #D (N1)                                                               
1 #E (N1) INIT <3>                                                      
1 #F (N1)                                                               
END-DEFINE                                                              
*                                                                       
DIVIDE 5 INTO #A                                                      
WRITE NOTITLE 'DIVIDE 5 INTO #A' 20X '=' #A                             
*                                                                       
RESET INITIAL #A                                                        
DIVIDE 5 INTO #A GIVING #B                                          
WRITE 'DIVIDE 5 INTO #A GIVING #B' 10X '=' #B                           
*                                                                       
DIVIDE 3 INTO 3.10 GIVING #C
WRITE 'DIVIDE 3 INTO 3.10 GIVING #C' 8X '=' #C        
*                                                     
DIVIDE 3 INTO 3.1 GIVING #D                     
WRITE 'DIVIDE 3 INTO 3.1 GIVING #D' 9X '=' #D         
*                                                     
DIVIDE 2 INTO #E REMAINDER #F                    
WRITE 'DIVIDE 2 INTO #E REMAINDER #F' 7X '=' #E '=' #F
*                                                     
END

Output of Program DIVEX1:

DIVIDE 5 INTO #A                    #A:        4 
DIVIDE 5 INTO #A GIVING #B          #B:        4 
DIVIDE 3 INTO 3.10 GIVING #C        #C:    1.03  
DIVIDE 3 INTO 3.1 GIVING #D         #D:  1       
DIVIDE 2 INTO #E REMAINDER #F       #E:  1 #F:  1