Homework: Floating Point

Learning objective: hands-on experience with floating point special values.
(Hint: some of what you might learn is that things don't work as you'd expect. Don't spend too much time if you hit a figurative brick wall; ask staff for help at office hours if that happens to you. We've found that even very small version changes for compilers and libraries can produce significant changes for how exceptional floating point values are handled.)

Write a program that exercises 32-bit floating point math edge cases in C or C++ as described below.

Ground rules:This software can be as simple as you like. For example, you can hard-code test input values in const declarations. However, we want you to create a SINGLE function that is used for ALL the test cases and the function needs to do some computation that creates special values, not just return special values. So, for example, to generate infinity you might divide two inputs and in the test set one of them to zero.

Important hint: some systems have trouble generating, propagating, and printing some cases, especially signed NaN (some only do +NaN while others do -NaN) and negative zero. If you get stuck on this ask a TA to make sure it is the library and not you. If your system is "broken" in that way hand in and explain the issue. (And take this as a practical lesson on why floating point is so tricky to use for life critical systems.) We do not intend for you to spend multiple hours chasing this down for the homework.

As an example, if you have some function that takes three input parameters a, b, c, the computation result to produce NaN using Function A might be:
25-1-d4 Positive NaN: a=17 b=34 c=59==> FuncA(a,b,c)==> result=+NaN (0x7FC00000)

25-1. Create one function that takes at least one floating point input and returns one floating point output. For example, something that computes (b + sqrt(a)) / c (which might or might not be enough for all test cases). Tell us what the function does (normal human-readable math description is OK). A function with a switch statement that returns hard-coded special values is NOT acceptable.

25-2. Create one function called " PrintResult(float r)" that takes a floating point number as input and prints an output including the special values that you need to produce. If simply calling "printf" from within that function does the whole job, good for you. If not, do what you need to do for things to work. You can ONLY use one or more "printf" calls as your output function with whatever functionality is supported by printf-- no other library formatting helper routines accessed via function/procedure calls. It is OK to have internal decision logic and multiple different printf calls if you need to.

25-3. Create one function called " PrintHex(float r) " that takes a floating point number as input and prints an output indicating the hexadecimal value of the floating point variable.

25-4. Create a .c or .cpp file with the function, output printing functions, and test routines using 32-bit floating point variables. As mentioned, don't use standard templates and only use libraries for I/O and generating special floating point values. Also implement enough tests to complete the following (one test per result). We encourage you to use Cunit, but that is not required. Make sure that the code compiles with gcc on a course platform. It should compile warning free with default command line parameters, although the float to hex conversion might generate an unavoidable warning depending upon your compiler and how you write the code.

25-5. Your code, when run, shall produce the following output values using your floating point output function. Show the input variable values (floats) and the output variable values (both float and hex) as printed out from your program (a screenshot is fine). Some creativity might be required to display the values properly depending upon your runtime environment.

Rubric:

Supplemental Material: