floating_bar
floating_bar copied to clipboard
Handling square roots
A design decision must be made for how to calculate and handle square root values.
There's two ways to handle square roots:
- Return
Some
value only when it's an exact result (i.e. the numerator and denominator are perfect squares), orNone
otherwise. - Calculate a value close to its floating-point equivalent using continued fractions, and document the issues with saving or reusing such a value. This could be difficult to calculate dynamically, so maybe it's better to do some statistical analysis and pick a fixed number of iterations to do for all numbers.
There's also the option of providing both through different method names. The first method can be called checked_sqrt
and the second one sqrt
, where checked_sqrt
can be mentioned in the documentation for sqrt
in case that behavior is preferable to the user.
Update: sqrt()
has been implemented by converting to float, using {f32,f64}::sqrt()
, and then converting back to the corresponding rational value. The float conversion is derived from this post. Some tests should be written to ensure that it works.