[Space Age] Why are the tests expecting rounded values?

I was wondering why the Space Age exercise in the Kotlin track expects the values to be rounded to 2 decimal places? So far I haven’t encountered this in any other track, and there is absolutely no mention of it in the exercise instructions.

I checked the reference implementation and that even uses BigDecimal to calculate the results with the correct precision - that seems totally out-of-scope for this exercise IMO.

It’s because the test data also suggest using two decimals: https://github.com/exercism/problem-specifications/blob/a8d3df1f6c761501bb7f65bd96decd36baf4edf6/exercises/space-age/canonical-data.json#L4 We need to do something with decimals as floating point comparisons are notoriously fickle, but the best way to go about this is to have the tests only consider the first two decimal places and ignore the latter. See this C# test https://github.com/exercism/csharp/blob/main/exercises/practice/space-age/SpaceAgeTests.cs

I don’t know if the test framework Kotlin uses supports this, but if so, we should update.

That is a good point.

I checked the reference of kotlin.test.assertEquals() and it seems to have an overload that takes an absoluteTolerance parameter. This would allow the tests to check with a precision of 2 decimals, without requiring the solution to perform any rounding:

private fun assertYearsEqual(expectedYears: Double, actualYears: Double) = 
    assertEquals(expectedYears, actualYears, absoluteTolerance = 0.01)

That sounds perfect! Would you be willing to submit a PR?

Sure thing, here you go: Update space-age: less strict on precision by sanderploegsma · Pull Request #598 · exercism/kotlin · GitHub

2 Likes

Thanks, this was a good nudge for me to add a similar fix on the not-yet launched Pyret track as well. :slight_smile:

Pr has been merged!