瀏覽代碼

math: implement <<(ostream &, real).

legacy
Sam Hocevar 5 年之前
父節點
當前提交
b5de2bd6e6
共有 3 個文件被更改,包括 21 次插入9 次删除
  1. +10
    -8
      doc/samples/math/pi.cpp
  2. +3
    -0
      src/lol/math/real.h
  3. +8
    -1
      src/math/real.cpp

+ 10
- 8
doc/samples/math/pi.cpp 查看文件

@@ -15,6 +15,7 @@
#endif

#include <iostream>
#include <iomanip>

#include <lol/engine.h>

@@ -24,13 +25,14 @@ int main(int argc, char **argv)
{
UNUSED(argc, argv);

std::cout << " 0: " << real::R_0().str() << '\n';
std::cout << " 1: " << real::R_1().str() << '\n';
std::cout << "sqrt(2): " << real::R_SQRT2().str() << '\n';
std::cout << "sqrt(½): " << real::R_SQRT1_2().str() << '\n';
std::cout << " ln(2): " << real::R_LN2().str() << '\n';
std::cout << " e: " << real::R_E().str() << '\n';
std::cout << " π: " << real::R_PI().str() << '\n';
std::cout << std::setprecision(150);
std::cout << " 0: " << real::R_0() << '\n';
std::cout << " 1: " << real::R_1() << '\n';
std::cout << "sqrt(2): " << real::R_SQRT2() << '\n';
std::cout << "sqrt(½): " << real::R_SQRT1_2() << '\n';
std::cout << " ln(2): " << real::R_LN2() << '\n';
std::cout << " e: " << real::R_E() << '\n';
std::cout << " π: " << real::R_PI() << '\n';

// Gauss-Legendre computation of Pi — six iterations are enough for 150 digits
real a = 1.0, b = real::R_SQRT1_2(), t = 0.25, p = 1.0;
@@ -46,7 +48,7 @@ int main(int argc, char **argv)

real sum = a + b;
sum = sum * sum / ((real)4 * t);
std::cout << " " << sum.str() << '\n';
std::cout << " " << sum << '\n';

return EXIT_SUCCESS;
}


+ 3
- 0
src/lol/math/real.h 查看文件

@@ -235,6 +235,9 @@ public:
inline int total_bits() const { return bigit_count() * bigit_bits(); }
};

template<typename U>
std::ostream& operator <<(std::ostream &s, Real<U> const &x);

/*
* Mandatory forward declarations of template specialisations
*/


+ 8
- 1
src/math/real.cpp 查看文件

@@ -17,7 +17,6 @@
#include <sstream>
#include <iomanip>
#include <cstring>
#include <cstdio>
#include <cstdlib>

namespace lol
@@ -1563,6 +1562,14 @@ template<> real peaks(real const &x, real const &y)
return ret;
}

template<>
std::ostream& operator <<(std::ostream &s, real const &x)
{
bool hex = (s.flags() & std::ios_base::basefield) == std::ios_base::hex;
s << (hex ? x.xstr() : x.str((int)s.precision()));
return s;
}

template<> std::string real::str(int ndigits) const
{
std::stringstream ss;


Loading…
取消
儲存