Lab 8: Floating Point

Due: Sunday, May 15 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.

Preliminaries

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, Xfloat.java 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.

Hints

Tests

Included in the repository is a Python test script, test.py. This script will compile and test your code against 10 fixed inputs or, if you pass the -r option to it, against an additional 100 randomly chosen inputs.

$ ./test.py
Compiling
x=0.0 y=0.0: PASS
x=-1.0 y=0.0: PASS
x=0.0 y=-1.0: PASS
x=0.9 y=0.1: PASS
x=1.5 y=1.5: PASS
x=6.66666 y=3.3333333: PASS
x=2.25 y=0.5: PASS
x=-4.0 y=2.0: PASS
x=-20486.0 y=-2.5e-06: PASS
x=4.70197740328915e-38 y=2.1267647932558654e+37: PASS
10/10 tests passed

Or

$ ./test.py -r
Compiling
x=0.0 y=0.0: PASS
x=-1.0 y=0.0: PASS
x=0.0 y=-1.0: PASS
x=0.9 y=0.1: PASS
x=1.5 y=1.5: PASS
x=6.66666 y=3.3333333: PASS
x=2.25 y=0.5: PASS
x=-4.0 y=2.0: PASS
x=-20486.0 y=-2.5e-06: PASS
x=4.70197740328915e-38 y=2.1267647932558654e+37: PASS
10/10 tests passed

Performing 100 random tests
x=-1.5824437428766114e+19 y=7.654288931433146e-16: PASS
x=-519727136.0 y=-4.693174584159976e+27: PASS
x=5.338058977860687e-34 y=-110286184.0: PASS
...
x=-1.7480781718172477e-25 y=3.453274344019857e+34: PASS
100/100 random tests passed

Submission

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