Lab 8: Floating Point

Due: Sunday, January 2 at 23:59

In this lab, you will implement part of the IEEE 754 Standard for Floating-Point Arithmetic. In particular, you will implement single precision (32-bit) floating point addition and multiplication.


You may discuss your implementation with a partner; however, each of you needs to write and submit your own code. So no copying and pasting, but you should feel free to work together on a solution and to debug.

Click on the assignment link.

Once you have accepted the assignment, you can clone the repository on your computer by following the instruction and begin working.

Be sure to ask any questions on Piazza.

Program Specification

In the assignment repo, you’ll find a single Java file, which defines an Xfloat class. This class represents a single-precision floating point number with the following members.

private byte sign;
private byte exponent;
private int significand;

Of course, these represent the three fields of a floating point number.

The file also contains a main method that takes two floats as command-line arguments and prints them out along with the product and sum of the numbers. For example, running the program with floats 2.5 and 7.25 gives this output.

$ java Xfloat 2.5 7.25
x:   40200000 (0 10000000 01000000000000000000000) sign: 0 exp: 80 sig: 200000 2.5
y:   40E80000 (0 10000001 11010000000000000000000) sign: 0 exp: 81 sig: 680000 7.25
x*y: 00000000 (0 00000000 00000000000000000000000) sign: 0 exp: 00 sig: 000000 0.0
x+y: 00000000 (0 00000000 00000000000000000000000) sign: 0 exp: 00 sig: 000000 0.0

The first 8 hex-digit number corresponds to the bits of the actual IEEE floating point number. The binary representation follows, separated into sign, exponent and significand fields. Next are the corresponding Xfloat fields in hex. Note that the significand field requires only 6 hex digits since only 23 of the 32 bits are actually used.

You only need to implement two methods.

public static Xfloat xadd(Xfloat x, Xfloat y)
Add Xfloats x and y together and return the result.
public static Xfloat xmult(Xfloat x, Xfloat y)
Multiply Xfloats x and y together and return the result.

You must implement the methods by performing the addition/multiplication by operating on the three fields of each Xfloat.

You should not modify any other methods.

You may ignore subnormal numbers, infinities, and NaN, but you must properly handle 0.0.

You must use bit masking and shifting to manipulate bits. In particular, you are not allowed to convert your integers to strings.



Submit the lab by committing your code and pushing it to your GitHub repository.