Skip to content

Commit 51a34eb

Browse files
committed
Make Fraction IEquatable, update overrides and ops
1 parent b7d75ce commit 51a34eb

File tree

1 file changed

+79
-39
lines changed

1 file changed

+79
-39
lines changed

Mathos/Arithmetic/Fraction.cs

Lines changed: 79 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Fractions
1717
/// The the numerator and denominator are both Int64 (long).
1818
/// </remarks>
1919
[Serializable]
20-
public class Fraction : IRational
20+
public class Fraction : IRational, IEquatable<Fraction>
2121
{
2222
/// <summary>
2323
/// Gets or sets the numerator.
@@ -146,22 +146,44 @@ public override string ToString()
146146
}
147147

148148
/// <summary>
149-
/// Indicates whether this instance and a specified object are equal.
149+
/// Gets the hashcode of this instance.
150150
/// </summary>
151-
/// <returns>Whether <paramref name="obj"/> is equal to the fraction.</returns>
152-
/// <param name="obj">Another object to compare to. </param><filterpriority>2</filterpriority>
151+
/// <returns></returns>
152+
public override int GetHashCode()
153+
{
154+
unchecked
155+
{
156+
return (_denominator.GetHashCode() * 397) ^ Numerator.GetHashCode();
157+
}
158+
}
159+
160+
/// <summary>
161+
/// Indicates whether this instance and a given object are equal.
162+
/// </summary>
163+
/// <returns>Whether <paramref name="obj"/> is equal to the current fraction.</returns>
164+
/// <param name="obj">Another object to compare to. </param>
153165
public override bool Equals(object obj)
154166
{
155167
return obj != null && obj.GetType() == GetType() && this == (Fraction) obj;
156168
}
157169

170+
/// <summary>
171+
/// Indicates whether this instance and a given fraction are equal.
172+
/// </summary>
173+
/// <returns>Whether <paramref name="other"/> is equal to the current fraction.</returns>
174+
/// <param name="other">Another object to compare to. </param>
175+
public bool Equals(Fraction other)
176+
{
177+
return other != null && (Numerator == other.Numerator) && (Denominator == other.Denominator);
178+
}
179+
158180
/// <summary>
159181
/// Convert the fraction into a decimal.
160182
/// </summary>
161183
/// <returns>The fraction converted into a decimal.</returns>
162184
public decimal ToDecimal()
163185
{
164-
return (decimal)Numerator / _denominator;
186+
return (decimal) Numerator/_denominator;
165187
}
166188

167189
/// <summary>
@@ -170,7 +192,7 @@ public decimal ToDecimal()
170192
/// <returns>The fraction converted into a long.</returns>
171193
public long ToLong()
172194
{
173-
return Numerator / _denominator;
195+
return Numerator/_denominator;
174196
}
175197

176198
/// <summary>
@@ -182,6 +204,15 @@ public double ToDouble()
182204
return (double) Numerator/_denominator;
183205
}
184206

207+
/// <summary>
208+
/// Convert the fraction into a float.
209+
/// </summary>
210+
/// <returns>The fraction converted into a float.</returns>
211+
public float ToFloat()
212+
{
213+
return (float) Numerator/_denominator;
214+
}
215+
185216
/// <summary>
186217
/// Convert the fraction into a Stern-Brocot system
187218
/// </summary>
@@ -350,22 +381,31 @@ public static string ToCondensedSternBrocotSystem(string sternBrocotRepresentati
350381
/// <returns>The simplified form of the fraction.</returns>
351382
public Fraction Simplify()
352383
{
353-
var gdc = Numbers.Get.Gdc(Numbers.Convert.ToPositive(Numerator),
354-
Numbers.Convert.ToPositive(_denominator));
384+
var gdc = Get.Gdc(Numbers.Convert.ToPositive(Numerator), Numbers.Convert.ToPositive(_denominator));
355385

356386
return new Fraction(Numerator/gdc, _denominator/gdc);
357387
}
358388

359389
/// <summary>
360-
/// Find the inverse
390+
/// Gets the inverse of this instance.
361391
/// </summary>
362392
/// <example>2/3 -> 3/2</example>
363-
/// <returns>The inverse of the fraction.</returns>
393+
/// <returns>The inverse of this fraction.</returns>
364394
public Fraction Inverse()
365395
{
366396
return new Fraction(Denominator, Numerator);
367397
}
368398

399+
/// <summary>
400+
/// Gets the reciprocal of this instance.
401+
/// </summary>
402+
/// <example>2/3 -> 3/2</example>
403+
/// <returns>The reciprocal of this fraction.</returns>
404+
public Fraction Reciprocal()
405+
{
406+
return new Fraction(Denominator, Numerator);
407+
}
408+
369409
/// <summary>
370410
/// Avoid fractions like 2/-3 (better: -2/3),
371411
/// and -2/-3 (better: 2/3).
@@ -387,7 +427,10 @@ private void FractionChecker()
387427
/// <returns>Whether <paramref name="fractA"/> is equal to <paramref name="fractB"/>.</returns>
388428
public static bool operator ==(Fraction fractA, Fraction fractB)
389429
{
390-
fractA = fractA.Simplify(); // simplifying, e.g. if a is 4/2, and b 2/1, return true
430+
if (fractA == null || fractB == null)
431+
return fractA == null && fractB == null;
432+
433+
fractA = fractA.Simplify();
391434
fractB = fractB.Simplify();
392435

393436
return (fractA.Numerator == fractB.Numerator) && (fractA.Denominator == fractB.Denominator);
@@ -467,35 +510,34 @@ private void FractionChecker()
467510
/// <returns><paramref name="fractA"/> added to <paramref name="fractB"/>.</returns>
468511
public static Fraction operator +(Fraction fractA, Fraction fractB)
469512
{
470-
fractA = fractA.Simplify(); // simplifying
513+
fractA = fractA.Simplify();
471514
fractB = fractB.Simplify();
472515

473516
if (fractA.Denominator == fractB.Denominator)
474517
{
475-
var fraction = new Fraction //creating a new fraction
518+
var fraction = new Fraction
476519
{
477-
Numerator = fractA.Numerator + fractB.Numerator, // adding the nominators
478-
Denominator = fractA.Denominator // assigning the denominator
520+
Numerator = fractA.Numerator + fractB.Numerator,
521+
Denominator = fractA.Denominator
479522
};
480523

481-
return fraction.Simplify(); // a simplified version of the fraction
524+
return fraction.Simplify();
482525
}
483526

484-
var gdc = Numbers.Get.Gdc(fractA.Denominator, fractB.Denominator);
527+
var gdc = Get.Gdc(fractA.Denominator, fractB.Denominator);
485528

486529
if (gdc == 1)
487530
{
488-
// if the denominators are coprimes, i.e. no factors in common
489531
var fraction = new Fraction
490532
{
491533
Numerator = fractA.Numerator * fractB.Denominator + fractB.Numerator * fractA.Denominator,
492534
Denominator = fractA.Denominator * fractB.Denominator
493535
};
494536

495-
return fraction.Simplify(); // a simplified version of the fraction
537+
return fraction.Simplify();
496538
}
497539

498-
if (fractA.Denominator > fractB.Denominator) //fractA.Denominator will be the new denominator
540+
if (fractA.Denominator > fractB.Denominator)
499541
{
500542
var fraction = new Fraction
501543
{
@@ -505,7 +547,7 @@ private void FractionChecker()
505547

506548
return fraction.Simplify();
507549
}
508-
else //fractB.Denominator will be the new denominator
550+
else
509551
{
510552
var fraction = new Fraction
511553
{
@@ -525,34 +567,33 @@ private void FractionChecker()
525567
/// <returns><paramref name="fractB"/> substracted from <paramref name="fractA"/>.</returns>
526568
public static Fraction operator -(Fraction fractA, Fraction fractB)
527569
{
528-
//addition
529-
fractA = fractA.Simplify(); // simplifying
570+
fractA = fractA.Simplify();
530571
fractB = fractB.Simplify();
531572

532573
if (fractA.Denominator == fractB.Denominator)
533574
{
534-
var fraction = new Fraction //creating a new fraction
575+
var fraction = new Fraction
535576
{
536-
Numerator = fractA.Numerator - fractB.Numerator, // subtracting the nominators
537-
Denominator = fractA.Denominator // assigning the denominator
577+
Numerator = fractA.Numerator - fractB.Numerator,
578+
Denominator = fractA.Denominator
538579
};
539-
return fraction.Simplify(); // a simplified version of the fraction
580+
return fraction.Simplify();
540581
}
541582

542-
var gdc = Numbers.Get.Gdc(fractA.Denominator, fractB.Denominator);
583+
var gdc = Get.Gdc(fractA.Denominator, fractB.Denominator);
584+
543585
if (gdc == 1)
544586
{
545-
// if the denominators are coprimes, i.e. no factors in common
546587
var fraction = new Fraction
547588
{
548589
Numerator = fractA.Numerator * fractB.Denominator - fractB.Numerator * fractA.Denominator,
549590
Denominator = fractA.Denominator * fractB.Denominator
550591
};
551592

552-
return fraction.Simplify(); // a simplified version of the fraction
593+
return fraction.Simplify();
553594
}
554595

555-
if (fractA.Denominator > fractB.Denominator) //fractA.Denominator will be the new denominator
596+
if (fractA.Denominator > fractB.Denominator)
556597
{
557598
var fraction = new Fraction
558599
{
@@ -562,7 +603,7 @@ private void FractionChecker()
562603

563604
return fraction.Simplify();
564605
}
565-
else //fractB.Denominator will be the new denominator
606+
else
566607
{
567608
var fraction = new Fraction
568609
{
@@ -582,7 +623,7 @@ private void FractionChecker()
582623
/// <returns><paramref name="fractA"/> multiplied by <paramref name="fractB"/>.</returns>
583624
public static Fraction operator *(Fraction fractA, Fraction fractB)
584625
{
585-
fractA = fractA.Simplify(); // simplifying
626+
fractA = fractA.Simplify();
586627
fractB = fractB.Simplify();
587628

588629
return new Fraction(fractA.Numerator * fractB.Numerator,
@@ -597,7 +638,7 @@ private void FractionChecker()
597638
/// <returns><paramref name="fractA"/> divided by <paramref name="fractB"/>.</returns>
598639
public static Fraction operator /(Fraction fractA, Fraction fractB)
599640
{
600-
fractA = fractA.Simplify(); // simplifying
641+
fractA = fractA.Simplify();
601642
fractB = fractB.Simplify();
602643

603644
return new Fraction(fractA.Numerator * fractB.Denominator,
@@ -613,12 +654,11 @@ public static implicit operator Fraction(string value)
613654
{
614655
Fraction fraction;
615656

616-
if (value.Contains("/")) //checking if the separator exists
657+
if (value.Contains("/"))
617658
{
618-
// if it is a string input
619659
try
620660
{
621-
value = value.Trim(' '); // trim away unnessesary stuff
661+
value = value.Trim(' ');
622662

623663
fraction = new Fraction
624664
{
@@ -634,7 +674,7 @@ public static implicit operator Fraction(string value)
634674
}
635675
}
636676

637-
value = value.Trim(' '); // trim away unnessesary stuff
677+
value = value.Trim(' ');
638678

639679
fraction = new Fraction
640680
{
@@ -660,7 +700,7 @@ public static implicit operator Fraction(long num)
660700
/// </summary>
661701
/// <param name="fraction">The fraction to convert into a double.</param>
662702
/// <returns>Returns <paramref name="fraction"/> converted into a double.</returns>
663-
public static implicit operator decimal(Fraction fraction)
703+
public static explicit operator decimal(Fraction fraction)
664704
{
665705
return (decimal)fraction.Numerator / fraction.Denominator;
666706
}

0 commit comments

Comments
 (0)