From 8f400b63641a7be7c0c8da6f47578a0bdd33e109 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 28 Sep 2011 17:04:52 +0000 Subject: [PATCH] core: create global real number variables such as R_E, R_PI, R_LN2 etc. --- src/real.cpp | 31 +++++++++++++++++++++++++++++++ src/real.h | 14 ++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/real.cpp b/src/real.cpp index f6adb3f8..8d797000 100644 --- a/src/real.cpp +++ b/src/real.cpp @@ -711,5 +711,36 @@ void real::print(int ndigits) const printf("\n"); } +static real fast_pi() +{ + /* Approximate Pi using Machin's formula: 16*atan(1/5)-4*atan(1/239) */ + real ret = 0.0, x0 = 5.0, x1 = 239.0; + real const m0 = -x0 * x0, m1 = -x1 * x1, r16 = 16.0, r4 = 4.0; + + /* Degree 240 is required for 512-bit mantissa precision */ + for (int i = 1; i < 240; i += 2) + { + ret += r16 / (x0 * (real)i) - r4 / (x1 * (real)i); + x0 *= m0; + x1 *= m1; + } + + return ret; +} + +real const real::R_E = exp((real)1.0); +real const real::R_LOG2E = (real)1.0 / R_LN2; +real const real::R_LOG10E = (real)1.0 / R_LN10; +real const real::R_LN2 = log((real)2.0); +real const real::R_LN10 = log((real)10.0); +real const real::R_PI = fast_pi(); +real const real::R_PI_2 = R_PI >> 1; +real const real::R_PI_4 = R_PI >> 2; +real const real::R_1_PI = (real)1.0 / R_PI; +real const real::R_2_PI = R_1_PI << 1; +real const real::R_2_SQRTPI = (real)2.0 / sqrt(R_PI); +real const real::R_SQRT2 = sqrt((real)2.0); +real const real::R_SQRT1_2 = R_SQRT2 >> 1; + } /* namespace lol */ diff --git a/src/real.h b/src/real.h index bf9ca60e..42a53b18 100644 --- a/src/real.h +++ b/src/real.h @@ -70,6 +70,20 @@ public: void print(int ndigits = 150) const; + static real const R_E; + static real const R_LOG2E; + static real const R_LOG10E; + static real const R_LN2; + static real const R_LN10; + static real const R_PI; + static real const R_PI_2; + static real const R_PI_4; + static real const R_1_PI; + static real const R_2_PI; + static real const R_2_SQRTPI; + static real const R_SQRT2; + static real const R_SQRT1_2; + private: /* XXX: changing this requires tuning real::fres (the number of * Newton-Raphson iterations) and real::print (the number of printed