CSCI 210: Lab 7

Floating Point
Due: 11:59pm on Sunday April 18th

For this problem you will implement the IEEE Floating point standard either as a Java class. Some of the code is provided in your github repo - you are required to write methods to perform addition and multiplication on 32-bit IEEE-format floating point numbers. You can create your repository by clicking this link.

Partners

You can work on this lab with a partner if you choose. If you decide to work with a partner, you and your partner should check out a single lab7 repository. The first partner will create a team name, and the second partner should choose that team name. Please be careful choosing a team, as this cannot be undone. Please name your team something that makes it clear who you are.

If you choose to work with a partner, you and your partner must complete the entire lab together. Dividing the lab up into pieces and having each partner complete a part of it on their own will be considered a violation of the honor code. Both you and your partner are expected to fully understand all of the code you submit.

The Code

The file Xfloat.java contains an abstract base class Xfloat with three fields:

  • byte sign;
  • int exp;
  • int man;

These fields correspond to the sign, exponent and mantissa fields of a floating point number. The abstract methods are xadd and xmult. The class also contains methods for converting between actual floating point and Xfloat.

MyXfloat.java contains the definition of a subclass of Xfloat. It contains a main method that reads 2 floating point numbers from the command line (called x and y) and prints out their Xfloat fields. For example,


> java MyXfloat 2.5 7.25
		x:   40200000 (0 10000000 01000000000000000000000) s: 0 e: 80 m: 200000 2.5
		y:   40e80000 (0 10000001 11010000000000000000000) s: 0 e: 81 m: 680000 7.25
		x*y: 0 (0 00000000 00000000000000000000000) s: 0 e: 0 m: 0 0.0
		x+y: 0 (0 00000000 00000000000000000000000) s: 0 e: 0 m: 0 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 mantissa fields. Next are the corresponding Xfloat fields in hex. Note that the mantissa field requires only 6 hex digits since only 23 of the 32 bits are actually used.

MyXfloat.java includes only dummy implementations for xadd and xmult, which return objects of type MyXfloat with the value 0. Your assignment is to write proper implementations for these methods. Note that the program is set up to print the product and sum, currently using dummy versions of xmult and xadd.

I've also provided a correct version of the program (XfloatAns.class). You can run it with the command "java XfloatAns ...". (You must have already compiled Xfloat.java and have Xfloat.class visible on your classpath in order for XfloatAns to run).

Notes:

In the Java Virtual Machine, floating point values are always converted to double precision before any computation takes place. Mantissa multiplication should proceed as follows:

  1. Use Xfloat.BMASK to return the implicit 1 in the mantissa
  2. Convert the mantissas to 64 bit long integers by type casting;
  3. Multiply the 64 bit long integers;
  4. Shift the result appropriately (making necessary adjustments to the exponent).
  5. Cast the mantissa back to 32 bits and remove the implicit 1.

The various static MASK constants defined in Xfloat should be helpful in this process. Note that in Java, the unsigned right shift is represented by the operator >>>.

You can ignore denormalized values, but be sure to properly handle the special case of 0.


C. Taylor