Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

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