UNIVERSITY OF WISCONSIN
Computer Sciences Department
CS 542
Spring 2021
Barton Miller
Elisa Heymann
CS 639 - Exercise 3.2: Numeric Errors
Due: February 9 at 11am.

Solve the exercise #2 from the Numeric Errors chapter:

Write functions that do 32-bit signed integer arithmetic returning a correct 32-bit signed integer result or throw an exception when overflow occurs. For each function, do no use compiler overflow checking, and do not up-cast to a larger capacity type. Also, for each function, try to specify what are the tricky corner cases that might cause the function to misbehave.
  1. (Easy) Write a function, add (x,y) that returns the 32-bit sum of two 32-bit signed integers or in the case of overflow raises an exception.
  2. (Slightly harder) Copy and adapt the add function to do subtract (x,y), the difference of two integers, detecting overflow as above.
  3. (Harder) Write multiply(x,y), the product of the two integers, again, detecting overflow. Why is this harder than doing the sum or difference?
  4. (Harder?) Similarly write divide (x,y), which divides two integers, x by y. Division by zero is undefined, but this is a separate exception from overflow. Is overflow detection necessary? Why or why not?

You can do this assignment in either C or Java.

If you use C:
On the CS Department Linux systems, using the default gcc compiler, the int type will be 32 bits (4 bytes). If you use a different system or compiler, you will need to check the size of your variables to ensure that they are 32 bits. You can write a little test program and use the sizeof function to determine how many bytes are allocated for a given type or variable.
If you use Java:
You should use int, as this is always a 32 bit (4 byte) value in Java.

Hand in Instructions

  1. Please compile your solutions into a single pdf document and upload it to Canvas (Assignments -> Exercise on Numeric Errors)
  2. The document should begin with both your and your partner's names.
  3. This should be followed by the answers in order of questions as stated above.
  4. Please clearly identify the answers with the question number/letter as stated in the writeup.
  5. The answers should include your explanations and code with comments.

Requirements for comments:

  1. Please use the comment style specified in Google style guide for your language of choice.
  2. The code should also have your names as a comment header at the top.
  3. You should add comments as necessary to explain your solutions, as you would if handing over the code to an employer/client.


Last modified: Mon Feb 1 10:01:11 CST 2021