fcmp<l,g>

Single precision float comparison

Bytecode

Type Description
u1 fcmpl opcode = 0x95 (149)
u1 fcmpg opcode = 0x96 (150)

Stack ..., value1, value2 => ..., result

Description

Both value1 and value2 must be of type float. The values are popped from the operand stack, and a floating-point comparison is performed.

If value1 is greater than value2, the int value 1 is pushed onto the operand stack.

If value1 is equal to value2, the int value 0 is pushed onto the operand stack.

If value1 is less than value2, the int value -1 is pushed onto the operand stack.

If either value1 or value2 is NaN, the fcmpg instruction pushes the int value 1 onto the operand stack and the fcmpl instruction pushes the int value -1 onto the operand stack.

Floating-point comparison is performed in accordance with IEEE 754. All values other than NaN are ordered, with negative infinity less than all finite values and positive infinity greater than all finite values. Positive zero and negative zero are considered equal.

Example

fload_1       ; push the float in local variable 1
fconst_0      ; push the float 0 onto the stack
fcmpl         ; compare the two numbers

; The integer result on the stack is:
;     0 if local variable 1 equals 0
;     -1 if local variable 1 is less than 0
;     1 if local variable 1 is greater than 0

Notes

The fcmpg and fcmpl instructions differ only in their treatment of a comparison involving NaN. NaN is unordered, so any float comparison fails if either or both of its operands are NaN. With both fcmpg and fcmpl available, any float comparison may be compiled to push the same result onto the operand stack whether the comparison fails on non-NaN values or fails because it encountered a NaN.