2014-06-05 17:05:06 -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;
|
|
|
|
|
2014-11-23 22:34:20 -05:00
|
|
|
namespace MPIR.Tests.HugeFloatTests
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
[TestClass]
|
|
|
|
public class Comparisons
|
|
|
|
{
|
|
|
|
#region CompareTo
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatCompareToHugeFloat()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-06-16 16:03:07 -04:00
|
|
|
using (var a = new HugeFloat("-22250983250345029834503.9835740293845721345345354"))
|
|
|
|
using (var b = new HugeFloat("22250983250345029834502.9835740293845721345345354"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-06-16 16:03:07 -04:00
|
|
|
Assert.AreEqual(1, Math.Sign(b.CompareTo(a)));
|
|
|
|
Assert.AreEqual(-1,Math.Sign(a.CompareTo(b + 1)));
|
|
|
|
Assert.AreEqual(0, Math.Sign((a + 1).CompareTo(-b)));
|
|
|
|
Assert.AreEqual(1, Math.Sign(a.CompareTo(null)));
|
2014-06-05 17:05:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatCompareToObject()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-06-16 16:03:07 -04:00
|
|
|
using (var a = new HugeFloat("-22250983250345029834503.9835740293845721345345354"))
|
|
|
|
using (var b = new HugeFloat("22250983250345029834502.9835740293845721345345354"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-06-16 16:03:07 -04:00
|
|
|
Assert.AreEqual(1, Math.Sign(((IComparable)b).CompareTo((object)a)));
|
|
|
|
Assert.AreEqual(-1,Math.Sign(((IComparable)a).CompareTo((object)b)));
|
|
|
|
Assert.AreEqual(1, Math.Sign(((IComparable)a).CompareTo(null)));
|
|
|
|
Assert.AreEqual(0, Math.Sign(((IComparable)(a + 1)).CompareTo((object)-b)));
|
2014-06-05 17:05:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatCompareToExpression()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-06-16 16:03:07 -04:00
|
|
|
using (var a = new HugeFloat("-22250983250345029834503.9835740293845721345345354"))
|
|
|
|
using (var b = new HugeFloat("22250983250345029834502.9835740293845721345345354"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-06-16 16:03:07 -04:00
|
|
|
Assert.AreEqual(1, Math.Sign(((IComparable<FloatExpression>)b).CompareTo(a)));
|
|
|
|
Assert.AreEqual(-1,Math.Sign(((IComparable<FloatExpression>)a).CompareTo(b)));
|
|
|
|
Assert.AreEqual(1, Math.Sign(((IComparable<FloatExpression>)a).CompareTo(null)));
|
|
|
|
Assert.AreEqual(0, Math.Sign(((IComparable<FloatExpression>)(a + 1)).CompareTo(-b)));
|
2014-06-05 17:05:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
[ExpectedException(typeof(ArgumentException))]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatCompareToNonExpression()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-06-16 16:03:07 -04:00
|
|
|
using (var a = new HugeFloat("-222509832503450298345029835740293845721.57898962467957"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
a.CompareTo("abc");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatCompareToLimb()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
using (var a = new HugeFloat(Platform.Select("-22250982876541", "-222509821")))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Ui(22250982876540, 222509820);
|
2014-06-16 16:03:07 -04:00
|
|
|
Assert.AreEqual(-1,Math.Sign(a.CompareTo(b)));
|
|
|
|
Assert.AreEqual(-1,Math.Sign(a.CompareTo(b + 1)));
|
|
|
|
Assert.AreEqual(0, Math.Sign((-(a + 1)).CompareTo(b)));
|
|
|
|
Assert.AreEqual(0, Math.Sign((-a).CompareTo(b + 1)));
|
|
|
|
Assert.AreEqual(1, Math.Sign((-a).CompareTo(b)));
|
2014-06-05 17:05:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatCompareToSignedLimb()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
using (var a = new HugeFloat(Platform.Select("-222509825551", "-222509821")))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Si(-222509825550, -222509820);
|
2014-06-16 16:03:07 -04:00
|
|
|
Assert.AreEqual(-1,Math.Sign(a.CompareTo(b)));
|
|
|
|
Assert.AreEqual(-1,Math.Sign(a.CompareTo(b + 1)));
|
|
|
|
Assert.AreEqual(0, Math.Sign((a + 1).CompareTo(b)));
|
|
|
|
Assert.AreEqual(0, Math.Sign(a.CompareTo(b - 1)));
|
|
|
|
Assert.AreEqual(1, Math.Sign(a.CompareTo(b - 2)));
|
2014-06-05 17:05:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatCompareToSignedDouble()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var a = new HugeFloat("-222509821"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
double b = -222509820;
|
2014-06-16 16:03:07 -04:00
|
|
|
Assert.AreEqual(-1,Math.Sign(a.CompareTo(b)));
|
|
|
|
Assert.AreEqual(-1,Math.Sign(a.CompareTo(b + 1)));
|
|
|
|
Assert.AreEqual(0, Math.Sign((a + 1).CompareTo(b)));
|
|
|
|
Assert.AreEqual(0, Math.Sign(a.CompareTo(b - 1)));
|
|
|
|
Assert.AreEqual(1, Math.Sign(a.CompareTo(b - 1.1)));
|
2014-06-05 17:05:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region comparison operators with expr
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatOperatorLessThan()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-06-16 16:03:07 -04:00
|
|
|
using (var a = new HugeFloat("-2225098325034502983450.29835740293845721"))
|
|
|
|
using (var b = new HugeFloat("2225098325034502983450.29835740293845721"))
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var c = new HugeFloat())
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
c.Value = a;
|
|
|
|
Assert.IsTrue(a < b);
|
|
|
|
Assert.IsFalse(b < a);
|
|
|
|
Assert.IsFalse(a < c);
|
2014-06-16 16:03:07 -04:00
|
|
|
Assert.IsFalse(a > c);
|
2014-06-05 17:05:06 -04:00
|
|
|
Assert.IsFalse(a < null);
|
|
|
|
Assert.IsTrue(null < a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatOperatorLessThanOrEqual()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-06-16 16:03:07 -04:00
|
|
|
using (var a = new HugeFloat("-2225098325034502983451.29835740293845721"))
|
|
|
|
using (var b = new HugeFloat("2225098325034502983450.29835740293845721"))
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var c = new HugeFloat())
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
c.Value = a;
|
|
|
|
Assert.IsTrue(a <= b);
|
|
|
|
Assert.IsFalse(b <= a);
|
|
|
|
Assert.IsTrue(a <= c);
|
|
|
|
Assert.IsFalse(a <= null);
|
|
|
|
Assert.IsTrue(null <= a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatOperatorGreaterThan()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-06-16 16:03:07 -04:00
|
|
|
using (var a = new HugeFloat("-2225098325034502983451.29835740293845721"))
|
|
|
|
using (var b = new HugeFloat("2225098325034502983450.29835740293845721"))
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var c = new HugeFloat())
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
c.Value = a;
|
|
|
|
Assert.IsFalse(a > b);
|
|
|
|
Assert.IsTrue(b > a);
|
|
|
|
Assert.IsFalse(a > c);
|
|
|
|
Assert.IsTrue(a > null);
|
|
|
|
Assert.IsFalse(null > a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatOperatorGreaterThanOrEqual()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-06-16 16:03:07 -04:00
|
|
|
using (var a = new HugeFloat("-2225098325034502983451.29835740293845721"))
|
|
|
|
using (var b = new HugeFloat("2225098325034502983450.29835740293845721"))
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var c = new HugeFloat())
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
c.Value = a;
|
|
|
|
Assert.IsFalse(a >= b);
|
|
|
|
Assert.IsTrue(b >= a);
|
|
|
|
Assert.IsTrue(a >= c);
|
|
|
|
Assert.IsTrue(a >= null);
|
|
|
|
Assert.IsFalse(null >= a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region comparison operators with limb
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatOperatorLessThanLimb()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var a = new HugeFloat("3845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
ulong c = 5432;
|
|
|
|
ulong b = 5432349587;
|
|
|
|
Assert.IsTrue(a < b);
|
|
|
|
Assert.IsFalse(b < a);
|
|
|
|
Assert.IsFalse(a < c);
|
|
|
|
Assert.IsTrue(c < a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatOperatorLessThanOrEqualLimb()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var a = new HugeFloat("3845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
ulong c = 5432;
|
|
|
|
ulong b = 5432349587;
|
|
|
|
ulong d = 3845721;
|
|
|
|
Assert.IsTrue(a <= b);
|
|
|
|
Assert.IsFalse(b <= a);
|
|
|
|
Assert.IsFalse(a <= c);
|
|
|
|
Assert.IsTrue(c <= a);
|
|
|
|
Assert.IsTrue(a <= d);
|
|
|
|
Assert.IsTrue(d <= a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatOperatorGreaterThanLimb()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var a = new HugeFloat("3845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
ulong c = 5432;
|
|
|
|
ulong b = 5432349587;
|
|
|
|
Assert.IsFalse(a > b);
|
|
|
|
Assert.IsTrue(b > a);
|
|
|
|
Assert.IsTrue(a > c);
|
|
|
|
Assert.IsFalse(c > a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatOperatorGreaterThanOrEqualLimb()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var a = new HugeFloat("3845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
ulong c = 5432;
|
|
|
|
ulong b = 5432349587;
|
|
|
|
ulong d = 3845721;
|
|
|
|
Assert.IsFalse(a >= b);
|
|
|
|
Assert.IsTrue(b >= a);
|
|
|
|
Assert.IsTrue(a >= c);
|
|
|
|
Assert.IsFalse(c >= a);
|
|
|
|
Assert.IsTrue(a >= d);
|
|
|
|
Assert.IsTrue(d >= a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region comparison operators with signed limb
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatOperatorLessThanSignedLimb()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var a = new HugeFloat("-3845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
long c = -543254325432;
|
|
|
|
long b = -9587;
|
|
|
|
Assert.IsTrue(a < b);
|
|
|
|
Assert.IsFalse(b < a);
|
|
|
|
Assert.IsFalse(a < c);
|
|
|
|
Assert.IsTrue(c < a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatOperatorLessThanOrEqualSignedLimb()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var a = new HugeFloat("-3845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
long c = -543254325432;
|
|
|
|
long b = -9587;
|
|
|
|
long d = -3845721;
|
|
|
|
Assert.IsTrue(a <= b);
|
|
|
|
Assert.IsFalse(b <= a);
|
|
|
|
Assert.IsFalse(a <= c);
|
|
|
|
Assert.IsTrue(c <= a);
|
|
|
|
Assert.IsTrue(a <= d);
|
|
|
|
Assert.IsTrue(d <= a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatOperatorGreaterThanSignedLimb()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var a = new HugeFloat("-3845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
long c = -543254325432;
|
|
|
|
long b = -9587;
|
|
|
|
Assert.IsFalse(a > b);
|
|
|
|
Assert.IsTrue(b > a);
|
|
|
|
Assert.IsTrue(a > c);
|
|
|
|
Assert.IsFalse(c > a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatOperatorGreaterThanOrEqualSignedLimb()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var a = new HugeFloat("-3845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
long c = -543254325432;
|
|
|
|
long b = -9587;
|
|
|
|
long d = -3845721;
|
|
|
|
Assert.IsFalse(a >= b);
|
|
|
|
Assert.IsTrue(b >= a);
|
|
|
|
Assert.IsTrue(a >= c);
|
|
|
|
Assert.IsFalse(c >= a);
|
|
|
|
Assert.IsTrue(a >= d);
|
|
|
|
Assert.IsTrue(d >= a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region comparison operators with double
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatOperatorLessThanDouble()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var a = new HugeFloat("-3845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
double c = -543254325432;
|
|
|
|
double b = -9587;
|
|
|
|
Assert.IsTrue(a < b);
|
|
|
|
Assert.IsFalse(b < a);
|
|
|
|
Assert.IsFalse(a < c);
|
|
|
|
Assert.IsTrue(c < a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatOperatorLessThanOrEqualDouble()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var a = new HugeFloat("-3845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
double c = -543254325432;
|
|
|
|
double b = -9587;
|
|
|
|
double d = -3845721;
|
|
|
|
Assert.IsTrue(a <= b);
|
|
|
|
Assert.IsFalse(b <= a);
|
|
|
|
Assert.IsFalse(a <= c);
|
|
|
|
Assert.IsTrue(c <= a);
|
|
|
|
Assert.IsTrue(a <= d);
|
|
|
|
Assert.IsTrue(d <= a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatOperatorGreaterThanDouble()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var a = new HugeFloat("-3845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
double c = -543254325432;
|
|
|
|
double b = -9587;
|
|
|
|
Assert.IsFalse(a > b);
|
|
|
|
Assert.IsTrue(b > a);
|
|
|
|
Assert.IsTrue(a > c);
|
|
|
|
Assert.IsFalse(c > a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatOperatorGreaterThanOrEqualDouble()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var a = new HugeFloat("-3845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
double c = -543254325432;
|
|
|
|
double b = -9587;
|
|
|
|
double d = -3845721;
|
|
|
|
Assert.IsFalse(a >= b);
|
|
|
|
Assert.IsTrue(b >= a);
|
|
|
|
Assert.IsTrue(a >= c);
|
|
|
|
Assert.IsFalse(c >= a);
|
|
|
|
Assert.IsTrue(a >= d);
|
|
|
|
Assert.IsTrue(d >= a);
|
|
|
|
Assert.IsFalse(d - 0.1 >= a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Equals
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatEqualsHugeFloat()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-06-16 16:03:07 -04:00
|
|
|
using (var a = new HugeFloat("-2225098325034502983451.29835740293845721"))
|
|
|
|
using (var b = new HugeFloat("2225098325034502983450.29835740293845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
Assert.IsFalse(b.Equals(a));
|
2014-06-16 16:03:07 -04:00
|
|
|
Assert.IsFalse(a.Equals(b + 1));
|
|
|
|
Assert.IsTrue((a + 1).Equals(-b));
|
2014-06-05 17:05:06 -04:00
|
|
|
Assert.IsFalse(a.Equals(null));
|
2014-06-16 16:03:07 -04:00
|
|
|
Assert.IsTrue(Equals(a + 1, -b));
|
2014-06-05 17:05:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatEqualsExpression()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-06-16 16:03:07 -04:00
|
|
|
using (var a = new HugeFloat("-2225098325034502983451.29835740293845721"))
|
|
|
|
using (var b = new HugeFloat("2225098325034502983450.29835740293845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-11-23 22:34:20 -05:00
|
|
|
Assert.IsFalse(((IEquatable<FloatExpression>)b).Equals(a));
|
|
|
|
Assert.IsFalse(((IEquatable<FloatExpression>)a).Equals(b));
|
|
|
|
Assert.IsFalse(((IEquatable<FloatExpression>)a).Equals(null));
|
2014-06-16 16:03:07 -04:00
|
|
|
Assert.IsTrue(((IEquatable<FloatExpression>)(a + 1)).Equals(-b));
|
2014-06-05 17:05:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatEqualsNonExpression()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-06-16 16:03:07 -04:00
|
|
|
using (var a = new HugeFloat("-2225098325034502983450.29835740293845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
Assert.IsFalse(a.Equals("abc"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatEqualsLimb()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
using (var a = new HugeFloat(Platform.Select("222509832503", "2225098323")))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Ui(222509832504, 2225098324);
|
2014-06-05 17:05:06 -04:00
|
|
|
Assert.IsFalse(a.Equals(b + 1));
|
|
|
|
Assert.IsTrue(a.Equals(b - 1));
|
|
|
|
Assert.IsTrue((a + 1).Equals(b));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatEqualsSignedLimb()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
using (var a = new HugeFloat(Platform.Select("-222509832505", "-222509835")))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
var b = Platform.Si(-222509832504, -222509834);
|
2014-06-05 17:05:06 -04:00
|
|
|
Assert.IsFalse(a.Equals(b + 1));
|
|
|
|
Assert.IsTrue(a.Equals(b - 1));
|
|
|
|
Assert.IsTrue((a + 1).Equals(b));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-06-16 16:03:07 -04:00
|
|
|
public void FloatEqualsDouble()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-06-16 16:03:07 -04:00
|
|
|
using (var a = new HugeFloat("-222509832505"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-06-16 16:03:07 -04:00
|
|
|
double b = -222509832504;
|
|
|
|
Assert.IsFalse(a.Equals(b + 1));
|
|
|
|
Assert.IsTrue(a.Equals(b - 1));
|
|
|
|
Assert.IsTrue((a + 1).Equals(b));
|
|
|
|
Assert.IsFalse((a + 1).Equals(b + 0.1));
|
2014-06-05 17:05:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-06-16 16:03:07 -04:00
|
|
|
public void FloatEqualsHugeFloatApproximately()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
using (var a = HugeFloat.Allocate(128))
|
|
|
|
using (var b = HugeFloat.Allocate(128))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
a.SetTo("ABCDEF12948576AB49587.ACD34EFB345", 16);
|
|
|
|
b.SetTo("ABCDEF12948576AB49587.ACD34EFB245", 16);
|
|
|
|
|
2014-06-16 16:03:07 -04:00
|
|
|
Assert.IsTrue(a.Equals(b, 119));
|
|
|
|
Assert.IsFalse(a.Equals(b, 120));
|
|
|
|
Assert.IsTrue(a.Equals(b - 1, 83));
|
|
|
|
Assert.IsFalse(a.Equals(b - 1, 84));
|
|
|
|
Assert.IsTrue((a + 512).Equals(b, 74));
|
|
|
|
Assert.IsFalse((a + 512).Equals(b, 75));
|
|
|
|
|
|
|
|
//same mantissa, different exponent should always return false
|
|
|
|
Assert.IsFalse(a.Equals(a >> 1, 119));
|
|
|
|
Assert.IsFalse(a.Equals(a << 1, 119));
|
|
|
|
Assert.IsFalse(a.Equals(a * 2, 119));
|
|
|
|
Assert.IsFalse(a.Equals(a / 2, 119));
|
2014-06-05 17:05:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-06-16 16:03:07 -04:00
|
|
|
public void FloatRelDiff()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-06-16 16:03:07 -04:00
|
|
|
using (var a = new HugeFloat("1234523549876.24935230589472305894245"))
|
|
|
|
using (var b = new HugeFloat("1234523549541.45207354209357842979873"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-06-16 16:03:07 -04:00
|
|
|
Assert.AreEqual(a.RelativeDifferenceFrom(b), (a - b).Abs() / a);
|
|
|
|
Assert.AreNotEqual(a.RelativeDifferenceFrom(b), (a - b).Abs() / b);
|
|
|
|
Assert.AreEqual(b.RelativeDifferenceFrom(a), (a - b).Abs() / b);
|
|
|
|
Assert.AreNotEqual(b.RelativeDifferenceFrom(a), (a - b).Abs() / a);
|
2014-06-05 17:05:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Equality operators with expr
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatEqualsOperatorHugeFloat()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-06-16 16:03:07 -04:00
|
|
|
using (var a = new HugeFloat("-2225098325034502983451.29835740293845721"))
|
|
|
|
using (var b = new HugeFloat("2225098325034502983450.29835740293845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
Assert.IsFalse(b == a);
|
2014-06-16 16:03:07 -04:00
|
|
|
Assert.IsFalse(a == b + 1);
|
|
|
|
Assert.IsTrue(a + 1 == -b);
|
2014-06-05 17:05:06 -04:00
|
|
|
Assert.IsFalse(a == null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatNotEqualOperatorHugeFloat()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-06-16 16:03:07 -04:00
|
|
|
using (var a = new HugeFloat("-2225098325034502983451.29835740293845721"))
|
|
|
|
using (var b = new HugeFloat("2225098325034502983450.29835740293845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
Assert.IsTrue(b != a);
|
2014-06-16 16:03:07 -04:00
|
|
|
Assert.IsTrue(a != b + 1);
|
|
|
|
Assert.IsFalse(a + 1 != -b);
|
2014-06-05 17:05:06 -04:00
|
|
|
Assert.IsTrue(a != null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Equality operators with Limb
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatEqualsOperatorLimb()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var a = new HugeFloat("-835740293845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
ulong b = 835740293845720;
|
|
|
|
Assert.IsFalse(b == a);
|
|
|
|
Assert.IsFalse(a == b + 1);
|
|
|
|
Assert.IsTrue(-(a + 1) == b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatNotEqualOperatorLimb()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var a = new HugeFloat("-835740293845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
ulong b = 835740293845720;
|
|
|
|
Assert.IsTrue(b != a);
|
|
|
|
Assert.IsTrue(a != b + 1);
|
|
|
|
Assert.IsFalse(-(a + 1) != b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Equality operators with Signed Limb
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatEqualsOperatorSignedLimb()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var a = new HugeFloat("-835740293845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
long b = -835740293845720;
|
|
|
|
Assert.IsFalse(b == a);
|
|
|
|
Assert.IsFalse(a == b + 1);
|
|
|
|
Assert.IsTrue(a + 1 == b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatNotEqualOperatorSignedLimb()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var a = new HugeFloat("-835740293845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
long b = -835740293845720;
|
|
|
|
Assert.IsTrue(b != a);
|
|
|
|
Assert.IsTrue(a != b + 1);
|
|
|
|
Assert.IsFalse(a + 1 != b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Equality operators with Double
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatEqualsOperatorDouble()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var a = new HugeFloat("-835740293845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
double b = -835740293845720;
|
|
|
|
Assert.IsFalse(b == a);
|
|
|
|
Assert.IsFalse(a == b + 1);
|
|
|
|
Assert.IsTrue(a + 1 == b);
|
|
|
|
Assert.IsFalse(a + 1 == b + 0.1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatNotEqualOperatorDouble()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-11-23 22:34:20 -05:00
|
|
|
using (var a = new HugeFloat("-835740293845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
double b = -835740293845720;
|
|
|
|
Assert.IsTrue(b != a);
|
|
|
|
Assert.IsTrue(a != b + 1);
|
|
|
|
Assert.IsFalse(a + 1 != b);
|
|
|
|
Assert.IsTrue(a + 1 != b + 0.1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region GetHashCode
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatGetHashCodeTest()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-06-16 16:03:07 -04:00
|
|
|
using (var a = new HugeFloat("-2225098325034502983450298357.40293845721"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
Assert.AreNotEqual(0, a.GetHashCode());
|
|
|
|
Assert.AreEqual(a.GetHashCode(), (a + 0).GetHashCode());
|
|
|
|
Assert.AreNotEqual(a.GetHashCode(), (-a).GetHashCode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Sign
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-11-23 22:34:20 -05:00
|
|
|
public void FloatSign()
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
2014-06-16 16:03:07 -04:00
|
|
|
using (var a = new HugeFloat("-22250983250345029834.502983574029384572134354"))
|
2014-06-05 17:05:06 -04:00
|
|
|
{
|
|
|
|
Assert.AreEqual(-1, a.Sign());
|
|
|
|
Assert.AreEqual(1, (-a).Sign());
|
|
|
|
Assert.AreEqual(0, (a-a).Sign());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
//more tests coming here
|
|
|
|
}
|
|
|
|
}
|