From 7b4a394e738824d9cbe6a60b207e2b202a0c009d Mon Sep 17 00:00:00 2001 From: kimwalisch Date: Sat, 10 Feb 2024 14:17:29 +0100 Subject: [PATCH] Increase RiemannR(x) precision --- src/app/main.cpp | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/src/app/main.cpp b/src/app/main.cpp index 16eb9f3f0..168b5ff8e 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -14,10 +14,10 @@ #include "cmdoptions.hpp" #include -#include #include #include #include +#include #include using namespace primesieve; @@ -140,15 +140,41 @@ int main(int argc, char* argv[]) nthPrime(opt); else if (opt.RiemannR) { - long double res = RiemannR((long double) opt.numbers[0]); - int precision = (res != std::floor(res)) ? 3 : 0; - std::cout << std::fixed << std::setprecision(precision) << res << std::endl; + std::ostringstream oss; + oss << std::fixed << std::setprecision(10) << RiemannR((long double) opt.numbers[0]); + std::string res = oss.str(); + + // Remove trailing 0 decimal digits + if (res.find('.') != std::string::npos) + { + std::reverse(res.begin(), res.end()); + res = res.substr(res.find_first_not_of('0')); + if (res.at(0) == '.') + res = res.substr(1); + + std::reverse(res.begin(), res.end()); + } + + std::cout << res << std::endl; } else if (opt.RiemannR_inverse) { - long double res = RiemannR_inverse((long double) opt.numbers[0]); - int precision = (res != std::floor(res)) ? 3 : 0; - std::cout << std::fixed << std::setprecision(precision) << res << std::endl; + std::ostringstream oss; + oss << std::fixed << std::setprecision(10) << RiemannR_inverse((long double) opt.numbers[0]); + std::string res = oss.str(); + + // Remove trailing 0 decimal digits + if (res.find('.') != std::string::npos) + { + std::reverse(res.begin(), res.end()); + res = res.substr(res.find_first_not_of('0')); + if (res.at(0) == '.') + res = res.substr(1); + + std::reverse(res.begin(), res.end()); + } + + std::cout << res << std::endl; } else sieve(opt);