From abb2403cca91643d7758143d5708af31fae81460 Mon Sep 17 00:00:00 2001 From: Alex Dyachenko Date: Fri, 13 Jun 2014 13:54:43 -0400 Subject: [PATCH] Float square root. MPIR 2.7.0 section 7.5 completed. --- .../HugeFloatTests/Arithmetic.cs | 24 +++++++++++++++++++ mpir.net/mpir.net/HugeFloat.cpp | 3 +++ mpir.net/mpir.net/HugeFloat.h | 18 ++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/build.vc11/mpir.net/mpir.net-tests/HugeFloatTests/Arithmetic.cs b/build.vc11/mpir.net/mpir.net-tests/HugeFloatTests/Arithmetic.cs index ec308aac..3570f1f5 100644 --- a/build.vc11/mpir.net/mpir.net-tests/HugeFloatTests/Arithmetic.cs +++ b/build.vc11/mpir.net/mpir.net-tests/HugeFloatTests/Arithmetic.cs @@ -277,6 +277,30 @@ namespace MPIR.Tests.HugeFloatTests #endregion + #region Sqrt + + [TestMethod] + public void FloatSqrt() + { + using(var a = new HugeFloat("9023404035982394058230584.9434590783455098345")) + { + a.Value = a.SquareRoot(); + FloatAssert.AreEqual("3003898140081.0504277325488426221152130989784856425363142756", a); + } + } + + [TestMethod] + public void FloatSqrtLimb() + { + using(var a = new HugeFloat()) + { + a.Value = HugeFloat.SquareRoot(2540928740928357403UL); + FloatAssert.AreEqual("1594029090.3645257511790832426801336140951314995369452070983", a); + } + } + + #endregion + #region Divide #region Int diff --git a/mpir.net/mpir.net/HugeFloat.cpp b/mpir.net/mpir.net/HugeFloat.cpp index b0ef3306..dbc3453d 100644 --- a/mpir.net/mpir.net/HugeFloat.cpp +++ b/mpir.net/mpir.net/HugeFloat.cpp @@ -265,6 +265,7 @@ namespace MPIR MAKE_UNARY_OPERATOR (MPEXPR_NAME, DEFINE, -, Negate, Flt) MAKE_VOID_FUNCTION (MPEXPR_NAME, DEFINE, Abs, Flt) + MAKE_VOID_FUNCTION (MPEXPR_NAME, DEFINE, SquareRoot, Flt) MAKE_BINARY_OPERATOR_STANDARD (MPEXPR_NAME, DEFINE, /, Divide, Flt, Flt) MAKE_BINARY_OPERATOR_RLIMB (MPEXPR_NAME, DEFINE, /, Divide, Flt, Ui) @@ -274,6 +275,8 @@ namespace MPIR DEFINE_UNARY_ASSIGNMENT_REF(Negate, Flt, MP(neg)) DEFINE_UNARY_ASSIGNMENT_REF(Abs, Flt, MP(abs)) + DEFINE_UNARY_ASSIGNMENT_REF(SquareRoot, Flt, MP(sqrt)) + DEFINE_UNARY_ASSIGNMENT_VAL(SquareRoot, Ui, MP(sqrt_ui)) DEFINE_BINARY_ASSIGNMENT_REF_REF(Add, Flt, MP(add)) DEFINE_BINARY_ASSIGNMENT_REF_VAL(Add, Flt, Ui, MP(add_ui)) diff --git a/mpir.net/mpir.net/HugeFloat.h b/mpir.net/mpir.net/HugeFloat.h index 2ead99c4..c601e1fe 100644 --- a/mpir.net/mpir.net/HugeFloat.h +++ b/mpir.net/mpir.net/HugeFloat.h @@ -298,6 +298,12 @@ 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^ Abs(); + /// Computes the square root 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. + /// + /// An expression object that, when assigned to the Value property or consumed by a primitive-returning method, computes the requested operation + MPEXPR_NAME^ SquareRoot(); + #pragma endregion #pragma region Comparisons @@ -713,6 +719,8 @@ namespace MPIR DEFINE_UNARY_EXPRESSION_WITH_ONE (MPEXPR_NAME, Negate, Flt) DEFINE_UNARY_EXPRESSION_WITH_ONE (MPEXPR_NAME, Abs, Flt) + DEFINE_UNARY_EXPRESSION_WITH_ONE (MPEXPR_NAME, SquareRoot, Flt) + DEFINE_UNARY_EXPRESSION_WITH_BUILT_INS_ONLY (MPEXPR_NAME, SquareRoot, Ui) #pragma endregion @@ -1239,6 +1247,16 @@ namespace MPIR // size_t GetLimb(mp_size_t index) { return MP(getlimbn)(_value, index); } #pragma endregion + + #pragma region Arithmetic + + /// Computes the square root 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. + /// + /// An expression object that, when assigned to the Value property or consumed by a primitive-returning method, computes the requested operation + static MPEXPR_NAME^ SquareRoot(mpir_ui a) { return gcnew MPEXPR(SquareRootUi)(a); } + + #pragma endregion }; #pragma endregion