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

51 行
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://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cstdio>
  14. #include "core.h"
  15. using namespace lol;
  16. using namespace std;
  17. int main(int argc, char **argv)
  18. {
  19. printf("Pi: "); real::R_PI.print();
  20. printf("e: "); real::R_E.print();
  21. printf("ln(2): "); real::R_LN2.print();
  22. printf("sqrt(2): "); real::R_SQRT2.print();
  23. printf("sqrt(1/2): "); real::R_SQRT1_2.print();
  24. /* Gauss-Legendre computation of Pi -- doesn't work well at all,
  25. * probably because we use finite precision. */
  26. real a = 1.0, b = sqrt((real)0.5), t = 0.25, p = 1.0;
  27. for (int i = 0; i < 3; i++)
  28. {
  29. real a2 = (a + b) * (real)0.5;
  30. real b2 = sqrt(a * b);
  31. real tmp = a - a2;
  32. real t2 = t - p * tmp * tmp;
  33. real p2 = p + p;
  34. a = a2; b = b2; t = t2; p = p2;
  35. }
  36. real sum = a + b;
  37. sum = sum * sum / ((real)4 * t);
  38. sum.print();
  39. return EXIT_SUCCESS;
  40. }