The SIGNED-INT clause is used to define signed integer data types with specific size constraints in COBOL. It provides precise control over the storage size and value range of signed integer data, enabling optimized memory usage and ensuring data integrity within defined boundaries.
The SIGNED-INT clause is used in the DATA DIVISION for signed integer data definition with size constraints.
123456789101112131415161718192021222324252627* Basic SIGNED-INT syntax 01 data-name PIC S9(n) SIGNED-INT [size-specification]. * Complete example with different signed integer sizes IDENTIFICATION DIVISION. PROGRAM-ID. SIGNED-INT-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. * 16-bit signed integer (range: -32,768 to +32,767) 01 SMALL-SIGNED-INT PIC S9(5) SIGNED-INT 16. * 32-bit signed integer (range: -2,147,483,648 to +2,147,483,647) 01 MEDIUM-SIGNED-INT PIC S9(10) SIGNED-INT 32. * 64-bit signed integer (range: -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807) 01 LARGE-SIGNED-INT PIC S9(19) SIGNED-INT 64. PROCEDURE DIVISION. MAIN-LOGIC. * Assign values within the specified ranges MOVE 32000 TO SMALL-SIGNED-INT MOVE -2000000000 TO MEDIUM-SIGNED-INT MOVE 5000000000000000000 TO LARGE-SIGNED-INT DISPLAY "Small signed int: " SMALL-SIGNED-INT DISPLAY "Medium signed int: " MEDIUM-SIGNED-INT DISPLAY "Large signed int: " LARGE-SIGNED-INT STOP RUN.
SIGNED-INT provides precise control over signed integer storage size and value ranges.
Examples of using the SIGNED-INT clause in different scenarios with detailed explanations.
12345678910111213141516171819202122232425262728293031323334* Database application using SIGNED-INT for optimized storage DATA DIVISION. WORKING-STORAGE SECTION. * User ID: 16-bit signed integer (sufficient for most applications) 01 USER-ID PIC S9(5) SIGNED-INT 16. * Record counter: 32-bit signed integer (handles large datasets) 01 RECORD-COUNTER PIC S9(10) SIGNED-INT 32. * Transaction amount: 32-bit signed integer (financial precision) 01 TRANSACTION-AMOUNT PIC S9(10) SIGNED-INT 32. * Large calculation result: 64-bit signed integer (extreme precision) 01 CALCULATION-RESULT PIC S9(19) SIGNED-INT 64. PROCEDURE DIVISION. PROCESS-DATABASE-OPERATIONS. * Initialize user ID (positive value) MOVE 12345 TO USER-ID DISPLAY "User ID: " USER-ID * Set record counter (can be negative for deletions) MOVE 1000000 TO RECORD-COUNTER DISPLAY "Record count: " RECORD-COUNTER * Process transaction (can be negative for withdrawals) MOVE -50000 TO TRANSACTION-AMOUNT DISPLAY "Transaction: " TRANSACTION-AMOUNT * Perform large calculation MOVE 999999999999999999 TO CALCULATION-RESULT DISPLAY "Large result: " CALCULATION-RESULT * Validate ranges to prevent overflow IF USER-ID > 32767 OR USER-ID < -32768 DISPLAY "ERROR: User ID out of range" END-IF.
This example demonstrates how SIGNED-INT optimizes storage for different data types in a database application. User IDs use 16-bit integers (sufficient for most applications), while counters and amounts use 32-bit integers for larger ranges, and calculation results use 64-bit integers for extreme precision.
12345678910111213141516171819202122232425262728293031323334* Embedded system using SIGNED-INT for memory-constrained environment DATA DIVISION. WORKING-STORAGE SECTION. * Temperature sensor: 16-bit signed integer (range: -32768 to +32767) 01 TEMPERATURE-READING PIC S9(5) SIGNED-INT 16. * Pressure sensor: 16-bit signed integer (atmospheric pressure in Pa) 01 PRESSURE-READING PIC S9(5) SIGNED-INT 16. * Altitude calculation: 32-bit signed integer (can be negative for below sea level) 01 ALTITUDE-VALUE PIC S9(10) SIGNED-INT 32. * System timestamp: 32-bit signed integer (Unix timestamp) 01 SYSTEM-TIMESTAMP PIC S9(10) SIGNED-INT 32. PROCEDURE DIVISION. PROCESS-SENSOR-DATA. * Read temperature (can be negative for cold temperatures) MOVE -15 TO TEMPERATURE-READING DISPLAY "Temperature: " TEMPERATURE-READING "°C" * Read pressure (always positive, but using signed for calculations) MOVE 101325 TO PRESSURE-READING DISPLAY "Pressure: " PRESSURE-READING " Pa" * Calculate altitude (can be negative for below sea level) MOVE -50 TO ALTITUDE-VALUE DISPLAY "Altitude: " ALTITUDE-VALUE " meters" * Get system timestamp (large positive number) MOVE 1640995200 TO SYSTEM-TIMESTAMP DISPLAY "Timestamp: " SYSTEM-TIMESTAMP * Check for sensor errors (negative values might indicate errors) IF TEMPERATURE-READING < -30000 DISPLAY "WARNING: Temperature sensor error detected" END-IF.
This example shows how SIGNED-INT optimizes memory usage in embedded systems where resources are limited. Temperature and pressure sensors use 16-bit integers (sufficient for their ranges), while altitude and timestamp use 32-bit integers for larger value ranges. The system can detect sensor errors using negative values outside normal ranges.
1234567891011121314151617181920212223242526272829303132333435363738394041* Financial application using SIGNED-INT for currency conversion DATA DIVISION. WORKING-STORAGE SECTION. * Exchange rate multiplier: 32-bit signed integer (handles large rates) 01 EXCHANGE-RATE PIC S9(10) SIGNED-INT 32. * Base amount: 32-bit signed integer (amount in base currency) 01 BASE-AMOUNT PIC S9(10) SIGNED-INT 32. * Converted amount: 32-bit signed integer (amount in target currency) 01 CONVERTED-AMOUNT PIC S9(10) SIGNED-INT 32. * Fee calculation: 16-bit signed integer (smaller fees) 01 TRANSACTION-FEE PIC S9(5) SIGNED-INT 16. * Final amount: 64-bit signed integer (large financial calculations) 01 FINAL-AMOUNT PIC S9(19) SIGNED-INT 64. PROCEDURE DIVISION. PERFORM-CURRENCY-CONVERSION. * Set exchange rate (large positive number) MOVE 1150000 TO EXCHANGE-RATE * 1.15 as integer (multiplied by 1,000,000) DISPLAY "Exchange rate: " EXCHANGE-RATE * Set base amount (can be negative for refunds) MOVE 1000000 TO BASE-AMOUNT * $1000.00 as integer (multiplied by 1,000) DISPLAY "Base amount: " BASE-AMOUNT * Calculate converted amount MULTIPLY BASE-AMOUNT BY EXCHANGE-RATE GIVING CONVERTED-AMOUNT DIVIDE CONVERTED-AMOUNT BY 1000000 GIVING CONVERTED-AMOUNT DISPLAY "Converted amount: " CONVERTED-AMOUNT * Calculate transaction fee (small negative amount) MOVE -2500 TO TRANSACTION-FEE * -$2.50 as integer (multiplied by 1,000) DISPLAY "Transaction fee: " TRANSACTION-FEE * Calculate final amount ADD CONVERTED-AMOUNT TO TRANSACTION-FEE GIVING FINAL-AMOUNT DISPLAY "Final amount: " FINAL-AMOUNT * Validate final amount is within acceptable range IF FINAL-AMOUNT > 999999999999999999 OR FINAL-AMOUNT < -999999999999999999 DISPLAY "ERROR: Amount exceeds system limits" END-IF.
This example demonstrates SIGNED-INT usage in financial applications where different data types require different precision levels. Exchange rates and amounts use 32-bit integers for standard precision, fees use 16-bit integers for smaller values, and final calculations use 64-bit integers for extreme precision. The system handles negative values for refunds and fees.
Understanding best practices ensures effective use of the SIGNED-INT clause.
1. What is the primary purpose of the SIGNED-INT clause in COBOL?
2. In which COBOL division is the SIGNED-INT clause typically used?
3. What is the difference between SIGNED-INT and SIGNED?
4. Can SIGNED-INT be used with all numeric data types?
5. What is the relationship between SIGNED-INT and data storage?