Float point comparison in c_program

Float point comparison in c_program

by Jeremias Gomes -
Number of replies: 1

Hi,


Is it possible to use formulas to compare results in programs (c_program) that use floating point operations instead of comparing specific inputs and outputs?


What I want to do is use a formula to do the comparison like this:

((expected_output - provided_output) / 2) < 0.00001


Is this possible?


In reply to Jeremias Gomes

Re: Float point comparison in c_program

by Richard Lobb -

With C_function questions you can easily write tests that call the student's function (assumed to return a float value) and then check the returned value. For example you might have a test like:

got = student_func(...);
if (abs(got - expected) < 0.00001) {
     puts("OK");
} else {
     printf("Wrong. Expected %.4f, got %.4f\n", expected, got);
}

With the C_program question type, the code being run is just the C program written by the student. It consumes standard input, perhaps reads files, and generates some output. CodeRunner doesn't get into the act until the program run is over, at which point it has some output that it has to evaluate. 

The standard question types use the default Exact Match grader, which simply compares the textual got and expected strings and awards a pass only if the two match exactly. If you want to do something more elaborate, like extracting the floating point numbers from the output and comparing them with the expected values to some tolerance, you'll have to switch to using a template grader. With template graders, the template code is responsible for capturing the output from the student's program and grading it. The template grader has to suppress all output from the student's program, instead printing a JSON record describing either a row of the result table or the entire feedback for a per-test-template grader and a combinator-template grader respectively.

Here at Canterbury my colleagues and I do this regularly, but it's not for the faint of heart nor for CodeRunner newbies. Are you sure you can't achieve the same desired learning outcomes by using C_function questions?

To find out more about template graders, check out the section Grading with Templates in the documentation. One of these days I'll make a video describing how to write template graders, as they're invaluable for handling more challenging assessment tasks.