Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

57 строки
1.5 KiB

  1. //
  2. // Lol Engine — Sample math program: compute Pi
  3. //
  4. // Copyright © 2005—2020 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. #include <lol/real>
  19. using lol::real;
  20. int main(int argc, char **argv)
  21. {
  22. UNUSED(argc, argv);
  23. std::cout << std::setprecision(150);
  24. std::cout << " 0: " << real::R_0() << '\n';
  25. std::cout << " 1: " << real::R_1() << '\n';
  26. std::cout << "sqrt(2): " << real::R_SQRT2() << '\n';
  27. std::cout << "sqrt(½): " << real::R_SQRT1_2() << '\n';
  28. std::cout << " ln(2): " << real::R_LN2() << '\n';
  29. std::cout << " e: " << real::R_E() << '\n';
  30. std::cout << " π: " << real::R_PI() << '\n';
  31. // Gauss-Legendre computation of Pi — six iterations are enough for 150 digits
  32. real a = 1.0, b = real::R_SQRT1_2(), t = 0.25, p = 1.0;
  33. for (int i = 0; i < 6; i++)
  34. {
  35. real tmp = (a - b) * (real)0.5;
  36. b = sqrt(a * b);
  37. a -= tmp;
  38. t -= p * tmp * tmp;
  39. p += p;
  40. }
  41. real sum = a + b;
  42. sum = sum * sum / ((real)4 * t);
  43. std::cout << " " << sum << '\n';
  44. return EXIT_SUCCESS;
  45. }