Sign in

docs RPG Reference

%HIVAL and %LOVAL (Highest Value or Lowest Value)

%HIVAL and %LOVAL (Highest Value or Lowest Value)

%HIVAL and %LOVAL (Highest Value or Lowest Value)

%HIVAL(variable)
%LOVAL(variable)

%HIVAL returns the highest value for the variable.

%LOVAL returns the lowest value for the variable.

The operand must be a variable name.

If it is an array name, the built-in function returns the highest or lowest value of one element of the array.

If the operand is an enumeration name, the built-in function returns the highest or lowest value in the enumeration.

If the operand is a variable that is defined like a typed enumeration, the built-in function returns the same value for the variable as it returns for the enumeration. For example, in the following code, variables num1 and num2 both have type PACKED(5:2). But since num1 is defined like enumeration en1, which has a high value of 12.3, %HIVAL(num1) has the value 12.3, while %HIVAL(num2) has the value 999.99.

DCL-ENUM e1 PACKED(5:2); // %HIVAL(e1) = 12.3, %LOVAL(e1) = -6.1
  v1 12.3;
  v2 -6.1;
  v3 2.35;
END-ENUM;
DCL-S num1 LIKE(e1);     // %HIVAL(num1) = 12.3, %LOVAL(num1) = -6.1
DCL-S num2 PACKED(5:2);  // %HIVAL(num2) = 999.99, %LOVAL(num2) = -999.99

If the operand is not valid for %HIVAL or %LOVAL, the error message from the compiler has the following reason codes:

  • ERROR: The operand has an error.
  • NOT_FLD_OR_ENUM: The operand is not the name of a variable or numeration.
  • DS: A data structure is not allowed.
  • POINTER: A variable with data type Pointer is not allowed.
  • POINTER(*PROC): A variable with data type Procedure Pointer is not allowed.
  • OBJECT: A variable with data type Object is not allowed.
  • STRING_LEN: A variable with data type Character, UCS-2, or Graphic cannot have a length greater than 1000.
  • ENUM_HEX_AND_NON_HEX: An enumeration has a mixture of hexadecimal literals and numeric or character literals.
  • ENUM_FLOAT_AND_NON_FLOAT: An enumeration has a mixture of float literals and non-float literals.
  • ENUM_HEX_DIFF_LEN: An enumeration has hexadecimal literals with different lengths.
  • ENUM_JOB_CCSID: An enumeration of type character has CCSID(*JOBRUN) or CCSID(*JOBRUNMIX). The high or low value might be different depending on the job CCSID at runtime.

Examples of %HIVAL and %LOVAL

  1. This example uses %HIVAL with a numeric variable to check in advance whether an assignment would cause an overflow exception

    DCL-S payment PACKED(7:2);
    DCL-S salary PACKED(7:2);
    DCL-S bonus PACKED(6:2);
    
    IF salary + bonus > %HIVAL(payment);
       SND-MSG *ESCAPE ('The "pay" variable is too small for "salary + bonus": '
                      + %CHAR(salary + bonus));
    ELSE;
       payment = salary + bonus;
    ENDIF;
  2. This example uses %LOVAL and %HIVAL with an enumeration

    DCL-ENUM meeting_time_range QUALIFIED;
       earliest_time T'09.00.00';
       latest_time   T'17.00.00';
    END-ENUM;
    DCL-S start_time TIME;
    DCL-S length INT(10);
    
    IF start_time < %LOVAL(meeting_time_range);
       error = START_TOO_EARLY;
    ELSEIF start_time + %MINUTES(length) > %HIVAL(meeting_time_range);
       error = END_TOO_LATE;
    ENDIF;