Coursemology / coursemology2

Rails 6 re-write of Coursemology
https://coursemology.org
MIT License
146 stars 77 forks source link

Formatting of double output in C++ test cases #2461

Closed leongwaikay closed 7 years ago

leongwaikay commented 7 years ago

Currently there are 2 issues:

  1. When the double is a whole number, e.g. 2.0, it only displays 2
  2. The double is rounded off to I think 5 decimal places. This is not desirable because we aim to teach students that precision matters when it comes to float and double types.

Proposed fix. double types should be displayed in full precision. C11 has std::to_string which does that. However, some string manipulation has to be done to remove trailing zeros while maintaining at least 1 dp.

If for display purpose the number is too long, perhaps truncate and append ... and on mouse hover show the full number.

fonglh commented 7 years ago

This PR was deployed a couple of days back and solves point 1. https://github.com/Coursemology/coursemology2/pull/2448 It's not retroactive so you'll have to update existing packages to see the benefit.

The double rounding off is from the str() method. We can change it to to_string if you prefer.

leongwaikay commented 7 years ago

Doesn't matter which method is used, just avoid rounding off. I did some Googling and it seems to suggest that to_string is useful, but you might want to do your own research on this.

fonglh commented 7 years ago

http://en.cppreference.com/w/cpp/string/basic_string/to_string suggests it could be misleading for small numbers and yield unexpected results for floating point types.

Is that ok for you?

leongwaikay commented 7 years ago

Converts a floating point value to a string with the same content as what std::sprintf(buf, "%f", value) would produce for sufficiently large buf.

Actually this is what we want because in C we use printf("%f", value) to display the value.

I get this output on my compiler

23.430000
0.000000
10000000000000000000000000000000000000000.000000
0.000000
123456789.000000

The only weird output is 10e40 for to_string. Can we actually use sprintf?

leongwaikay commented 7 years ago

Actually I think this might affect C++ code yah? I suppose we can use sprintf in the append to override custom_evaluator to fix the display.

fonglh commented 7 years ago

If you want the equivalent of printf("%f", value) then we shouldn't remove trailing zeroes at all so it'll match what the students see on their own machines.

We can change to to_string so you don't need to override custom_evaluator.

leongwaikay commented 7 years ago

Come to think of it, yea maybe we don't need to remove the trailing zeroes since printf truncates to 6dp. My concern is that when we move to teaching C++, their cout output might appear different.