Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 std::printf;
  16. using std::sqrt;
  17. using lol::real;
  18. int main(int argc, char **argv)
  19. {
  20. printf("Pi: "); real::R_PI.print();
  21. printf("e: "); real::R_E.print();
  22. printf("ln(2): "); real::R_LN2.print();
  23. printf("sqrt(2): "); real::R_SQRT2.print();
  24. printf("sqrt(1/2): "); real::R_SQRT1_2.print();
  25. /* Gauss-Legendre computation of Pi -- doesn't work well at all,
  26. * probably because we use finite precision. */
  27. real a = 1.0, b = sqrt((real)0.5), t = 0.25, p = 1.0;
  28. for (int i = 0; i < 3; i++)
  29. {
  30. real a2 = (a + b) * (real)0.5;
  31. real b2 = sqrt(a * b);
  32. real tmp = a - a2;
  33. real t2 = t - p * tmp * tmp;
  34. real p2 = p + p;
  35. a = a2; b = b2; t = t2; p = p2;
  36. }
  37. real sum = a + b;
  38. sum = sum * sum / ((real)4 * t);
  39. sum.print();
  40. return EXIT_SUCCESS;
  41. }