54 строки
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 <lol/engine.h>
  17. using lol::real;
  18. int main(int argc, char **argv)
  19. {
  20. UNUSED(argc, argv);
  21. std::cout << " 0: " << real::R_0().str() << '\n';
  22. std::cout << " 1: " << real::R_1().str() << '\n';
  23. std::cout << "sqrt(2): " << real::R_SQRT2().str() << '\n';
  24. std::cout << "sqrt(½): " << real::R_SQRT1_2().str() << '\n';
  25. std::cout << " ln(2): " << real::R_LN2().str() << '\n';
  26. std::cout << " e: " << real::R_E().str() << '\n';
  27. std::cout << " π: " << real::R_PI().str() << '\n';
  28. // Gauss-Legendre computation of Pi — six iterations are enough for 150 digits
  29. real a = 1.0, b = real::R_SQRT1_2(), t = 0.25, p = 1.0;
  30. for (int i = 0; i < 6; i++)
  31. {
  32. real tmp = (a - b) * (real)0.5;
  33. b = sqrt(a * b);
  34. a -= tmp;
  35. t -= p * tmp * tmp;
  36. p += p;
  37. }
  38. real sum = a + b;
  39. sum = sum * sum / ((real)4 * t);
  40. std::cout << " " << sum.str() << '\n';
  41. return EXIT_SUCCESS;
  42. }