diff --git a/build.vc11/mpir.net/mpir.net-tests/mpir.net-tests.csproj b/build.vc11/mpir.net/mpir.net-tests/mpir.net-tests.csproj index 7c2b2c93..1f28ed08 100644 --- a/build.vc11/mpir.net/mpir.net-tests/mpir.net-tests.csproj +++ b/build.vc11/mpir.net/mpir.net-tests/mpir.net-tests.csproj @@ -115,6 +115,9 @@ HugeFloatTests\IO.cs + + HugeFloatTests\Math.cs + HugeIntTests\Arithmetic.cs diff --git a/mpir.net/mpir.net-tests/HugeFloatTests/Arithmetic.cs b/mpir.net/mpir.net-tests/HugeFloatTests/Arithmetic.cs index 3570f1f5..730ef81c 100644 --- a/mpir.net/mpir.net-tests/HugeFloatTests/Arithmetic.cs +++ b/mpir.net/mpir.net-tests/HugeFloatTests/Arithmetic.cs @@ -34,6 +34,7 @@ namespace MPIR.Tests.HugeFloatTests } var exponent = expected.IndexOf('.'); + if(exponent < 0) exponent = expected.Length; expected = expected.Replace(".", ""); var exponentStr = "@" + exponent; diff --git a/mpir.net/mpir.net-tests/HugeFloatTests/ExpressionTests.cs b/mpir.net/mpir.net-tests/HugeFloatTests/ExpressionTests.cs index d2c09b93..d1c9d3f5 100644 --- a/mpir.net/mpir.net-tests/HugeFloatTests/ExpressionTests.cs +++ b/mpir.net/mpir.net-tests/HugeFloatTests/ExpressionTests.cs @@ -60,6 +60,8 @@ namespace MPIR.Tests.HugeFloatTests VerifyPartialResult(r, expr, -35); expr = expr + b.SquareRoot() + HugeFloat.SquareRoot(25) + ((b - 2) ^ 3) - (-b).RelativeDifferenceFrom(a + 1); VerifyPartialResult(r, expr, -19); + expr = expr - (a / 4).Floor() + (b / 3).Ceiling() - (a / b).Truncate(); + VerifyPartialResult(r, expr, -12); MarkExpressionsUsed(allExpressions, expr); } diff --git a/mpir.net/mpir.net-tests/HugeFloatTests/Math.cs b/mpir.net/mpir.net-tests/HugeFloatTests/Math.cs new file mode 100644 index 00000000..d08c4dd6 --- /dev/null +++ b/mpir.net/mpir.net-tests/HugeFloatTests/Math.cs @@ -0,0 +1,66 @@ +/* +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 Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace MPIR.Tests.HugeFloatTests +{ + [TestClass] + public class MiscMath + { + [TestMethod] + public void FloatFloor() + { + using(var a = new HugeFloat("-9023409872309847509847.9385345098345")) + using(var b = new HugeFloat()) + { + b.Value = a.Floor(); + FloatAssert.AreEqual("-9023409872309847509848", b); + b.Value = (-a).Floor(); + FloatAssert.AreEqual("9023409872309847509847", b); + } + } + + [TestMethod] + public void FloatCeiling() + { + using(var a = new HugeFloat("-9023409872309847509847.9385345098345")) + using(var b = new HugeFloat()) + { + b.Value = a.Ceiling(); + FloatAssert.AreEqual("-9023409872309847509847", b); + b.Value = (-a).Ceiling(); + FloatAssert.AreEqual("9023409872309847509848", b); + } + } + + [TestMethod] + public void FloatTruncate() + { + using(var a = new HugeFloat("-9023409872309847509847.9385345098345")) + using(var b = new HugeFloat()) + { + b.Value = a.Truncate(); + FloatAssert.AreEqual("-9023409872309847509847", b); + b.Value = (-a).Truncate(); + FloatAssert.AreEqual("9023409872309847509847", b); + } + } + } +} diff --git a/mpir.net/mpir.net/HugeFloat.cpp b/mpir.net/mpir.net/HugeFloat.cpp index 4fcb2828..04c1fd80 100644 --- a/mpir.net/mpir.net/HugeFloat.cpp +++ b/mpir.net/mpir.net/HugeFloat.cpp @@ -288,6 +288,9 @@ namespace MPIR MAKE_UNARY_OPERATOR (MPEXPR_NAME, DEFINE, -, Negate, Flt) MAKE_VOID_FUNCTION (MPEXPR_NAME, DEFINE, Abs, Flt) + MAKE_VOID_FUNCTION (MPEXPR_NAME, DEFINE, Floor, Flt) + MAKE_VOID_FUNCTION (MPEXPR_NAME, DEFINE, Ceiling, Flt) + MAKE_VOID_FUNCTION (MPEXPR_NAME, DEFINE, Truncate, Flt) MAKE_VOID_FUNCTION (MPEXPR_NAME, DEFINE, SquareRoot, Flt) MAKE_FUNCTION_WITH_ONE (MPEXPR_NAME, DEFINE, RelativeDifferenceFrom, Flt) @@ -299,6 +302,9 @@ namespace MPIR DEFINE_UNARY_ASSIGNMENT_REF(Negate, Flt, MP(neg)) DEFINE_UNARY_ASSIGNMENT_REF(Abs, Flt, MP(abs)) + DEFINE_UNARY_ASSIGNMENT_REF(Floor, Flt, MP(floor)) + DEFINE_UNARY_ASSIGNMENT_REF(Ceiling, Flt, MP(ceil)) + DEFINE_UNARY_ASSIGNMENT_REF(Truncate, Flt, MP(trunc)) DEFINE_UNARY_ASSIGNMENT_REF(SquareRoot, Flt, MP(sqrt)) DEFINE_UNARY_ASSIGNMENT_VAL(SquareRoot, Ui, MP(sqrt_ui)) diff --git a/mpir.net/mpir.net/HugeFloat.h b/mpir.net/mpir.net/HugeFloat.h index b1832142..d703b314 100644 --- a/mpir.net/mpir.net/HugeFloat.h +++ b/mpir.net/mpir.net/HugeFloat.h @@ -293,6 +293,10 @@ namespace MPIR /// An expression object that, when assigned to the Value property or consumed by a primitive-returning method, computes the requested operation static MPEXPR_NAME^ operator ^ (MPEXPR_NAME^ a, mpir_ui power); + #pragma endregion + + #pragma region Math + /// Computes the absolute value of the source number. /// As with all expressions, the result is not computed until the expression is assigned to the Value property or consumed by a method. /// @@ -305,6 +309,24 @@ namespace MPIR /// An expression object that, when assigned to the Value property or consumed by a primitive-returning method, computes the requested operation MPEXPR_NAME^ SquareRoot(); + /// Rounds the source number down to the next integer. + /// As with all expressions, the result is not computed until the expression is assigned to the Value property or consumed by a method. + /// + /// An expression object that, when assigned to the Value property or consumed by a primitive-returning method, computes the requested operation + MPEXPR_NAME^ Floor(); + + /// Rounds the source number up to the next integer. + /// As with all expressions, the result is not computed until the expression is assigned to the Value property or consumed by a method. + /// + /// An expression object that, when assigned to the Value property or consumed by a primitive-returning method, computes the requested operation + MPEXPR_NAME^ Ceiling(); + + /// Rounds the source number to the next integer toward zero. + /// As with all expressions, the result is not computed until the expression is assigned to the Value property or consumed by a method. + /// + /// An expression object that, when assigned to the Value property or consumed by a primitive-returning method, computes the requested operation + MPEXPR_NAME^ Truncate(); + #pragma endregion #pragma region Comparisons @@ -741,6 +763,10 @@ namespace MPIR DEFINE_BINARY_EXPRESSION_WITH_TWO (MPEXPR_NAME, RelativeDifferenceFrom, Flt) + DEFINE_UNARY_EXPRESSION_WITH_ONE (MPEXPR_NAME, Floor, Flt) + DEFINE_UNARY_EXPRESSION_WITH_ONE (MPEXPR_NAME, Ceiling, Flt) + DEFINE_UNARY_EXPRESSION_WITH_ONE (MPEXPR_NAME, Truncate, Flt) + #pragma endregion #pragma region HugeFloat class