From 0f63a2b6dec4ff3dc4c251e655e1e886a092af98 Mon Sep 17 00:00:00 2001 From: G Yuvan Shankar Date: Sat, 25 Dec 2021 19:32:08 +0530 Subject: [PATCH 1/2] Changed initial guess of Newtons Square Root Method --- include/real/real_math.hpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/include/real/real_math.hpp b/include/real/real_math.hpp index 15012a7..0811fc1 100755 --- a/include/real/real_math.hpp +++ b/include/real/real_math.hpp @@ -201,8 +201,17 @@ namespace boost{ return literals::zero_exact; } - // initial guess - exact_number result(x.digits, (x.exponent + 1)/2, true); + // initial guess using scalar estimate + exact_number result; + if(x.exponent%2==0) + { + if(x.digits[0]>=1) + result=exact_number (std::vector {6}, (x.exponent)/2, true); + else + result=exact_number (std::vector {2}, (x.exponent)/2, true); + } + else + result=exact_number(std::vector {2}, (x.exponent-1)/2, true); exact_number error; exact_number max_error(std::vector {1}, -max_error_exponent, true); From 2e0d4b4a13a5c970dbc3876ec575ff07c0a50920 Mon Sep 17 00:00:00 2001 From: G Yuvan Shankar Date: Fri, 7 Jan 2022 13:42:12 +0530 Subject: [PATCH 2/2] Removed loop and replaced with simple multiplication --- include/real/real_math.hpp | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/include/real/real_math.hpp b/include/real/real_math.hpp index 0811fc1..314a6c2 100755 --- a/include/real/real_math.hpp +++ b/include/real/real_math.hpp @@ -201,17 +201,8 @@ namespace boost{ return literals::zero_exact; } - // initial guess using scalar estimate - exact_number result; - if(x.exponent%2==0) - { - if(x.digits[0]>=1) - result=exact_number (std::vector {6}, (x.exponent)/2, true); - else - result=exact_number (std::vector {2}, (x.exponent)/2, true); - } - else - result=exact_number(std::vector {2}, (x.exponent-1)/2, true); + // initial guess + exact_number result(x.digits, (x.exponent + 1)/2, true); exact_number error; exact_number max_error(std::vector {1}, -max_error_exponent, true); @@ -369,9 +360,7 @@ namespace boost{ else result -= cur_term; - for(exact_number i = (two * term_number) + literals::one_exact ; i <= two * (term_number + literals::one_exact); i = i + literals::one_exact){ - factorial *= i; - } + factorial *=((two * term_number) + literals::one_exact)*(two * (term_number + literals::one_exact)); cur_power *= square_x; cur_term = cur_power; ++ term_number_int;