Extend Square Root to Work With Floating Points?

The exercise currently works with integer input and output. Perhaps it should be extended to work with floating points?

Interesting. What do you see as the learning benefit? Any specific tracks you’re thinking of this for?

My main motivation was to make sure whatever someone implements works correctly, i.e. it also works with real numbers and not only with square integers. Another advantage is that we can require the solution to be within certain precision away from the exact square root. This might help people understand topics like numerical errors, rate of convergence, etc.

Ok, thanks. I’d welcome other’s thoughts on this. It has the downside that it would break all the existing solutions but the upside of potentially teaching more :slight_smile:

1 Like

Assuming all the existing solutions only support perfect squares, I would suggest that changing this to support other values might make more sense as a second/additional exercise.

I do not see that currently extending this from the student’s standpoint breaks any tests.

So does the current specification get in the way of extending this if you want to explore?

The statement:

The exercise currently works with integer input and output. Perhaps it should be extended to work with floating points?

does not say that it currently only works with integer input and output.

I’d welcome other’s thoughts on this.

Yes, same, I am curious to see what the others think.

I’ve found Methods of computing square roots - Wikipedia is a nice tidy algorithm for integer square roots

Counter-argument then, if we presume that currently all student’s code would work with any new tests, what is the advantage to adding more tests? :slight_smile:

Counter-argument then, if we presume that currently all student’s code would work with any new tests, what is the advantage to adding more tests? :slight_smile:

To verify that the solution really works. There is no guarantee that an algorithm for computing the square root of a number (e.g. Newton’s method) is implemented correctly just because it works on square integers.

Another advantage is to require certain precision (e.g. 1.0e-6) and force students to tune the algorithm (e.g. add more iterations).

None, which is my point. They are not, from what I can see, restricted from exploring this already.

The current description explicitly mentions the exercise being about “natural radicans”, so working with natural numbers (integers): problem-specifications/exercises/square-root/description.md at main · exercism/problem-specifications · GitHub If we were to change the exercise, we should also update the text. That’s not to say we can’t, but it’s relatively invasive.

This might help people understand topics like numerical errors, rate of convergence, etc.

I think these are nice things to help students work with, I’m only wondering whether extending the existing exercise is the best way to do this.

1 Like