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

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

test: add a 10-line example program that computes Pi to the 150th digit.

  • Property svn:keywords set to Id
File size: 953 bytes
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    return EXIT_SUCCESS;
36}
37
Note: See TracBrowser for help on using the repository browser.