| | 1 | | namespace SharpFractions; |
| | 2 | |
|
| | 3 | | public readonly partial struct Fraction : IComparable<Fraction>, IEquatable<Fraction> |
| | 4 | | { |
| | 5 | | public static Fraction operator +(Fraction frac) |
| 1 | 6 | | { |
| 1 | 7 | | return new(frac.Numerator, frac.Denominator); |
| 1 | 8 | | } |
| | 9 | |
|
| | 10 | | public static Fraction operator +(Fraction left, Fraction right) |
| 1 | 11 | | { |
| 1 | 12 | | (left, right) = PutOnCommonDenominator(left, right); |
| | 13 | |
|
| 1 | 14 | | return new(left.Numerator + right.Numerator, left.Denominator); |
| 1 | 15 | | } |
| | 16 | |
|
| | 17 | | public static Fraction operator -(Fraction frac) |
| 1 | 18 | | { |
| 1 | 19 | | return new(-frac.Numerator, frac.Denominator); |
| 1 | 20 | | } |
| | 21 | |
|
| | 22 | | public static Fraction operator -(Fraction left, Fraction right) |
| 3 | 23 | | { |
| 3 | 24 | | (left, right) = PutOnCommonDenominator(left, right); |
| | 25 | |
|
| 3 | 26 | | return new(left.Numerator - right.Numerator, left.Denominator); |
| 3 | 27 | | } |
| | 28 | |
|
| | 29 | | public static Fraction operator *(Fraction left, Fraction right) |
| 1 | 30 | | { |
| 1 | 31 | | return new(left.Numerator * right.Numerator, left.Denominator * right.Denominator); |
| 1 | 32 | | } |
| | 33 | |
|
| | 34 | | public static Fraction operator /(Fraction left, Fraction right) |
| 2 | 35 | | { |
| 3 | 36 | | if (right.Numerator == 0) throw new DivideByZeroException(); |
| | 37 | |
|
| 1 | 38 | | return new(left.Numerator * right.Denominator, left.Denominator * right.Numerator); |
| 1 | 39 | | } |
| | 40 | |
|
| | 41 | |
|
| | 42 | | // For interaction with integer types: |
| | 43 | |
|
| 3 | 44 | | public static implicit operator Fraction(BigInteger bigInteger) => new(bigInteger); |
| 1 | 45 | | public static implicit operator Fraction(long longInteger) => new(longInteger); |
| 1 | 46 | | public static implicit operator Fraction(int integer) => new(integer); |
| 1 | 47 | | public static implicit operator Fraction(short shortInteger) => new(shortInteger); |
| | 48 | | } |