Last change
on this file since 1008 was
1008,
checked in by sam, 11 years ago
|
test: add missing <cstdio> include in pi test program.
|
-
Property svn:keywords set to
Id
|
File size:
1.3 KB
|
Rev | Line | |
---|
[984] | 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 | |
---|
[1008] | 15 | #include <cstdio> |
---|
| 16 | |
---|
[984] | 17 | #include "core.h" |
---|
| 18 | |
---|
| 19 | using namespace lol; |
---|
[1008] | 20 | using namespace std; |
---|
[984] | 21 | |
---|
| 22 | int main(int argc, char **argv) |
---|
| 23 | { |
---|
[996] | 24 | printf("Pi: "); real::R_PI.print(); |
---|
| 25 | printf("e: "); real::R_E.print(); |
---|
| 26 | printf("ln(2): "); real::R_LN2.print(); |
---|
| 27 | printf("sqrt(2): "); real::R_SQRT2.print(); |
---|
| 28 | printf("sqrt(1/2): "); real::R_SQRT1_2.print(); |
---|
[984] | 29 | |
---|
[996] | 30 | /* Gauss-Legendre computation of Pi -- doesn't work well at all, |
---|
| 31 | * probably because we use finite precision. */ |
---|
| 32 | real a = 1.0, b = sqrt((real)0.5), t = 0.25, p = 1.0; |
---|
[984] | 33 | |
---|
[996] | 34 | for (int i = 0; i < 3; i++) |
---|
[989] | 35 | { |
---|
[996] | 36 | real a2 = (a + b) * (real)0.5; |
---|
| 37 | real b2 = sqrt(a * b); |
---|
| 38 | real tmp = a - a2; |
---|
| 39 | real t2 = t - p * tmp * tmp; |
---|
| 40 | real p2 = p + p; |
---|
| 41 | a = a2; b = b2; t = t2; p = p2; |
---|
[989] | 42 | } |
---|
| 43 | |
---|
[996] | 44 | real sum = a + b; |
---|
| 45 | sum = sum * sum / ((real)4 * t); |
---|
[989] | 46 | sum.print(); |
---|
| 47 | |
---|
[984] | 48 | return EXIT_SUCCESS; |
---|
| 49 | } |
---|
| 50 | |
---|
Note: See
TracBrowser
for help on using the repository browser.