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

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

test: minor updates to the Pi program (now almost deprecated) and the
Remez exchange program.

• Property svn:keywords set to `Id`
File size: 1.2 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 "core.h"
16
17using namespace lol;
18
19int main(int argc, char **argv)
20{
21    printf("Pi: "); real::R_PI.print();
22    printf("e: "); real::R_E.print();
23    printf("ln(2): "); real::R_LN2.print();
24    printf("sqrt(2): "); real::R_SQRT2.print();
25    printf("sqrt(1/2): "); real::R_SQRT1_2.print();
26
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
31    for (int i = 0; i < 3; i++)
32    {
33        real a2 = (a + b) * (real)0.5;
34        real b2 = sqrt(a * b);
35        real tmp = a - a2;
36        real t2 = t - p * tmp * tmp;
37        real p2 = p + p;
38        a = a2; b = b2; t = t2; p = p2;
39    }
40
41    real sum = a + b;
42    sum = sum * sum / ((real)4 * t);
43    sum.print();
44
45    return EXIT_SUCCESS;
46}
47
Note: See TracBrowser for help on using the repository browser.