MainframeMaster

Floating-Point Sorting

Floating-point (FL) in DFSORT is the format you use when your sort key is stored in a floating-point representation: typically 4 bytes (single-precision) or 8 bytes (double-precision) in a format such as IEEE binary floating-point or IBM hexadecimal floating-point. COBOL COMP-1 and COMP-2 are examples. When you specify FL in SORT FIELDS=, DFSORT interprets the key as a floating-point number and compares numeric value. Support for FL and the exact length and format (e.g. IEEE vs IBM) are product-dependent—consult your DFSORT manual. Special values such as NaN (Not a Number), negative zero, and infinity may be ordered in a product-specific way. Using CH, PD, or ZD on float data produces wrong order because the byte layout is not decimal. This page explains when to use FL, length and format considerations, and caveats.

SORT Statement Deep Dive
Progress0 of 0 lessons

What Is Floating-Point (FL)?

Floating-point numbers are stored in a format that represents a value as sign, exponent, and mantissa—not as a simple integer or decimal string. On the mainframe, common formats include IBM hexadecimal floating-point (used by COBOL COMP-1, COMP-2) and IEEE binary floating-point. The length is usually 4 bytes (single) or 8 bytes (double). DFSORT FL tells the sort to interpret the key as a float and compare numeric value. The exact details (length, byte order, handling of NaN/infinity) depend on your DFSORT product and release; see the Application Programming Guide.

When to Use FL

Use FL only when the sort key is actually stored in a floating-point format that your product supports. That may include COBOL COMP-1 (often 4 bytes) or COMP-2 (often 8 bytes), or application data in IEEE/IBM float. Do not use FL for packed decimal (use PD), zoned decimal (use ZD), or character (use CH). Using FL on non-float data—or CH/PD/ZD on float data—produces wrong sort order.

Length and Format

Specify the key length in bytes: typically 4 for single-precision or 8 for double-precision. The product documentation will state which floating-point formats are supported (e.g. "FL for IEEE single") and the exact length. If your data is IBM hex float (COMP-1/COMP-2), ensure the product's FL option matches that format.

Special Values: NaN, Zero, Infinity

Floating-point has special bit patterns: NaN (Not a Number), +0 and -0, +infinity and -infinity. Different products may sort these in different positions (e.g. NaN last, or -inf first and +inf last in ascending order). If your data can contain these values, check your DFSORT documentation for FL comparison rules so you know where they will appear in the output.

FL vs Other Formats

Using CH on a float field compares raw bytes—the bit layout of floats does not sort in numeric order when compared byte-by-byte. Using PD or ZD treats the bytes as decimal digits, which is wrong. So for keys stored in floating-point form, use FL (when supported) to get correct numeric order.

Example

text
1
SORT FIELDS=(20,8,FL,A)

Sort by an 8-byte floating-point key at position 20, ascending. Confirm with your product that 8-byte FL is supported and matches your data format.

Explain It Like I'm Five

Floating-point is how computers store numbers with decimal points (like 3.14) in a special "science" form (sign, exponent, mantissa). The sort program needs to know "this is that kind of number" so it can put 2.5 before 3.0 before 10.0. If it read the bytes as regular digits or letters, the order would be wrong. So we say "FL" when the key is really stored that way.

Exercises

  1. Your key is COBOL COMP-2 (8-byte float). What format and length might you use? Where do you confirm?
  2. Why is CH wrong for a float key?
  3. What special float values might affect sort order? Where do you find how your product handles them?

Quiz

Test Your Knowledge

1. When should you use FL (floating-point) in SORT FIELDS?

  • For any decimal number
  • Only when the key is actually stored in a floating-point format (e.g. IEEE or IBM hex float)
  • For COMP-3
  • For dates

2. What length do you typically specify for a double-precision float in DFSORT?

  • 2 bytes
  • 4 bytes
  • 8 bytes
  • 16 bytes

3. Why can floating-point sort order be tricky?

  • FL is always wrong
  • Special values like NaN, negative zero, or infinity may sort in product-specific ways; byte comparison is not the same as numeric comparison for floats
  • Only descending is tricky
  • FL is only for integers

4. For a COBOL COMP-1 or COMP-2 field, which format might you use?

  • PD
  • ZD
  • CH
  • FL (if the product supports it for that storage format)

5. What is the main risk of using CH for a float field?

  • CH is faster
  • Float bytes do not sort in numeric order when compared as character; the order will be wrong
  • CH is only for integers
  • There is no risk