Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Negation

Status
Not open for further replies.

3gm

Programmer
Mar 7, 2001
437
US
I'm trying to put together some standards for my programming group. One of the things that has generated some discussion is "How do you negate a field?"

Generally, most of our folks do it with a multiplication:

Code:
COMPUTE A = A * -1

Others suggest:

Code:
 COMPUTE A = - A

I would be interested in arguments pro and con or any other approaches you think are useful.

Regards,

Glenn
 
I would think using the COMPUTE verb, that it would be a matter of personal preference, but at the same time would using a multiply verb generate fewer code instructions? (I ask because I don't know quite for sure - if it does I'd go that route.)
 
Glenn,

I would take the need for this (as a separate operation) as an indication that the programmer needs to rethink his implementation, and would so state in my standards.

Then, if you must actually standardize this, require using either of the two constructs you present.

Then cause to be fired anyone who argues the point more than a nanosecond, because to do so will waste more money than ever could be saved by choosing one or the other.
grumpy.gif


Tom Morrison
 
I think you are right in both statements, Tom. But even discussing this as a standard is too nit-picking for me.
 
fyi

Using IBM Enterprise COBOL on a mainframe, it generates 5 Assembler instructions to use COMPUTE and multiplying by
-1. It uses 4 instructions to use COMPUTE with the complement operator. It uses 4 instructions to use COMPUTE and subtract the number from zero. It uses 2 instructions to use a MULTIPLY statement or SUBTRACT statement.
 
Kenny - What is the syntax of the multiply? I would think it would generate one instruction if you use
Multiply -1 by A
Of course if A has an even number of digits, then there is the extra instruction to zero out the high-order nybble.

Back in the days when we worried about such things (48k OS), I found for instance that MOVE ALL 'X' TO ONE-BYTE-FIELD generated an MVI, whereas MOVE 'X' TO ONE-BYTE-FIELD generated an MVC (longer instruction and an entry in the constants area).
 
In the old days it did create one instruction. I used a Packed field with an odd number of digits and a sign - all the right things. Enterprise COBOL generates a ZAP instruction at the end of most of the calculations it does. So there is a MP and ZAP instruction when a multiply is used.
 
It also depends on platforms, compilers, optimizing options on or off....

Perhaps your compiler can also generate assembler instructions!

for example, compile this:

Code:
       IDENTIFICATION DIVISION.
       PROGRAM-ID. TEST.
       ENVIRONMENT DIVISION.
       CONFIGURATION SECTION.
       SOURCE-COMPUTER. IBM-PC.
       OBJECT-COMPUTER. IBM-PC.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       DATA DIVISION.
       FILE SECTION.
       WORKING-STORAGE SECTION.
       01 A     PIC S9(4) COMP-5 VALUE  1.
       PROCEDURE DIVISION.
            ENTRY 'LABEL1'.
            MULTIPLY -1 BY A.
            ENTRY 'LABEL2'.
            COMPUTE A = -A.
            ENTRY 'LABEL3'.
            COMPUTE A = -1 * A.
            ENTRY 'LABEL4'.
            MULTIPLY -1 BY A GIVING A.
            ENTRY 'LABEL5'.
            GOBACK.

This is mainframe dialect. In the generated assemler, you will recognize the entries. The instructions between, are translated cobol.

But most import of setting any standard is not the performance, but if the source is easy to maintain! CPU time is much cheaper than people.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top