Represents a rational number.
Inheritance: IComparable, IConvertible
Ejemplo n.º 1
0
 public void ToStringDoesSomethingReasonable()
 {
     BigInteger n1 = BigInteger.Create(30);
     BigInteger d1 = BigInteger.Create(60);
     Ratio r1 = new Ratio(n1, d1);
     Expect(r1.ToString(), EqualTo("30/60"));
 }
Ejemplo n.º 2
0
 public void GetsThePiecesFromTheConstructedThing()
 {
     BigInteger n = BigInteger.Create(30);
     BigInteger d = BigInteger.Create(60);
     Ratio r = new Ratio(n, d);
     Expect(r.numerator, SameAs(n));
     Expect(r.denominator, SameAs(d));
 }
Ejemplo n.º 3
0
 public void RaionalizeWorksOnDoubles()
 {
     object r = Numbers.rationalize(0.0625);
     Expect(r, InstanceOf(typeof(Ratio)));
     Ratio rr = (Ratio)r;
     Ratio ratio = new Ratio(BigInteger.Create(1), BigInteger.Create(16));
     Expect(rr, EqualTo(ratio));
 }
Ejemplo n.º 4
0
        public void ToBigDecimalWithNoContextAndNoRoundingRequiredWorks()
        {
            BigInteger n1 = BigInteger.Create(1);
            BigInteger d1 = BigInteger.Create(4);
            Ratio r1 = new Ratio(n1, d1);
            BigDecimal bd = r1.ToBigDecimal();

            Expect(bd, EqualTo(BigDecimal.Parse("0.25")));
        }
Ejemplo n.º 5
0
 public void ToBigDecimalWithContextWorks()
 {
     BigInteger n1 = BigInteger.Create(1);
     BigInteger d1 = BigInteger.Create(3);
     Ratio r1 = new Ratio(n1, d1);
     BigDecimal.Context c = new BigDecimal.Context(6, BigDecimal.RoundingMode.HalfUp);
     BigDecimal bd = r1.ToBigDecimal(c);
     Expect(bd, EqualTo(BigDecimal.Parse("0.333333")));
 }
Ejemplo n.º 6
0
 public void RationalizeTakesFractionToBigInteger()
 {
     BigDecimal bd = BigDecimal.Parse("123.45");
     object r = Numbers.rationalize(bd);
     Expect(r, InstanceOf(typeof(Ratio)));
     Ratio rr = (Ratio)r;
     Ratio ratio = new Ratio(BigInteger.Create(12345/5), BigInteger.Create(100/5));
     Expect(rr, EqualTo(ratio));
 }
Ejemplo n.º 7
0
 public void EqualsReturnsTrueOnEquals()
 {
     BigInteger n1 = BigInteger.Create(30);
     BigInteger d1 = BigInteger.Create(60);
     Ratio r1 = new Ratio(n1, d1);
     BigInteger n2 = BigInteger.Create(30);
     BigInteger d2 = BigInteger.Create(60);
     Ratio r2 = new Ratio(n2, d2);
     Expect(r1.Equals(r2));
 }
Ejemplo n.º 8
0
 public void ToBigDecimalWithNoContextThrowsIfRoundingIsRequired()
 {
     BigInteger n1 = BigInteger.Create(1);
     BigInteger d1 = BigInteger.Create(3);
     Ratio r1 = new Ratio(n1, d1);
     BigDecimal bd = r1.ToBigDecimal();
 }
Ejemplo n.º 9
0
 public void ToDoubleWorks()
 {
     BigInteger n1 = BigInteger.Create(1);
     BigInteger d1 = BigInteger.Create(3);
     Ratio r1 = new Ratio(n1, d1);
     BigDecimal.Context c = new BigDecimal.Context(6, BigDecimal.RoundingMode.HalfUp);
     double d = r1.ToDouble(null);
     Expect(d, EqualTo(0.3333333333333333));  // precision = 16
 }
Ejemplo n.º 10
0
 public void ToDoubleWorks()
 {
     BigInteger n1 = BigInteger.Create(1);
     BigInteger d1 = BigInteger.Create(3);
     Ratio r1 = new Ratio(n1, d1);
     double d = r1.ToDouble(null);
     Expect(d, EqualTo(0.3333333333333333));  // precision = 16
 }
Ejemplo n.º 11
0
        /// <summary>
        /// Determines of an object is equal to this object.
        /// </summary>
        /// <param name="obj">The object to compare to.</param>
        /// <returns><value>true</value> if the object is equal to this object; <value>false</value> otherwise.</returns>
        public override bool Equals(object obj)
        {
            Ratio r = obj as Ratio;

            return(r != null && r._numerator.Equals(_numerator) && r._denominator.Equals(_denominator));
        }