Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Floating-point arithmetic

A look at the floating-point support of the Java virtual machine

  • Print
  • Feedback

Page 4 of 5

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


Circle of squares: A JVM simulation

The applet below demonstrates a Java virtual machine executing a sequence of bytecodes that perform floating-point arithmetic. The bytecode sequence in the simulation was generated by javac for the squareItForever() method of the class shown below:

    class Struggle { 
    static void squareItForever() { 
        float f = 2; 
        while (true) { 
            f = f * f; 
            f = 0 - f; 
        } 
    } 
}


The actual bytecodes generated by javac for squareItForever() are shown below:

fconst_2          // Push float constant 2. 
fstore_0          // Pop to local variable 0 (float f): float f = 2; 
fload_0           // Push local variable 0 (float f). 
fload_0           // Push local variable 0 (float f). 
fmul              // Pop top two floats, multiply, push float result. 
fstore_0          // Pop to local variable 0 (float f): f = f * f; 
fconst_0          // Push float constant 0. 
fload_0           // Push local variable 0 (float f). 
fsub              // Subtract top float from next to top float: imByte = (byte) imInt; 
fstore_0          // Pop result to local variable 0 (float f): f = 0 - f; 
goto 2            // Jump back to the first fload_0 instruction: while (true) {} 


The squareItForever() method repeatedly squares a float value until it hits infinity. Each time the float is squared it is also negated. The float starts out as 2. It only takes seven iterations before infinity is reached, which isn't nearly as long as it takes in real life. The hex representation of the bits that make up the float are shown in the "hex value" column in the applet. The "value" column shows the number as humans are used to seeing it. This human-friendly value is generated by the Float.toString() method.

  • Print
  • Feedback

Resources
  • Previous Under The Hood articles:
  • The lean, mean virtual machine -- Gives an introduction to the Java virtual machine. Look here to see how the garbage collected heap fits in with the other parts of the JVM.
  • The Java class file lifestyle -- Gives an overview of the Java class file, the file format into which all Java programs are compiled.
  • Java's garbage-collected heap -- Gives an overview of garbage collection in general and the garbage-collected heap of the Java virtual machine in particular.
  • Bytecode basics -- Introduces the bytecodes of the JVM, and discusses primitive types, conversion operations, and stack operations.