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.
 
 
 

50 lines
1.1 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 "core.h"
  14. using namespace lol;
  15. int main(int argc, char **argv)
  16. {
  17. /* Approximate Pi using Machin's formula: 16*atan(1/5)-4*atan(1/239) */
  18. real sum = 0.0, x0 = 5.0, x1 = 239.0;
  19. real const m0 = -x0 * x0, m1 = -x1 * x1, r16 = 16.0, r4 = 4.0;
  20. /* Degree 240 is required for 512-bit mantissa precision */
  21. for (int i = 1; i < 240; i += 2)
  22. {
  23. sum += r16 / (x0 * (real)i) - r4 / (x1 * (real)i);
  24. x0 *= m0;
  25. x1 *= m1;
  26. }
  27. sum.print();
  28. /* Bonus: compute e for fun. */
  29. sum = 0.0;
  30. x0 = 1.0;
  31. for (int i = 1; i < 100; i++)
  32. {
  33. sum += fres(x0);
  34. x0 *= (real)i;
  35. }
  36. sum.print();
  37. return EXIT_SUCCESS;
  38. }