Thursday, September 13, 2012

Internal Representation of Fixed-Point Decimal Data

Fixed-point decimal data is stored in packed decimal format. Each digit is stored in a half-byte, as shown in Figure. Bits 0 through 3 of the last half-byte contain a value indicating the sign. Normally, the hexadecimal value C indicates a positive value and the hexadecimal value D indicates a negative value.   Figure: Fixed-Point Decimal Data Representation

Use in Expressions

You cannot use fixed-point decimal data with a nonzero scale factor in calculations with binary integer variables. If you must use the two types of data together, use the DECIMAL built-in function to convert the binary value to a scaled decimal value before attempting an arithmetic operation. For example:

DECLARE I FIXED BINARY, 
       SUM FIXED DECIMAL (10,2); 
 
SUM = SUM + DECIMAL (I); 

Fixed-Point Decimal Variables

The attributes FIXED and DECIMAL are used to declare fixed-point decimal variables. The FIXED attribute is implied by DECIMAL. If you do not specify the precision and the scale factor, the default values are 10 and 0, respectively. The format of a declaration of a single fixed-point decimal variable is:

DECLARE identifier [FIXED] DECIMAL [(p[,q])]; 
Some examples of fixed-point decimal declarations are:

DECLARE PERCENTAGE FIXED DECIMAL (5,2); 
DECLARE TONNAGE FIXED DECIMAL (9); 

Fixed-Point Decimal Constants

A fixed-point decimal constant can have between 1 and 31 of the decimal digits 0 through 9 with an optional decimal point or sign, or both. If there is no decimal point, PL/I assumes it to be immediately to the right of the rightmost digit. Some examples of fixed-point decimal constants are:

12 
4.56 
12345.54 
-2 
01. 
The precision of a fixed-point decimal value is the total number of digits in the value. The scale factor is the number of digits to the right of the decimal point, if any. The scale factor cannot be greater than the precision.

Fixed-Point Decimal Data

Fixed-point decimal data is used in calculations where exact decimal values must be maintained, for example, in financial applications. You can also use fixed-point decimal data with a scale factor of 0 whenever integer data is required. The following sections describe fixed-point constants and variables and their use in expressions. This discussion is divided into the following parts:
  • Constants
  • Variables
  • Use in expressions
  • Internal representation

Internal Representation of Fixed-Point Binary Data

The following figure shows the internal representation of fixed-point binary data. Storage for fixed-point binary variables is always allocated in a byte, word, or longword. For any fixed-point binary value:
  • If p is in the range 1 through 7, a byte is allocated.
  • If p is in the range 8 through 15, a word is allocated.
  • If p is in the range 16 through 31, a longword is allocated.
The binary digits of the stored value go from right to left in order of increasing significance; for example, bit 6 of a FIXED BINARY (7) value is the most significant bit, and bit 0 is the least signficant. In all cases, the high-order bit (7, 15, or 31) represents the sign.   Figure: Internal Representation of Fixed-Point Binary Data

Fixed-Point Binary Data

The attributes FIXED and BINARY are used to declare integer variables and fractional variables in which the number of fractional digits is fixed (that is, nonfloating-point numbers). The BINARY attribute is implied by FIXED. For example, a fixed-point binary variable can be declared as:

DECLARE X FIXED BINARY(31,0); 
The variable X is given the attributes FIXED, BINARY, and (31,0) in this declaration. The precision is 31. The scale factor is 0, so the number is an integer. There is no representation in PL/I for a fixed-point binary constant. Instead, integer constants are represented as fixed decimal. However, fixed decimal integer constants (and variables) are converted to fixed binary when combined with fixed binary variables in expressions. For example:

I = I+3; 
In this example, if I is a fixed binary variable, the integer 3 is represented as fixed decimal; however, PL/I converts it to fixed binary when evaluating the expression. Fixed binary variables have a maximum precision of 31, and therefore fixed binary integers can have values only in the range -2,147,483,648 through 2,147,483,647. An attempt to calculate a binary integer outside this range, in a context that requires an integer value, signals the FIXEDOVERFLOW condition. The attributes FIXED BINARY are used to declare binary data in PL/I. The BINARY attribute is implied by FIXED. The format of a declaration of a single, fixed-point, binary variable is:

DECLARE identifier FIXED [BINARY] [(precision[,scale-factor])]; 
There is no form for a fixed-point binary constant, although constants of other computational types are convertible to fixed-point binary. A fixed-point binary variable usually receives given values by being assigned to an expression of another computational type or another fixed-point binary variable.

Precision and Scale of Arithmetic Data Types

The PRECISION attribute applies to decimal and binary data as follows:
  • The precision of a fixed-point data item is the total number of decimal or binary digits used to represent a value.
  • The precision of a floating-point data item is the number of decimal or binary digits in the mantissa of the floating-point representation.
The scale of fixed-point data is the number of digits to the right of the decimal or binary point. Floating-point variables do not have a scale factor. In this manual, the letter p is used to indicate precision, and the letter q is used to indicate the scale factor. You can specify both precision and scale in a declaration. For example:

DECLARE x FIXED DECIMAL(10,3) INITIAL(1.234); 
This example indicates that the value of x has 10 decimal digits and that 3 of those are fractional. When a value is assigned to the variable, its internal representation does not include the decimal point; the previous value for x is stored as 1234, and the decimal point is inserted when the number is output. The scale factor has the effect of multiplying the internal representation of the decimal number by a factor of 10-q (where q is the absolute value of the specified scale). The ranges of values you can specify for the precision for each arithmetic data type, and the defaults applied if you do not specify a precision, are summarized as follows:
Data Type
Attributes

Precision
Scale
Factor
Default Precision
BINARY FIXED 1 <= p <= 31 p >= q >= -31 31
BINARY FLOAT (OpenVMS VAX systems) 1 <= p <= 113 - 24
BINARY FLOAT (OpenVMS Alpha systems) 1 <= p <= 53 - 24
DECIMAL FIXED 1 <= p <= 31 p >= q >= 0 10
DECIMAL FLOAT (OpenVMS VAX systems) 1 <= p <= 34 - 7
DECIMAL FLOAT (OpenVMS Alpha systems) 1 <= p <= 15 - 7
If no scale factor is specified for fixed-point data, the default is 0. For fixed-point binary data, the scale factor must be within the range -31 through 31 and less than or equal to the specified precision. Positive scale factors for fixed binary numbers function according to the same principles as those for fixed decimal. That is, a positive scale factor is similar to multiplying the internal representation binary number by a factor of 2-q. A negative scale factor indicates that the number of fractional bits are shifted in the opposite direction. In effect, this is similar to multiplying the binary number by a factor of 2q, where q is the absolute value of the specified scale. For example:

DECLARE (A,B) FIXED BINARY(31,-3), 
        (C,D) FIXED BINARY(31,3); 
A = 128;      /* output = 128   */ 
B = 7;        /* output =   0   */ 
C = 128;      /* output = 128.0 */ 
D = 7;        /* output =   7.0 */ 
 
PUT SKIP LIST (A,B,C,D); 
END; 
Internally, binary numbers undergo an implicit conversion and are represented as powers of 2. For instance, in the previous example variable A is first divided by 23 because it is declared with a scale factor of -3. The stored number is 16. On output, the number 16 is multiplied by 23 and the number is again 128. However, when variable B is first divided by 23, the result is 0, which is the value of the stored number. Therefore, on output, 0 is multiplied by 23 and the output is 0. Integer variables declared in the previous example with a positive scale factor are output as they were input, but they are followed on the right with a decimal point and a 0. Even though arithmetic operands can be of different arithmetic types, all operations must be performed on objects of the same type. Consequently, the compiler can convert operands to a derived type, as previously shown. Therefore, when you declare a fixed binary number with a scale factor and assign it a decimal value, the results may not be as you expect because the binary scale factor left-shifts the specified number of bits to the right of the decimal point. During conversion to a decimal representation, the difference between the resulting binary number and its decimal representation is not the equivalent of dividing or multiplying the decimal number by 10. Instead, the binary number is first converted to its internal representation and then this representation is converted to its decimal representation. When excess fractional digits are truncated, no condition is signaled. If there is any resulting loss of precision, it may be difficult to detect because truncated fractional digits do not signal a condition. For example:

A: PROCEDURE OPTIONS (MAIN); 
   DECLARE A FIXED BIN (31,3), 
           B DECIMAL (10,5), 
           C DECIMAL (10,5); 
A = .3; 
B = 34.8; 
 
C = MULTIPLY(A,B,10,5); 
 
PUT SKIP LIST (A,B,C); 
END; 
Before the multiplication is performed, the variables are converted to fixed binary so that the operands share a common data type. However, after conversion, variable A is output as 0.2 rather than 0.3. The output from the previous example is:

0.2     34.80000    8.6875 
If variable A was declared with the attributes FIXED DECIMAL (10,5), the output will be:

0.30000     34.80000    10.44000 

DATE/TIME BUILT IN FUCNCTIONS


DATEReturns the current date in the pattern YYMMDD
DATETIMEReturns the current date and time in the user-specified pattern or in the default pattern YYYYMMDDHHMISS999
DAYSReturns the number of days corresponding to a date/time pattern string, or the number of days for today's date
DAYSTODATEConverts a number of days to a date/time pattern string
DAYSTOSECSConverts a number of days to a number of seconds
REPATTERNTakes a value holding a date in one pattern and returns that value converted to a date in a second pattern
SECSReturns the number of seconds corresponding to a date/time pattern string, or the number of seconds for today's date
SECSTODATEConverts a number of seconds to a date/time pattern string
SECSTODAYSConverts a number of seconds to a number of days
TIMEReturns the current time in the pattern HHMISS999
VALIDDATEIndicates if a string holds a valid date
WEEKDAYReturns the day of the week corresponding to the current day or specified DAYS value
Y4DATETakes a date value with the pattern 'YYMMDD' and returns the date value with the two-digit year widened to a four-digit year
Y4JULIANTakes a date value with the pattern 'YYDDD' and returns the date value with the two-digit year widened to a four-digit year
Y4YEARTakes a date value with the pattern 'YY' and returns the date value with the two-digit year widened to a four-digit year

TRANSLATE


TRANSLATE : SUBSTITUTES ONE CHARACTER/BIT WITH ANOTHER

SYNTAX:  TRANSLATE(X,Y,Z)

X = SOURCE STRING
Y = REPLACEMENT STRING
Z = POSITION STRING

EX: TRANSLATE(X, Y, Z);

X= '1GOLDEN'
Y='ABCSILVER'
Z='WXYGOLDEN'

NOW FOR EACH CHARACTER OF X, A SEARCH OF Z IS MADE AND IF MATCH IS FOUND THE CORRESPONDING POSITIONAL CHARACTER IN Y IS RETURNED.

SO AFTER EXECUTING TRANSLATE(X,Y,Z), THE X BECOMES '1SILVER'

REPEAT


 
IT TAKES A GIVEN STRING VALUE AND FORMS A NEW STRING CONSISTING OF STRING VALUE CONCATENATED WITH ITSELF A SPECIFIED NUMBER OF TIMES.

SYNTAX:      REPEAT ( M, N )

M = STRING THAT HAS TO BE REPEATED
N = DECIMAL INTEGER CONSTANT/VARIABLE  THAT MUST  BE GREATER THAN '0'

EX:  REPEAT('HYDERABAD ', 2) RESULTS IN  'HYDERABAD HYDERABAD'

VERIFY


VERIFY returns a FIXED value indicating the position in x of the leftmost character that is not in y. It also allows you to specify the location within x at which to begin processing.

VERIFY(x,y,n)


x
Expression.x should have CHARACTER type, and if not, it is converted thereto.
y
Expression.y should have CHARACTER type, and if not, it is converted thereto.
n
Expression n specifies the location within x where processing begins.n should have FIXED type, and if not, it is converted thereto.
If all the characters in x do appear in y, a value of zero is returned. If x is the null string, a value of zero is returned. If x is not the null string andy is the null string, the value of n is returned. The default value for n is one.
n must be greater than 0 and no greater than 1 + LENGTH(x).
If n = LENGTH(x) + 1, the result is zero.

INDEX

INDEX returns the starting position of a substring within a string.
INDEX(STRING,SUBSTRING,BACK) 

EX:
1. INDEX('FORTRAN', 'R') has the value 3  
2. INDEX('FORTRAN', 'R',BACK=TRUE) has the value 5  
BACK is an optional parameter/argument.

Case (i): If BACK is absent or present with the value .FALSE., the result is the minimum positive value of I such that STRING (I : I + LEN (SUBSTRING) - 1) = SUBSTRING or zero if there is no such value. Zero is returned if LEN (STRING) < LEN (SUBSTRING). One is returned if LEN (SUBSTRING) = 0.

Case (ii): If BACK is present with the value .TRUE., the result is the maximum value of I less than or equal to LEN (STRING) - LEN (SUBSTRING) + 1, such that STRING (I : I + LEN (SUBSTRING) - 1) =  SUBSTRING or zero if there is no such value. Zero is returned if LEN (STRING) < LEN (SUBSTRING) and LEN (STRING) + 1 is returned if LEN (SUBSTRING) = 0.

Arithmetic data types

Arithmetic data types are used for variables on which arithmetic calculations are to be performed. The arithmetic data types supported by PL/I are as follows:
  • Fixed point-for binary and decimal data with a fixed number of fractional digits
  • Floating point-for calculations on very large or very small numbers with the decimal point (number of fractional digits) allowed to float
  • Pictured-for fixed point decimal data that is stored internally in character form with special formatting characters

Monday, September 10, 2012

PL1 ERRORS / MESSAGES COMPILATION TIME

There are five types of messages including errors/warnings etc , that we get while compiling a pl1 program.
They are I - informational, W - warning, S - severe, E - error and U - unrecoverable .

Compiler messages are printed in groups as per these severity levels.

Let's see all the types of messages in detail,

INFORMATIONAL:
Severity Code  :     I
Return code     :     0000
Description      :  The compiled program should run correctly, It probably shows the inefficiency in the code that might be rectified . Generally developers are least bothered about this.



WARNING:

Severity Code  :     W
Return code     :     0004
Description      :  The compiled program should run correctly but it might produce weird results. Generally its   an error in the statement which is syntactically correct. Preferably this should be rectified in order to make the program efficient



ERROR:

Severity Code  :     E
Return code     :     0008
Description      :      Its a simple error that can be fixed by compiler. The compiled program should run correctly but it might produce unexpected results.



SEVERE:

Severity Code  :     S
Return code     :     0012
Description      :      An error that can not be fixed by compiler. If the program is compiled an object module is created which cant be used.



UNRECOVERABLE:

Severity Code  :     U
Return code     :     0016
Description      :      An error that forces termination of the compilation. An object module is not created successfully.

Sunday, September 9, 2012

SUBSTR



The SUBSTR built-in function is of particular importance since it is a basic PL/1 string operator. It is a three argument function which allows a reference to be made to a portion of a string variable, i.e., SUBSTR (a, b,c) is a reference to the bth through b + c -1th character (or bit) in the string a.

SUBSTR returns a substring, specified by b and c, of a.

SUBSTR(a,b,c)
         
EX: a="MAINFRAMES"; b=5; c=5; SUBSTR(a,b,c)value will be "FRAME";

a
String expression. It specifies the string from which the substring is extracted. If a is not a string, it is converted to character.
b
Expression that is converted to FIXED BINARY(31,0). b specifies the starting position of the substring in a.
c
Expression that is converted to FIXED BINARY(31,0). c specifies the length of the substring in a. If c is zero, a null string is returned. If c is omitted, the substring returned is position b in a to the end of a.
The STRINGRANGE condition is raised if c is negative or if the values of b and c are such that the substring does not lie entirely within the current length of a. It is not raised when b = LENGTH(a)+1 and c = 0.