Be sure to look at the Canvas Module for this week for various hand-in mechanisms!

### C24 In Class Exercise: Floating Point NaN Handling

This is a group programming assignment that should be completed in class.

Create a procedure that enforces a speed limit:

• bool SpeedLimitShutdown(double currentSpeed, double speedLimit) { ... }

This procedure compares a robot's current speed to a speed limit and returns FALSE if the robot is not exceeding the speed limit. It returns TRUE if the robot is exceeding the speed limit. The general use of this procedure is to implement an emergency shutdown function whenever the result is TRUE. Assume that the robot uses direct drive electric motors with a conventional automotive-style steering system (Ackerman steering). Try to create the simplest possible procedure that handles relevant special cases. Don't forget to consider that in reverse gear currentSpeed will be negative.

Also create and execute a set of unit tests. You can use CUnit, Google testing frameworks, brute force, or whatever unit test process you prefer. Be sure to cover the following cases:

• Boundary conditions (smallest possible difference above and below speed limit, as well as exactly equal to speed limit)
• All floating point special values that are likely to cause a problem

Hand in:

• One slide with code
• One slide with a table showing test plan and results. Table should have one row per test, and columns:
• Test #
• currentSpeed value
• speedLimit value
• currect output (true/false)
• test output (true/false)
• PASS or FAIL

Prioritize your work so that you are able to hand in code that compiles and a COMPLETE test plan table. Do as many tests as you can in time available. This means that if you don't have time to finish your test plan will have all the tests, but some of them will have blank test output and pass/fail cells. As long as you hand in the code, a reasonable table, and have done at least a couple tests you'll get full credit.

### Homework #24: Floating Point

24-1. 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.

• Name the file you hand in as we do for project files: hw24_student_[AndrewID]_[Family Name]_[First Name].cpp
• We want you to understand the guts of what really happens, so you are NOT permitted to call ANY library functions to EXCEPT for I/O handling (reading inputs in, printf for output) and functions to generate a special result. This means, for example, you shall not call "isnan()" and you shall not call "fpclassify()" because we want you to get a bit under the hood for this stuff and not just blindly rely on library functions.
• You can paste code from a library function such as isnan() into your program if you cite the source (so you can see how it actually gets done).
• You ARE allowed to call library functions for the purpose of using computations that might special values (e.g., OK to call a square root function or tangent function).
• We strongly suggest you use defined standard types for values that you might need. (See float.h)
• You can just do this homework at a Unix command line -- there is no need to involve ROS or the virtual machine mechanisms.
• Hand in both the source code file(s) and example run outputs. The example run outputs MUST be formatted as indicated to make grading practical. In other words, the answer to each "show a computation result" question must be printed out by the program itself, including the purpose of the run (e.g., "Positive NaN"), the inputs, the function called, and the resultant output.
• Do NOT convert binary values in hex to floats to create special values to be put into the tested function. (You might need to use hex values when deciding how to print outputs.) We want you to generate the results using normal floating point computations.
• Feed ONLY normal (non special values) as inputs to your function. For example, creating a NaN and then feeding it into a function that returns NaN is NOT what we want. So this means that special values should be created within the function itself based on normal (non-special) floating point values.
• Create ONE function that is used by all the tests. The calculation inside the function doesn't really need to make sense for any particular application.

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:
24-1-d4 Positive NaN: a=17 b=34 c=59==> FuncA(a,b,c)==> result=+NaN (0x7FC00000)

You should hand in two files for this homework question. First file: a single .cpp file with all the code including a main() function that can be compiled and run. Second file: powerpoint that gives the results lines from the program run annotated with any comments that are relevant. If you couldn't get a particular value to work, show what you could get to work and explain what you think went wrong.

24-1a.(5 points) 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). This should be mathematical notation (scan of hand-written is fine) and not source code. A function with a switch statement that returns hard-coded special values is NOT acceptable.

24-1b. (5 points) 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 -- no other library formatting helper routines. It is OK to have internal decision logic and multiple different printf calls if you need to.

24-1c. (5 points) 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.

24-1d. (5 points) Create a .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. Hand in the source code all as one .cpp file even if it is a bit large just to help with grading manageability. 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.

24-1e. (5 points each * 8=40 points) 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.

• 24-1e1. A non-special result (e.g., 1 /3=0.333333)
• 24-1e2. Positive infinity
• 24-1e3. Negative infinity
• 24-1e4. Positive NaN
• 24-1e5. Negative NaN
• 24-1e6. Positive zero
• 24-1e7. Negative zero
• 24-1e8. Largest positive denormalized value (i.e., the largest value that can be stored in a floating point number that is still a denorm). (If you're not quite sure what this is, see the supplemental material.)

Hand in: a single .cpp or .c file with all code (should be able to compile / if you need a special command line please note that in your powerpoint file), AND a powerpoint file with results (add comments in powerpoint as necessary so they make sense). Put them both into a single zip file with the usual naming convention as "HW24"

Supplemental Material: