2014-05-20 11:06:26 -04:00
|
|
|
/*
|
|
|
|
Copyright 2014 Alex Dyachenko
|
|
|
|
|
|
|
|
This file is part of the MPIR Library.
|
|
|
|
|
|
|
|
The MPIR Library is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
|
|
|
by the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
your option) any later version.
|
|
|
|
|
|
|
|
The MPIR Library is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with the MPIR Library. If not, see http://www.gnu.org/licenses/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
|
|
|
|
namespace MPIR.Tests.HugeRationalTests
|
|
|
|
{
|
|
|
|
[TestClass]
|
|
|
|
public class Arithmetic
|
|
|
|
{
|
|
|
|
#region Add
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalAddHugeRational()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
|
|
|
|
using (var b = new HugeRational("222987435987982730594288574029879874539/590872612825179551336102196593"))
|
|
|
|
using (var c = new HugeRational(a + b))
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
Assert.AreEqual(a.Numerator * b.Denominator + b.Numerator * a.Denominator, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator * b.Denominator, c.Denominator);
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-23 13:24:37 -04:00
|
|
|
[TestMethod]
|
|
|
|
public void RationalAddHugeInt()
|
|
|
|
{
|
|
|
|
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
|
|
|
|
using (var b = new HugeInt("222987435987982730594288574029879874539"))
|
|
|
|
using (var c = new HugeRational(a + b))
|
|
|
|
{
|
|
|
|
Assert.AreEqual(a.Numerator + b * a.Denominator, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator, c.Denominator);
|
2014-05-23 17:00:32 -04:00
|
|
|
c.Value = (b + 1) + a;
|
|
|
|
Assert.AreEqual(a.Numerator + (b + 1) * a.Denominator, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator, c.Denominator);
|
2014-05-23 13:24:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-20 11:06:26 -04:00
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalAddLimb()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
|
|
|
|
using (var c = new HugeRational())
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Ui(4288574029879874539UL, 4288574029U);
|
2014-05-20 17:15:58 -04:00
|
|
|
c.Value = a + b;
|
|
|
|
Assert.AreEqual(a.Numerator + b * a.Denominator, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator, c.Denominator);
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalAddToLimb()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
|
|
|
|
using (var c = new HugeRational())
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Ui(4288574029879874539UL, 4279874539U);
|
2014-05-20 17:15:58 -04:00
|
|
|
c.Value = b + a;
|
|
|
|
Assert.AreEqual(a.Numerator + b * a.Denominator, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator, c.Denominator);
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalAddSignedLimb()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
|
|
|
|
using (var c = new HugeRational())
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Si(-4288574029879874539L, -1279874539);
|
2014-05-20 17:15:58 -04:00
|
|
|
c.Value = a + b;
|
|
|
|
Assert.AreEqual(a.Numerator + b * a.Denominator, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator, c.Denominator);
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalAddToSignedLimb()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
|
|
|
|
using (var c = new HugeRational())
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Si(-4288574029879874539L, -1279874539);
|
2014-05-20 17:15:58 -04:00
|
|
|
c.Value = b + a;
|
|
|
|
Assert.AreEqual(a.Numerator + b * a.Denominator, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator, c.Denominator);
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalAddToMaxSignedLimb()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
|
|
|
|
using (var c = new HugeRational())
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Si(long.MinValue, int.MinValue);
|
2014-05-20 17:15:58 -04:00
|
|
|
c.Value = b + a;
|
|
|
|
Assert.AreEqual(a.Numerator + b * a.Denominator, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator, c.Denominator);
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalAddExpressionHugeRational()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
|
|
|
|
using (var b = new HugeRational("222987435987982730594288574029879874539/590872612825179551336102196593"))
|
2014-05-20 11:06:26 -04:00
|
|
|
using (var c = new HugeRational())
|
|
|
|
{
|
|
|
|
c.Value = 1 + (a + b);
|
2014-05-20 17:15:58 -04:00
|
|
|
Assert.AreEqual(a.Numerator * b.Denominator + b.Numerator * a.Denominator + a.Denominator * b.Denominator, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator * b.Denominator, c.Denominator);
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Subtract
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalSubtractHugeRational()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("445497268491433028939318409770173720259/115756986668303657898962467957"))
|
|
|
|
using (var b = new HugeRational("222987435987982730594288574029879874539/590872612825179551336102196593"))
|
|
|
|
using (var c = new HugeRational(a - b))
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
Assert.AreEqual(a.Numerator * b.Denominator - b.Numerator * a.Denominator, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator * b.Denominator, c.Denominator);
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-23 17:00:32 -04:00
|
|
|
[TestMethod]
|
|
|
|
public void RationalSubtractHugeInt()
|
|
|
|
{
|
|
|
|
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
|
|
|
|
using (var b = new HugeInt("222987435987982730594288574029879874539"))
|
|
|
|
using (var c = new HugeRational(a - b))
|
|
|
|
{
|
|
|
|
Assert.AreEqual(a.Numerator - b * a.Denominator, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator, c.Denominator);
|
|
|
|
c.Value = (b + 1) - a;
|
|
|
|
Assert.AreEqual((b + 1) * a.Denominator - a.Numerator, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator, c.Denominator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-20 11:06:26 -04:00
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalSubtractLimb()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Ui(4288574029879874539UL, 2885740298U);
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("222509832503450298349318409770173720259/115756986668303657898962467957"))
|
|
|
|
using (var c = new HugeRational(a - b))
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
Assert.AreEqual(a.Numerator - b * a.Denominator, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator, c.Denominator);
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalSubtractFromLimb()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Ui(4288574029879874539UL, 2885740298U);
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("222509832503450298349318409770173720259/115756986668303657898962467957"))
|
|
|
|
using (var c = new HugeRational(b - a))
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
Assert.AreEqual(b * a.Denominator - a.Numerator, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator, c.Denominator);
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalSubtractPositiveSignedLimb()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Si(4288574029879874539L, 1885740298);
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("222509832503450298349318409770173720259/115756986668303657898962467957"))
|
|
|
|
using (var c = new HugeRational(a - b))
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
Assert.AreEqual(a.Numerator - b * a.Denominator, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator, c.Denominator);
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalSubtractFromPositiveSignedLimb()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Si(4288574029879874539L, 1885740298);
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("222509832503450298349318409770173720259/115756986668303657898962467957"))
|
|
|
|
using (var c = new HugeRational(a - b))
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
Assert.AreEqual(a.Numerator - b * a.Denominator, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator, c.Denominator);
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalSubtractSignedLimb()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Si(-4288574029879874539L, -1885740298);
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
|
|
|
|
using (var c = new HugeRational(a - b))
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
Assert.AreEqual(a.Numerator - b * a.Denominator, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator, c.Denominator);
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalSubtractFromSignedLimb()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Si(-4288574029879874539L, -1885740298);
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
|
|
|
|
using (var c = new HugeRational(b - a))
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
Assert.AreEqual(b * a.Denominator - a.Numerator, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator, c.Denominator);
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Multiply
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalMultiplyByHugeRational()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("90234098723098475098479385345098345/115756986668303657898962467957"))
|
|
|
|
using (var b = new HugeRational("7859487359873459872354987610987897/590872612825179551336102196593"))
|
|
|
|
using (var c = new HugeRational(a * b))
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
Assert.AreEqual(a.Numerator * b.Numerator, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator * b.Denominator, c.Denominator);
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-23 17:00:32 -04:00
|
|
|
[TestMethod]
|
|
|
|
public void RationalMultiplytHugeInt()
|
|
|
|
{
|
|
|
|
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
|
|
|
|
using (var b = new HugeInt("222987435987982730594288574029879874539"))
|
|
|
|
using (var c = new HugeRational(a * b))
|
|
|
|
{
|
|
|
|
Assert.AreEqual(a.Numerator * b, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator, c.Denominator);
|
|
|
|
c.Value = (b + 1) * a;
|
|
|
|
Assert.AreEqual((b + 1) * a.Numerator, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator, c.Denominator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-20 11:06:26 -04:00
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalMultiplyByLimb()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("90234098723098475098479385345098345/115756986668303657898962467957"))
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Ui(17390538260286101342, 1500450271);
|
2014-05-20 11:06:26 -04:00
|
|
|
a.Value = a * b;
|
2016-01-14 14:07:20 -05:00
|
|
|
Assert.AreEqual(Platform.Select(
|
|
|
|
"1569219546226477273686601978789044606491747469626478990/115756986668303657898962467957",
|
|
|
|
"135391777882513860921200145428966240276901495/115756986668303657898962467957"),
|
|
|
|
a.ToString());
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalMultiplyLimbBy()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("90234098723098475098479385345098345/115756986668303657898962467957"))
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Ui(17390538260286101342, 1500450271);
|
2014-05-20 11:06:26 -04:00
|
|
|
a.Value = b * a;
|
2016-01-14 14:07:20 -05:00
|
|
|
Assert.AreEqual(Platform.Select(
|
|
|
|
"1569219546226477273686601978789044606491747469626478990/115756986668303657898962467957",
|
|
|
|
"135391777882513860921200145428966240276901495/115756986668303657898962467957"),
|
|
|
|
a.ToString());
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalMultiplyBySignedLimb()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("90234098723098475098479385345098345/115756986668303657898962467957"))
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Si(-7390538260286101342, -1987450271);
|
2014-05-20 11:06:26 -04:00
|
|
|
a.Value = a * b;
|
2016-01-14 14:07:20 -05:00
|
|
|
Assert.AreEqual(Platform.Select(
|
|
|
|
"-666878558995492522701808125338061156491747469626478990/115756986668303657898962467957",
|
|
|
|
"-179335783960662818294159606092029134291901495/115756986668303657898962467957"),
|
|
|
|
a.ToString());
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalMultiplySignedLimbBy()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("90234098723098475098479385345098345/115756986668303657898962467957"))
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Si(-7390538260286101342, -1987450271);
|
2014-05-20 11:06:26 -04:00
|
|
|
a.Value = b * a;
|
2016-01-14 14:07:20 -05:00
|
|
|
Assert.AreEqual(Platform.Select(
|
|
|
|
"-666878558995492522701808125338061156491747469626478990/115756986668303657898962467957",
|
|
|
|
"-179335783960662818294159606092029134291901495/115756986668303657898962467957"),
|
|
|
|
a.ToString());
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Shift Left
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalShiftLeft()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-21 11:47:51 -04:00
|
|
|
using (var a = new HugeRational("-12345700987ABCDEF2345CBDEFA245230948/17607EF654EB9A13FFA163C75", 16))
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
uint b = 40;
|
2014-05-20 11:06:26 -04:00
|
|
|
a.Value = a << b;
|
2014-05-21 11:47:51 -04:00
|
|
|
Assert.AreEqual("-12345700987ABCDEF2345CBDEFA2452309480000000000/17607EF654EB9A13FFA163C75", a.ToString(16));
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Shift Right
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalShiftRight()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-21 11:47:51 -04:00
|
|
|
using (var a = new HugeRational("ABCDEF052834524092854092874502983745029345723098457209305983434345/17607EF654EB9A13FFA163C75", 16))
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
uint b = 96;
|
2014-05-20 11:06:26 -04:00
|
|
|
a.Value = a >> b;
|
2014-05-21 11:47:51 -04:00
|
|
|
Assert.AreEqual("ABCDEF052834524092854092874502983745029345723098457209305983434345/17607EF654EB9A13FFA163C75000000000000000000000000", a.ToString(16));
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Negate
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalNegate()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("24092854092874502983745029345723098457209/115756986668303657898962467957"))
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
|
|
|
a.Value = -a;
|
2014-05-20 17:15:58 -04:00
|
|
|
Assert.AreEqual("-24092854092874502983745029345723098457209/115756986668303657898962467957", a.ToString());
|
2014-05-20 11:06:26 -04:00
|
|
|
a.Value = -a;
|
2014-05-20 17:15:58 -04:00
|
|
|
Assert.AreEqual("24092854092874502983745029345723098457209/115756986668303657898962467957", a.ToString());
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Abs
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalMakeAbsolute()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("-24092854092874502983745029345723098457209/115756986668303657898962467957"))
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
|
|
|
a.Value = a.Abs();
|
2014-05-20 17:15:58 -04:00
|
|
|
Assert.AreEqual("24092854092874502983745029345723098457209/115756986668303657898962467957", a.ToString());
|
2014-05-20 11:06:26 -04:00
|
|
|
a.Value = a.Abs();
|
2014-05-20 17:15:58 -04:00
|
|
|
Assert.AreEqual("24092854092874502983745029345723098457209/115756986668303657898962467957", a.ToString());
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2014-06-03 10:44:28 -04:00
|
|
|
#region Invert
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
public void RationalInvert()
|
|
|
|
{
|
|
|
|
using(var a = new HugeRational("-24092854092874502983745029345723098457209/115756986668303657898962467957"))
|
|
|
|
{
|
|
|
|
a.Value = a.Invert();
|
|
|
|
Assert.AreEqual("-115756986668303657898962467957/24092854092874502983745029345723098457209", a.ToString());
|
|
|
|
a.Value = a.Invert();
|
|
|
|
Assert.AreEqual("-24092854092874502983745029345723098457209/115756986668303657898962467957", a.ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Power
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
public void RationalPower()
|
|
|
|
{
|
|
|
|
using(var n = new HugeInt("-24092854092874502983745029345723098457209"))
|
|
|
|
using(var d = new HugeInt("115756986668303657898962467957"))
|
|
|
|
using(var a = new HugeRational(n, d))
|
|
|
|
{
|
|
|
|
a.Value = a ^ 5;
|
|
|
|
Assert.AreEqual(n ^ 5, a.Numerator);
|
|
|
|
Assert.AreEqual(d ^ 5, a.Denominator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2014-05-20 11:06:26 -04:00
|
|
|
#region Divide
|
|
|
|
|
|
|
|
#region Int
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalDivideHugeRational()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("115756986668303657898962467957/394580293847502987609283945873594873409587"))
|
|
|
|
using (var b = new HugeRational("593169091750307653294"))
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
|
|
|
a.Value = a / b;
|
2014-05-20 17:15:58 -04:00
|
|
|
Assert.AreEqual("115756986668303657898962467957/234052834524092854092760134269509268758750275703033222451729578", a.ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
public void RationalDivideHugeRational2()
|
|
|
|
{
|
|
|
|
using (var a = new HugeRational("90234098723098475098479385345098345/115756986668303657898962467957"))
|
|
|
|
using (var b = new HugeRational("6847944682037444681162770672798288913849/590872612825179551336102196593"))
|
|
|
|
using (var c = new HugeRational(a / b))
|
|
|
|
{
|
|
|
|
Assert.AreEqual(a.Numerator * b.Denominator, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator * b.Numerator, c.Denominator);
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-26 15:05:21 -04:00
|
|
|
[TestMethod]
|
|
|
|
public void RationalDivideHugeInt()
|
|
|
|
{
|
|
|
|
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
|
|
|
|
using (var b = new HugeInt("222987435987982730594288574029879874539"))
|
|
|
|
using (var c = new HugeRational(a / b))
|
|
|
|
using (var d = new HugeRational())
|
|
|
|
{
|
|
|
|
Assert.AreEqual(a.Numerator, c.Numerator);
|
|
|
|
Assert.AreEqual(a.Denominator * b, c.Denominator);
|
|
|
|
c.Value = (b + 1) / a;
|
|
|
|
d.Numerator.Value = (b + 1) * a.Denominator;
|
|
|
|
d.Denominator.Value = a.Numerator;
|
|
|
|
d.Canonicalize();
|
|
|
|
Assert.AreEqual(d, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-20 11:06:26 -04:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Limb
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void RationalDivideLimb()
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2014-05-20 17:15:58 -04:00
|
|
|
using (var a = new HugeRational("115756986668303657898962467957/39458029384750298767200622330399462537522498"))
|
2014-05-20 11:06:26 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Ui(5931690917503076532, 3367900313);
|
2014-05-20 11:06:26 -04:00
|
|
|
a.Value = a / b;
|
2016-01-14 14:07:20 -05:00
|
|
|
Assert.AreEqual(Platform.Select(
|
|
|
|
"115756986668303657898962467957/234052834524092854092874502983745029345723092857791404165816936",
|
|
|
|
"115756986668303657898962467957/132890709515263728644898490080347139295153795258741874"),
|
|
|
|
a.ToString());
|
2014-05-20 11:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-03 10:44:28 -04:00
|
|
|
[TestMethod]
|
|
|
|
public void RationalDivideSignedLimb()
|
|
|
|
{
|
|
|
|
using(var a = new HugeRational("115756986668303657898962467957/39458029384750298767200622330399462537522498"))
|
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Si(-5931690917503076532, -1500450271);
|
2014-06-03 10:44:28 -04:00
|
|
|
a.Value = a / b;
|
2016-01-14 14:07:20 -05:00
|
|
|
Assert.AreEqual(Platform.Select(
|
|
|
|
"-115756986668303657898962467957/234052834524092854092874502983745029345723092857791404165816936",
|
|
|
|
"-115756986668303657898962467957/59204810883474549052577139687016525102679979792696958"),
|
|
|
|
a.ToString());
|
2014-06-03 10:44:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
public void RationalDivideLimbBy()
|
|
|
|
{
|
|
|
|
using(var a = new HugeRational("115756986668303657898962467957/39458029384750298767200622330399462537522498"))
|
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Ui(5931690917503076532, 3367900313);
|
2014-06-03 10:44:28 -04:00
|
|
|
a.Value = b / a;
|
2016-01-14 14:07:20 -05:00
|
|
|
Assert.AreEqual(Platform.Select(
|
|
|
|
"234052834524092854092874502983745029345723092857791404165816936/115756986668303657898962467957",
|
|
|
|
"132890709515263728644898490080347139295153795258741874/115756986668303657898962467957"),
|
|
|
|
a.ToString());
|
2014-06-03 10:44:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
public void RationalDivideSignedLimbBy()
|
|
|
|
{
|
|
|
|
using(var a = new HugeRational("115756986668303657898962467957/39458029384750298767200622330399462537522498"))
|
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Si(-5931690917503076532, -1500450271);
|
2014-06-03 10:44:28 -04:00
|
|
|
a.Value = b / a;
|
2016-01-14 14:07:20 -05:00
|
|
|
Assert.AreEqual(Platform.Select(
|
|
|
|
"-234052834524092854092874502983745029345723092857791404165816936/115756986668303657898962467957",
|
|
|
|
"-59204810883474549052577139687016525102679979792696958/115756986668303657898962467957"),
|
|
|
|
a.ToString());
|
2014-06-03 10:44:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-20 11:06:26 -04:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|