Browse Source

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

legacy
Sam Hocevar sam 13 years ago
parent
commit
ff45d2ac98
6 changed files with 63 additions and 2 deletions
  1. +1
    -0
      .gitignore
  2. +1
    -0
      configure.ac
  3. +5
    -2
      src/real.h
  4. +2
    -0
      test/Makefile.am
  5. +17
    -0
      test/math/Makefile.am
  6. +37
    -0
      test/math/pi.cpp

+ 1
- 0
.gitignore View File

@@ -59,5 +59,6 @@ test/testsuite
test/benchsuite
test/quad
test/sandbox
test/math/pi
tools/make-font
# Our data

+ 1
- 0
configure.ac View File

@@ -322,6 +322,7 @@ AC_CONFIG_FILES(
[Makefile
src/Makefile
test/Makefile
test/math/Makefile
gfx/Makefile
gfx/font/Makefile
tools/Makefile


+ 5
- 2
src/real.h View File

@@ -23,8 +23,6 @@ namespace lol

class real
{
static int const BIGITS = 32;

public:
inline real() { }

@@ -60,6 +58,11 @@ public:
void print() const;

private:
/* XXX: changing this requires tuning real::fres (the number of
* Newton-Raphson iterations) and real::print (the number of printed
* digits) */
static int const BIGITS = 32;

uint32_t m_size;
uint32_t m_signexp;
uint16_t m_mantissa[BIGITS];


+ 2
- 0
test/Makefile.am View File

@@ -1,4 +1,6 @@

SUBDIRS = math

AM_CPPFLAGS = -I$(top_srcdir)/src

bench: benchsuite$(EXEEXT)


+ 17
- 0
test/math/Makefile.am View File

@@ -0,0 +1,17 @@

AM_CPPFLAGS = -I$(top_srcdir)/src

all-local: $(noinst_PROGRAMS)
test x$(MAKE_FSELF) = xno || make_fself pi$(EXEEXT) pi.self

CLEANFILES = $(noinst_PROGRAMS:%$(EXEEXT)=%.self) \
$(noinst_PROGRAMS:%$(EXEEXT)=%.elf) \
$(noinst_PROGRAMS:%$(EXEEXT)=%.exe)

noinst_PROGRAMS = pi

pi_SOURCES = pi.cpp
pi_CPPFLAGS = @LOL_CFLAGS@ @PIPI_CFLAGS@
pi_LDFLAGS = $(top_builddir)/src/liblol.a @LOL_LIBS@ @PIPI_LIBS@
pi_DEPENDENCIES = $(top_builddir)/src/liblol.a


+ 37
- 0
test/math/pi.cpp View File

@@ -0,0 +1,37 @@
//
// Lol Engine - Sample math program: compute Pi
//
// Copyright: (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
// This program is free software; you can redistribute it and/or
// modify it under the terms of the Do What The Fuck You Want To
// Public License, Version 2, as published by Sam Hocevar. See
// http://sam.zoy.org/projects/COPYING.WTFPL for more details.
//

#if defined HAVE_CONFIG_H
# include "config.h"
#endif

#include "core.h"

using namespace lol;

int main(int argc, char **argv)
{
/* Approximate Pi using Machin's formula: 16*atan(1/5)-4*atan(1/239) */
real sum = 0.0, x0 = 5.0, x1 = 239.0;
real const m0 = -x0 * x0, m1 = -x1 * x1, r16 = 16.0, r4 = 4.0;

/* Degree 240 is required for 512-bit mantissa precision */
for (int i = 1; i < 240; i += 2)
{
sum += r16 / (x0 * (real)i) - r4 / (x1 * (real)i);
x0 *= m0;
x1 *= m1;
}

sum.print();

return EXIT_SUCCESS;
}


Loading…
Cancel
Save