tompazourek/Rationals

Add Simplify method

paulirwin opened this issue · 2 comments

I found it trivial to add the ability to simplify Rationals for a project I'm working on, so figured I'd contribute it upstream. I'll be submitting a PR for this shortly, but tracking it as an issue for posterity.

Would like an instance method Simplify() on Rational that computes the greatest common divisor of the numerator and denominator, and then if that gcd is != 1, divides both the numerator and denominator by the gcd and returns a new rational, otherwise returns itself (as already simplified).

Example:

var r = new Rational(14, 26);
var s = r.Simplify(); // returns 7/13

Personally, I like this being an instance method as it feels natural in usage, but if you would prefer to make it a static method instead I would not be opposed to changing my PR. Thanks for this great library!

... and I just noticed there is an existing CanonicalForm property. Sigh. Sorry for the noise. 😄

No problem, I'm glad you found it. :)