選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

56 行
1.5 KiB

  1. //
  2. // Lol Engine — Sample math program: compute Pi
  3. //
  4. // Copyright © 2005—2019 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // This program is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #if HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include <iostream>
  16. #include <iomanip>
  17. #include <lol/engine.h>
  18. using lol::real;
  19. int main(int argc, char **argv)
  20. {
  21. UNUSED(argc, argv);
  22. std::cout << std::setprecision(150);
  23. std::cout << " 0: " << real::R_0() << '\n';
  24. std::cout << " 1: " << real::R_1() << '\n';
  25. std::cout << "sqrt(2): " << real::R_SQRT2() << '\n';
  26. std::cout << "sqrt(½): " << real::R_SQRT1_2() << '\n';
  27. std::cout << " ln(2): " << real::R_LN2() << '\n';
  28. std::cout << " e: " << real::R_E() << '\n';
  29. std::cout << " π: " << real::R_PI() << '\n';
  30. // Gauss-Legendre computation of Pi — six iterations are enough for 150 digits
  31. real a = 1.0, b = real::R_SQRT1_2(), t = 0.25, p = 1.0;
  32. for (int i = 0; i < 6; i++)
  33. {
  34. real tmp = (a - b) * (real)0.5;
  35. b = sqrt(a * b);
  36. a -= tmp;
  37. t -= p * tmp * tmp;
  38. p += p;
  39. }
  40. real sum = a + b;
  41. sum = sum * sum / ((real)4 * t);
  42. std::cout << " " << sum << '\n';
  43. return EXIT_SUCCESS;
  44. }