diff --git a/mpir.net/mpir.net-tests/HugeFloatTests/Conversions.cs b/mpir.net/mpir.net-tests/HugeFloatTests/Conversions.cs index fdbd41a8..b1ea338e 100644 --- a/mpir.net/mpir.net-tests/HugeFloatTests/Conversions.cs +++ b/mpir.net/mpir.net-tests/HugeFloatTests/Conversions.cs @@ -26,6 +26,14 @@ namespace MPIR.Tests.HugeFloatTests [TestClass] public class Conversions { + private static HugeFloat AlmostOne; + + [ClassInitialize] + public static void Init(TestContext context) + { + AlmostOne = new HugeFloat(0.99999); + } + [TestMethod] public void FloatToStringDecimal() { @@ -200,5 +208,155 @@ namespace MPIR.Tests.HugeFloatTests } } } + + [TestMethod] + public void FloatFitsLong() + { + using (var a = new HugeFloat(long.MaxValue)) + using (var b = new HugeFloat()) + { + Assert.IsTrue(a.FitsLong()); + + b.Value = a + AlmostOne; + Assert.IsTrue(b.FitsLong()); + + b.Value = a + 1; + Assert.IsFalse(b.FitsLong()); + + a.SetTo(long.MinValue); + Assert.IsTrue(a.FitsLong()); + + b.Value = a - AlmostOne; + Assert.IsTrue(b.FitsLong()); + + b.Value = a - 1; + Assert.IsFalse(b.FitsLong()); + } + } + + [TestMethod] + public void FloatFitsUlong() + { + using(var a = new HugeFloat(ulong.MaxValue)) + using(var b = new HugeFloat()) + { + Assert.IsTrue(a.FitsUlong()); + + b.Value = a + AlmostOne; + Assert.IsTrue(b.FitsUlong()); + + b.Value = a + 1; + Assert.IsFalse(b.FitsUlong()); + + a.SetTo(ulong.MinValue); + Assert.IsTrue(a.FitsUlong()); + + b.Value = a - AlmostOne; + Assert.IsTrue(b.FitsUlong()); + + b.Value = a - 1; + Assert.IsFalse(b.FitsUlong()); + } + } + + [TestMethod] + public void FloatFitsInt() + { + using(var a = new HugeFloat(int.MaxValue)) + using(var b = new HugeFloat()) + { + Assert.IsTrue(a.FitsInt()); + + b.Value = a + AlmostOne; + Assert.IsTrue(b.FitsInt()); + + b.Value = a + 1; + Assert.IsFalse(b.FitsInt()); + + a.SetTo(int.MinValue); + Assert.IsTrue(a.FitsInt()); + + b.Value = a - AlmostOne; + Assert.IsTrue(b.FitsInt()); + + b.Value = a - 1; + Assert.IsFalse(b.FitsInt()); + } + } + + [TestMethod] + public void FloatFitsUint() + { + using(var a = new HugeFloat(uint.MaxValue)) + using(var b = new HugeFloat()) + { + Assert.IsTrue(a.FitsUint()); + + b.Value = a + AlmostOne; + Assert.IsTrue(b.FitsUint()); + + b.Value = a + 1; + Assert.IsFalse(b.FitsUint()); + + a.SetTo(uint.MinValue); + Assert.IsTrue(a.FitsUint()); + + b.Value = a - AlmostOne; + Assert.IsTrue(b.FitsUint()); + + b.Value = a - 1; + Assert.IsFalse(b.FitsUint()); + } + } + + [TestMethod] + public void FloatFitsShort() + { + using(var a = new HugeFloat(short.MaxValue)) + using(var b = new HugeFloat()) + { + Assert.IsTrue(a.FitsShort()); + + b.Value = a + AlmostOne; + Assert.IsTrue(b.FitsShort()); + + b.Value = a + 1; + Assert.IsFalse(b.FitsShort()); + + a.SetTo(short.MinValue); + Assert.IsTrue(a.FitsShort()); + + b.Value = a - AlmostOne; + Assert.IsTrue(b.FitsShort()); + + b.Value = a - 1; + Assert.IsFalse(b.FitsShort()); + } + } + + [TestMethod] + public void FloatFitsUshort() + { + using(var a = new HugeFloat(ushort.MaxValue)) + using(var b = new HugeFloat()) + { + Assert.IsTrue(a.FitsUshort()); + + b.Value = a + AlmostOne; + Assert.IsTrue(b.FitsUshort()); + + b.Value = a + 1; + Assert.IsFalse(b.FitsUshort()); + + a.SetTo(ushort.MinValue); + Assert.IsTrue(a.FitsUshort()); + + b.Value = a - AlmostOne; + Assert.IsTrue(b.FitsUshort()); + + b.Value = a - 1; + Assert.IsFalse(b.FitsUshort()); + } + } } } diff --git a/mpir.net/mpir.net/HugeFloat.h b/mpir.net/mpir.net/HugeFloat.h index d703b314..0611a009 100644 --- a/mpir.net/mpir.net/HugeFloat.h +++ b/mpir.net/mpir.net/HugeFloat.h @@ -1187,6 +1187,42 @@ namespace MPIR _allocatedPrecision = precision; } + /// + /// Returns true if the value of the source number, if truncated to an integer, is in the long range. + /// + /// true if the value will fit in a long + bool FitsLong() { return MP(fits_si_p)(_value) != 0; } + + /// + /// Returns true if the value of the source number, if truncated to an integer, is in the ulong range. + /// + /// true if the value will fit in a long + bool FitsUlong() { return MP(fits_ui_p)(_value) != 0; } + + /// + /// Returns true if the value of the source number, if truncated to an integer, is in the int range. + /// + /// true if the value will fit in an int + bool FitsInt() { return MP(fits_sint_p)(_value) != 0; } + + /// + /// Returns true if the value of the source number, if truncated to an integer, is in the uint range. + /// + /// true if the value will fit in an int + bool FitsUint() { return MP(fits_uint_p)(_value) != 0; } + + /// + /// Returns true if the value of the source number, if truncated to an integer, is in the short range. + /// + /// true if the value will fit in a short + bool FitsShort() { return MP(fits_sshort_p)(_value) != 0; } + + /// + /// Returns true if the value of the source number, if truncated to an integer, is in the ushort range. + /// + /// true if the value will fit in a short + bool FitsUshort() { return MP(fits_ushort_p)(_value) != 0; } + #pragma endregion #pragma region IO