source: trunk/test/math/pi.cpp @ 989

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

test: more work on the Remez exchange algorithm.

  • Property svn:keywords set to Id
File size: 1.1 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
7//   Public License, Version 2, as published by Sam Hocevar. See
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    /* Approximate Pi using Machin's formula: 16*atan(1/5)-4*atan(1/239) */
22    real sum = 0.0, x0 = 5.0, x1 = 239.0;
23    real const m0 = -x0 * x0, m1 = -x1 * x1, r16 = 16.0, r4 = 4.0;
24
25    /* Degree 240 is required for 512-bit mantissa precision */
26    for (int i = 1; i < 240; i += 2)
27    {
28        sum += r16 / (x0 * (real)i) - r4 / (x1 * (real)i);
29        x0 *= m0;
30        x1 *= m1;
31    }
32
33    sum.print();
34
35    /* Bonus: compute e for fun. */
36    sum = 0.0;
37    x0 = 1.0;
38
39    for (int i = 1; i < 100; i++)
40    {
41        sum += fres(x0);
42        x0 *= (real)i;
43    }
44
45    sum.print();
46
47    return EXIT_SUCCESS;
48}
49
Note: See TracBrowser for help on using the repository browser.