You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.3 KiB

  1. //
  2. // Lol Engine - Sample math program: compute Pi
  3. //
  4. // Copyright: (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cstdio>
  14. #include <lol/main.h>
  15. using std::printf;
  16. using std::sqrt;
  17. using lol::real;
  18. int main(int argc, char **argv)
  19. {
  20. UNUSED(argc, argv);
  21. printf("Pi: "); real::R_PI().print();
  22. printf("e: "); real::R_E().print();
  23. printf("ln(2): "); real::R_LN2().print();
  24. printf("sqrt(2): "); real::R_SQRT2().print();
  25. printf("sqrt(1/2): "); real::R_SQRT1_2().print();
  26. /* Gauss-Legendre computation of Pi -- doesn't work well at all,
  27. * probably because we use finite precision. */
  28. real a = 1.0, b = sqrt((real)0.5), t = 0.25, p = 1.0;
  29. for (int i = 0; i < 3; i++)
  30. {
  31. real a2 = (a + b) * (real)0.5;
  32. real b2 = sqrt(a * b);
  33. real tmp = a - a2;
  34. real t2 = t - p * tmp * tmp;
  35. real p2 = p + p;
  36. a = a2; b = b2; t = t2; p = p2;
  37. }
  38. real sum = a + b;
  39. sum = sum * sum / ((real)4 * t);
  40. sum.print();
  41. return EXIT_SUCCESS;
  42. }