# source:trunk/test/math/pi.cpp@1050Tweet

Last change on this file since 1050 was 1050, checked in by sam, 11 years ago

test: fix OS X compilation; we still need SDLmain.a on that platform.

• Property svn:keywords set to `Id`
File size: 1.3 KB
Line
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
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cstdio>
16
17#if USE_SDL && defined __APPLE__
18#   include <SDL_main.h>
19#endif
20
21#include "core.h"
22
23using namespace lol;
24using namespace std;
25
26int main(int argc, char **argv)
27{
28    printf("Pi: "); real::R_PI.print();
29    printf("e: "); real::R_E.print();
30    printf("ln(2): "); real::R_LN2.print();
31    printf("sqrt(2): "); real::R_SQRT2.print();
32    printf("sqrt(1/2): "); real::R_SQRT1_2.print();
33
34    /* Gauss-Legendre computation of Pi -- doesn't work well at all,
35     * probably because we use finite precision. */
36    real a = 1.0, b = sqrt((real)0.5), t = 0.25, p = 1.0;
37
38    for (int i = 0; i < 3; i++)
39    {
40        real a2 = (a + b) * (real)0.5;
41        real b2 = sqrt(a * b);
42        real tmp = a - a2;
43        real t2 = t - p * tmp * tmp;
44        real p2 = p + p;
45        a = a2; b = b2; t = t2; p = p2;
46    }
47
48    real sum = a + b;
49    sum = sum * sum / ((real)4 * t);
50    sum.print();
51
52    return EXIT_SUCCESS;
53}
54
Note: See TracBrowser for help on using the repository browser.