Skip to content
This repository was archived by the owner on Sep 27, 2024. It is now read-only.

Commit e9dae2b

Browse files
author
Lennart
committed
Merge pull request #18 from mooxmirror/master
Prepare for release v0.1.1
2 parents 02ddea9 + 7d63676 commit e9dae2b

File tree

20 files changed

+499
-36
lines changed

20 files changed

+499
-36
lines changed

.travis.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
language: c
2-
compiler: clang
2+
compiler: g++-4.8
33
sudo: required
4+
dist: trusty
5+
6+
install:
7+
- sudo apt-get update -qq
8+
- sudo apt-get install gcc-4.8 -y -qq
49

510
script:
611
- make all

Makefile

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,73 @@
11
# Compiler flags
2-
CFLAGS=-I ./include -std=c++11 -Wall
3-
CFLAGS_LIB=-I ./include -std=c++11 -c
2+
CFLAGS=-I./include -std=gnu++11 -Wall
3+
CFLAGS_LIB=-I./include -std=gnu++11 -c
4+
CFLAGS_TEST=test/tmath_test.cpp build/libtmath.a
45

5-
all: lib
6+
all: lib test
7+
@echo Done.
68

7-
lib: tmath.o
8-
mkdir -p build
9-
echo "Packaging library ..."
9+
lib: build_folder tmath.o
1010
ar rcs build/libtmath.a build/tmath.o
1111

12-
test: lib
13-
echo "Running tests ..."
12+
test: test_sine test_cosine test_tangent test_cosecant test_cotangent test_secant test_rad_deg test_abs test_factorial test_roots test_power test_exp_log
13+
@echo all tests passed
1414

15-
examples: lib
16-
echo "Building examples ..."
15+
build_folder:
16+
@mkdir -p build
1717

18-
tmath.o:
19-
echo "Building source files ..."
20-
mkdir -p build
18+
test_folder:
19+
@mkdir -p build/test
20+
21+
test_sine: test_folder
22+
$(CC) $(CFLAGS) test/sine/test.cpp -o build/test/sine $(CFLAGS_TEST)
23+
@build/test/sine
24+
25+
test_cosine: test_folder
26+
$(CC) $(CFLAGS) test/cosine/test.cpp -o build/test/cosine $(CFLAGS_TEST)
27+
@build/test/cosine
28+
29+
test_tangent: test_folder
30+
$(CC) $(CFLAGS) test/tangent/test.cpp -o build/test/tangent $(CFLAGS_TEST)
31+
@build/test/tangent
32+
33+
test_cosecant: test_folder
34+
$(CC) $(CFLAGS) test/cosecant/test.cpp -o build/test/cosecant $(CFLAGS_TEST)
35+
@build/test/cosecant
36+
37+
test_cotangent: test_folder
38+
$(CC) $(CFLAGS) test/cotangent/test.cpp -o build/test/cotangent $(CFLAGS_TEST)
39+
@build/test/cotangent
40+
41+
test_secant: test_folder
42+
$(CC) $(CFLAGS) test/secant/test.cpp -o build/test/secant $(CFLAGS_TEST)
43+
@build/test/secant
44+
45+
test_rad_deg: test_folder
46+
$(CC) $(CFLAGS) test/rad-deg/test.cpp -o build/test/rad-deg $(CFLAGS_TEST)
47+
@build/test/rad-deg
48+
49+
test_abs: test_folder
50+
$(CC) $(CFLAGS) test/abs/test.cpp -o build/test/abs $(CFLAGS_TEST)
51+
@build/test/abs
52+
53+
test_factorial: test_folder
54+
$(CC) $(CFLAGS) test/factorial/test.cpp -o build/test/factorial $(CFLAGS_TEST)
55+
@build/test/factorial
56+
57+
test_roots: test_folder
58+
$(CC) $(CFLAGS) test/roots/test.cpp -o build/test/roots $(CFLAGS_TEST)
59+
@build/test/roots
60+
61+
test_power: test_folder
62+
$(CC) $(CFLAGS) test/power/test.cpp -o build/test/power $(CFLAGS_TEST)
63+
@build/test/power
64+
65+
test_exp_log: test_folder
66+
$(CC) $(CFLAGS) test/exp-log/test.cpp -o build/test/exp-log $(CFLAGS_TEST)
67+
@build/test/exp-log
68+
69+
tmath.o: build_folder
2170
$(CC) $(CFLAGS_LIB) src/tmath.cpp -o build/tmath.o
2271

23-
clean:
24-
echo "Cleaning up ..."
72+
clean: build_folder
2573
rm build -f -r

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ A small math function collection based on the Taylor expansion series.
33

44
## Building the project
55
- Download the source files from the `master`-tree
6-
- To build the project, you need to have `clang` and `make` installed
6+
- To build the project, you need to have `g++` and `make` installed
7+
- And you need to run `export CC=g++`
78
- If everything is ready, run `make all` and ...
89
- ... your `libtmath.a` library file is ready in the `build` folder
910

@@ -13,7 +14,7 @@ Just build it as described above, include the header files and link the library.
1314
## What is included?
1415

1516
Function | Description
16-
:------------------------: | -----------------------------------
17+
:------------------------: | ---------------------------------------
1718
`sin(DOUBLE x)` | sine of x
1819
`asin(DOUBLE x)` | arcsine of x
1920
`sinh(DOUBLE x)` | hyperbolic sine of x
@@ -45,8 +46,12 @@ Function | Description
4546
`pow(LONG x, LONG n)` | x to the power of n
4647
`pow(DOUBLE x, LONG n)` | x to the power of n
4748
`fac(LONG n)` | factorial of n
49+
`facd(LONG n)` | factorial of n using floating point
50+
`oddfac(LONG n)` | odd-factorial of n
51+
`oddfacd(LONG n)` | odd-factorial of n using floating point
4852
`rad(DOUBLE x)` | degrees to radiant
4953
`deg(DOUBLE x)` | radiant to degrees
54+
`abs(DOUBLE x)` | absolute value of x
5055

5156
## What is planned?
5257
- Statistics (planned for v0.3)

examples/arcsec_demo.cpp

Lines changed: 0 additions & 10 deletions
This file was deleted.

include/tmath.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ DOUBLE sec(DOUBLE x); // secant
3131
DOUBLE asec(DOUBLE x); // arcsecant
3232
DOUBLE sech(DOUBLE x); // hyperbolic secant
3333

34-
DOUBLE cosec(DOUBLE x); // cosecant
34+
DOUBLE csc(DOUBLE x); // cosecant
3535
DOUBLE acsc(DOUBLE x); // arccosecant
3636
DOUBLE csch(DOUBLE x); // hyperbolic cosecant
3737

@@ -51,6 +51,11 @@ DOUBLE pow(DOUBLE x, LONG n);
5151
LONG pow(LONG x, LONG n);
5252

5353
LONG fac(LONG n);
54+
DOUBLE facd(LONG n);
55+
LONG oddfac(LONG n);
56+
DOUBLE oddfacd(LONG n);
57+
58+
DOUBLE abs(DOUBLE x);
5459
}
5560

5661
#endif

include/tmath_test.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef _TMATH_TEST_HPP
2+
#define _TMATH_TEST_HPP
3+
4+
#include <string>
5+
#include "tmath.hpp"
6+
7+
namespace TMathTest {
8+
const TMath::DOUBLE DEFAULT_TOLERANCE = 0.001;
9+
bool equal(TMath::DOUBLE x, TMath::DOUBLE y, TMath::DOUBLE tolerance);
10+
void assert(TMath::DOUBLE value, TMath::DOUBLE correct, std::string expression);
11+
void assert(TMath::DOUBLE value, TMath::DOUBLE correct, std::string expression, TMath::DOUBLE tolerance);
12+
}
13+
14+
#endif

src/tmath.cpp

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@ TMath::DOUBLE TMath::sin(DOUBLE x)
1212
}
1313
TMath::DOUBLE TMath::asin(DOUBLE x)
1414
{
15-
x = mod(x + 1, 2) - 1;
1615
DOUBLE r = 0;
17-
for (LONG n = 0; n <= 8L; n++)
16+
DOUBLE delta = 1;
17+
for (LONG n = 1; delta > 1e-6; n++)
1818
{
19-
r += fac(2 * n) / (pow(DOUBLE(2), 2 * n) * pow(fac(n), 2) * (2 * n + 1)) * pow(x, 2 * n + 1);
19+
LONG odd = 2 * n - 1;
20+
DOUBLE oddf = oddfacd(odd - 2);
21+
DOUBLE f = facd(odd);
22+
DOUBLE p = pow(x, odd);
23+
DOUBLE d = p / f * oddf * oddf;
24+
delta = abs(d);
25+
r += p / f * oddf * oddf;
2026
}
2127
return r;
2228
}
@@ -50,9 +56,13 @@ TMath::DOUBLE TMath::tan(DOUBLE x)
5056
TMath::DOUBLE TMath::atan(DOUBLE x)
5157
{
5258
DOUBLE r = 0;
53-
for (LONG n = 0; n <= 8; n++)
59+
DOUBLE delta = 1;
60+
for (LONG n = 0; delta > 1e-4; n++)
5461
{
55-
r += pow(DOUBLE(-1), n) * pow(x, 2 * n + 1) / (2 * n + 1);
62+
LONG odd = 2 * n + 1;
63+
DOUBLE d = DOUBLE(pow(-1LL, n)) * pow(x, odd) / DOUBLE(odd);
64+
delta = abs(d);
65+
r += d;
5666
}
5767
return r;
5868
}
@@ -87,7 +97,7 @@ TMath::DOUBLE TMath::sech(DOUBLE x)
8797
return 1 / cosh(x);
8898
}
8999
/* ================================ COSECANT ======================================== */
90-
TMath::DOUBLE TMath::cosec(DOUBLE x)
100+
TMath::DOUBLE TMath::csc(DOUBLE x)
91101
{
92102
return 1 / sin(x);
93103
}
@@ -133,7 +143,7 @@ TMath::DOUBLE TMath::exp(DOUBLE x)
133143
DOUBLE r = 0;
134144
for (LONG n = 0; n <= 15L; n++)
135145
{
136-
r += pow(x, n) / fac(n);
146+
r += pow(x, n) / facd(n);
137147
}
138148
return r;
139149
}
@@ -208,6 +218,31 @@ TMath::LONG TMath::fac(LONG n) {
208218
}
209219
return r;
210220
}
221+
TMath::DOUBLE TMath::facd(LONG n) {
222+
DOUBLE r = 1;
223+
for (LONG i = 2; i <= n; i++) {
224+
r *= DOUBLE(i);
225+
}
226+
return r;
227+
}
228+
TMath::LONG TMath::oddfac(LONG n) {
229+
LONG r = 1;
230+
for (LONG i = 3; i <= n; i += 2) {
231+
r *= i;
232+
}
233+
return r;
234+
}
235+
TMath::DOUBLE TMath::oddfacd(LONG n) {
236+
DOUBLE r = 1;
237+
for (LONG i = 3; i <= n; i += 2) {
238+
r *= DOUBLE(i);
239+
}
240+
return r;
241+
}
242+
TMath::DOUBLE TMath::abs(DOUBLE x) {
243+
if (x < 0) return -x;
244+
else return x;
245+
}
211246
/* ========================================== DEGREE / RADIANT CONVERSION ================================*/
212247
TMath::DOUBLE TMath::rad(DOUBLE deg)
213248
{

test/abs/test.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
This test checks the abs function.
3+
*/
4+
5+
#include "tmath.hpp"
6+
#include "tmath_test.hpp"
7+
8+
int main(int argc, char const *argv[]) {
9+
using TMathTest::assert;
10+
using TMath::abs;
11+
12+
assert(abs(0.0), 0.0, "abs(0) == 0");
13+
assert(abs(1.0), 1.0, "abs(1) == 1");
14+
assert(abs(-1.0), 1.0, "abs(-1) == 1");
15+
16+
return 0;
17+
}

test/cosecant/test.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
This test checks the cosecant, arccosecant and hyperbolic cosecant function.
3+
*/
4+
5+
#include "tmath.hpp"
6+
#include "tmath_test.hpp"
7+
8+
int main(int argc, char const *argv[]) {
9+
using TMathTest::assert;
10+
using TMath::csc;
11+
using TMath::acsc;
12+
using TMath::csch;
13+
using TMath::PI;
14+
15+
assert(csc(PI / 2.0), 1.0, "csc(PI/2) == 1");
16+
assert(csc(-PI / 2.0), -1.0, "csc(-PI/2) == -1");
17+
18+
assert(acsc(1.1884), 1.0, "acsc(1.1884) == 1");
19+
assert(acsc(-1.1884), -1.0, "acsc(-1.1884) == -1");
20+
21+
assert(csch(0.88137), 1.0, "csch(0.88137) == 1.0");
22+
assert(csch(-0.88137), -1.0, "csch(-0.88137) == -1.0");
23+
24+
return 0;
25+
}

test/cosine/test.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
This test checks the cosine, arccosine and hyperbolic cosine function.
3+
*/
4+
5+
#include "tmath.hpp"
6+
#include "tmath_test.hpp"
7+
8+
int main(int argc, char const *argv[]) {
9+
using TMathTest::assert;
10+
using TMath::cos;
11+
using TMath::acos;
12+
using TMath::cosh;
13+
using TMath::PI;
14+
15+
assert(cos(0.0), 1, "cos(0) == 1");
16+
assert(cos(PI/2.0), 0, "cos(PI/2) == 0");
17+
assert(cos(PI), -1, "cos(PI) == -1");
18+
assert(cos(PI * 1.5), 0, "cos(PI * 1.5) == 0");
19+
20+
assert(acos(0), PI * 0.5, "acos(0) == PI/2");
21+
assert(acos(0.70711), PI * 0.25, "acos(1/sqrt(2)) == 1/4 PI");
22+
assert(acos(-0.70711), PI * 0.75, "acos(1/sqrt(2)) == 3/4 PI");
23+
24+
assert(cosh(0), 1, "cosh(0) == 1");
25+
assert(cosh(1.31696), 2, "cosh(1.31696) == 2");
26+
assert(cosh(-1.31696), 2, "cosh(-1.31696) == 2");
27+
28+
return 0;
29+
}

0 commit comments

Comments
 (0)