Floating-Point Arithmetic
Floating-Point Support in the Java Virtual Machine
by Bill Venners
First Published in JavaWorld, October 1996

<<  Page 2 of 3  >>

Floating opcodes
The following table shows the opcodes that pop two floating-point values from the top of the stack, add them, and push the result. The type of the values is indicated by the opcode itself, and the result always has the same type as the numbers being added. No exceptions are thrown by these opcodes. Overflow results in a positive or negative infinity, and underflow results in a positive or negative zero.

Opcode Operand(s) Description
fadd (none) pops two floats, adds them, and pushes the float result
dadd (none) pops two doubles, adds them, and pushes the double result

Subtraction is performed on floats and doubles via the following opcodes. Each opcode causes the top two values of the appropriate type to be popped off the stack. The topmost value is subtracted from the value just beneath the topmost value. The result is pushed back onto the stack. No exceptions are thrown by either of these opcodes.

Floating-point subtraction
Opcode Operand(s) Description
fsub (none) pops two floats, subtracts them, and pushes the float result
dsub (none) pops two doubles, subtracts them, and pushes the double result

Multiplication of floats and doubles is accomplished via the following opcodes. Each opcode causes two values of the same type to be popped off the stack and multiplied. The result, of the same type as the numbers being multiplied, is pushed back onto the stack. No exceptions are thrown.

Floating-point multiplication
Opcode Operand(s) Description
fmul (none) pops two floats, multiplies them, and pushes the float result
dmul (none) pops two doubles, multiplies them, and pushes the double result

The division experience is made available for floats and doubles by the following opcodes. The division opcodes cause the top two values of the appropriate type to be popped off the stack. The topmost value is divided by the value immediately beneath the topmost value. The result is pushed onto the stack. Floating-point division of a finite value by zero yields a positive or negative infinity. Floating-point division of zero by zero yields NaN. No exception is thrown as a result of any floating-point division.

Floating-point division
Opcode Operand(s) Description
fdiv (none) pops two floats, divides them, and pushes the float result
ddiv (none) pops two doubles, divides them, and pushes the double result

The remainder operation is accomplished via the following opcodes on floats and doubles. The following opcodes cause the top two values to be popped from the stack. The topmost value is divided by the value just beneath it, and the remainder of that division is pushed back onto the stack. Floating-point remainder of any value divided by zero yields a NaN result. No exception is thrown as a result of any floating-point division.

Floating-point remainder
Opcode Operand(s) Description
frem (none) pops two floats, divides them, and pushes the float remainder
drem (none) pops two doubles, divides them, and pushes the double remainder

The following opcodes perform arithmetic negation on floats and doubles. Negation opcodes pop the top value from the stack, negates it, and pushes the result.

Floating-point negation
Opcode Operand(s) Description
fneg (none) pops a float, negates it, and pushes the result
dneg (none) pops a double, negates it, and pushes the result

<<  Page 2 of 3  >>