Skip to content
This repository was archived by the owner on Sep 27, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
b56256c
feat: create first test case
Dec 27, 2015
069c31c
test: add basic test environment
Dec 27, 2015
8448200
chore: fix compiler choice
Dec 27, 2015
a600580
chore: revert compiler change
Dec 27, 2015
da8881a
test: add first sine test
Dec 27, 2015
c365f83
test: add cosine test, refactor makefile, minor change to header makro
Dec 27, 2015
5243338
feat: create first test case
Dec 27, 2015
a3b9db9
test: add basic test environment
Dec 27, 2015
711537e
chore: fix compiler choice
Dec 27, 2015
e6ef293
chore: revert compiler change
Dec 27, 2015
e68df8b
test: add first sine test
Dec 27, 2015
5bef6fd
test: add cosine test, refactor makefile, minor change to header makro
Dec 27, 2015
7e27390
fix: synchronize branches
Dec 27, 2015
711c128
fix: sine function error for negative x
Dec 27, 2015
6743501
fix: push accuracy of atan function
Dec 27, 2015
de0b67f
doc: fix copy-paste typo
Dec 27, 2015
0470dda
chore: compilers are annoying
Dec 27, 2015
3151be5
chore: compilers change again
Dec 27, 2015
0da1af5
chore: we all hate compilers
Dec 27, 2015
9de3590
chore: compiling is hard
Dec 27, 2015
752a8c2
chore: compiler change
Dec 28, 2015
df423e7
chore: last change, last hope
Dec 28, 2015
f929fe9
chore: change std lib
Dec 28, 2015
a2811f4
chore: change compiler version
Dec 28, 2015
ba1d931
chore: change install requirements
Dec 28, 2015
6013ffc
chore: change dist to trusty
Dec 28, 2015
b33ae3e
chore: typo in compiler name
Dec 28, 2015
3c9cc79
chore: change compiler version
Dec 28, 2015
577291b
fix: error in oddfac
Dec 28, 2015
cb07555
test: update notification on success
Dec 28, 2015
de634fb
fix: remove unused includes
Dec 28, 2015
ae9d334
test: add tests for abs, factorial and rad-deg functions
Dec 28, 2015
49e6730
test: add power test
Dec 28, 2015
5c02fb7
test: add roots test
Dec 28, 2015
f32ed93
test: add cosecant tests
Dec 28, 2015
bb571c2
test: add cosecant code
Dec 28, 2015
b1aa910
test: add test for cotangent
Dec 28, 2015
ee6789a
test: add secant test
Dec 28, 2015
cb645b1
fix: increased accuracy of exp
Dec 28, 2015
bed7fe9
test: add exp-log test
Dec 28, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
language: c
compiler: clang
compiler: g++-4.8
sudo: required
dist: trusty

install:
- sudo apt-get update -qq
- sudo apt-get install gcc-4.8 -y -qq

script:
- make all
Expand Down
78 changes: 63 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,25 +1,73 @@
# Compiler flags
CFLAGS=-I ./include -std=c++11 -Wall
CFLAGS_LIB=-I ./include -std=c++11 -c
CFLAGS=-I./include -std=gnu++11 -Wall
CFLAGS_LIB=-I./include -std=gnu++11 -c
CFLAGS_TEST=test/tmath_test.cpp build/libtmath.a

all: lib
all: lib test
@echo Done.

lib: tmath.o
mkdir -p build
echo "Packaging library ..."
lib: build_folder tmath.o
ar rcs build/libtmath.a build/tmath.o

test: lib
echo "Running tests ..."
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
@echo all tests passed

examples: lib
echo "Building examples ..."
build_folder:
@mkdir -p build

tmath.o:
echo "Building source files ..."
mkdir -p build
test_folder:
@mkdir -p build/test

test_sine: test_folder
$(CC) $(CFLAGS) test/sine/test.cpp -o build/test/sine $(CFLAGS_TEST)
@build/test/sine

test_cosine: test_folder
$(CC) $(CFLAGS) test/cosine/test.cpp -o build/test/cosine $(CFLAGS_TEST)
@build/test/cosine

test_tangent: test_folder
$(CC) $(CFLAGS) test/tangent/test.cpp -o build/test/tangent $(CFLAGS_TEST)
@build/test/tangent

test_cosecant: test_folder
$(CC) $(CFLAGS) test/cosecant/test.cpp -o build/test/cosecant $(CFLAGS_TEST)
@build/test/cosecant

test_cotangent: test_folder
$(CC) $(CFLAGS) test/cotangent/test.cpp -o build/test/cotangent $(CFLAGS_TEST)
@build/test/cotangent

test_secant: test_folder
$(CC) $(CFLAGS) test/secant/test.cpp -o build/test/secant $(CFLAGS_TEST)
@build/test/secant

test_rad_deg: test_folder
$(CC) $(CFLAGS) test/rad-deg/test.cpp -o build/test/rad-deg $(CFLAGS_TEST)
@build/test/rad-deg

test_abs: test_folder
$(CC) $(CFLAGS) test/abs/test.cpp -o build/test/abs $(CFLAGS_TEST)
@build/test/abs

test_factorial: test_folder
$(CC) $(CFLAGS) test/factorial/test.cpp -o build/test/factorial $(CFLAGS_TEST)
@build/test/factorial

test_roots: test_folder
$(CC) $(CFLAGS) test/roots/test.cpp -o build/test/roots $(CFLAGS_TEST)
@build/test/roots

test_power: test_folder
$(CC) $(CFLAGS) test/power/test.cpp -o build/test/power $(CFLAGS_TEST)
@build/test/power

test_exp_log: test_folder
$(CC) $(CFLAGS) test/exp-log/test.cpp -o build/test/exp-log $(CFLAGS_TEST)
@build/test/exp-log

tmath.o: build_folder
$(CC) $(CFLAGS_LIB) src/tmath.cpp -o build/tmath.o

clean:
echo "Cleaning up ..."
clean: build_folder
rm build -f -r
10 changes: 0 additions & 10 deletions examples/arcsec_demo.cpp

This file was deleted.

4 changes: 3 additions & 1 deletion include/tmath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ DOUBLE sec(DOUBLE x); // secant
DOUBLE asec(DOUBLE x); // arcsecant
DOUBLE sech(DOUBLE x); // hyperbolic secant

DOUBLE cosec(DOUBLE x); // cosecant
DOUBLE csc(DOUBLE x); // cosecant
DOUBLE acsc(DOUBLE x); // arccosecant
DOUBLE csch(DOUBLE x); // hyperbolic cosecant

Expand All @@ -54,6 +54,8 @@ LONG fac(LONG n);
DOUBLE facd(LONG n);
LONG oddfac(LONG n);
DOUBLE oddfacd(LONG n);

DOUBLE abs(DOUBLE x);
}

#endif
14 changes: 14 additions & 0 deletions include/tmath_test.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef _TMATH_TEST_HPP
#define _TMATH_TEST_HPP

#include <string>
#include "tmath.hpp"

namespace TMathTest {
const TMath::DOUBLE DEFAULT_TOLERANCE = 0.001;
bool equal(TMath::DOUBLE x, TMath::DOUBLE y, TMath::DOUBLE tolerance);
void assert(TMath::DOUBLE value, TMath::DOUBLE correct, std::string expression);
void assert(TMath::DOUBLE value, TMath::DOUBLE correct, std::string expression, TMath::DOUBLE tolerance);
}

#endif
20 changes: 14 additions & 6 deletions src/tmath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ TMath::DOUBLE TMath::asin(DOUBLE x)
DOUBLE f = facd(odd);
DOUBLE p = pow(x, odd);
DOUBLE d = p / f * oddf * oddf;
delta = d;
delta = abs(d);
r += p / f * oddf * oddf;
}
return r;
Expand Down Expand Up @@ -56,9 +56,13 @@ TMath::DOUBLE TMath::tan(DOUBLE x)
TMath::DOUBLE TMath::atan(DOUBLE x)
{
DOUBLE r = 0;
for (LONG n = 0; n <= 8; n++)
DOUBLE delta = 1;
for (LONG n = 0; delta > 1e-4; n++)
{
r += pow(DOUBLE(-1), n) * pow(x, 2 * n + 1) / (2 * n + 1);
LONG odd = 2 * n + 1;
DOUBLE d = DOUBLE(pow(-1LL, n)) * pow(x, odd) / DOUBLE(odd);
delta = abs(d);
r += d;
}
return r;
}
Expand Down Expand Up @@ -93,7 +97,7 @@ TMath::DOUBLE TMath::sech(DOUBLE x)
return 1 / cosh(x);
}
/* ================================ COSECANT ======================================== */
TMath::DOUBLE TMath::cosec(DOUBLE x)
TMath::DOUBLE TMath::csc(DOUBLE x)
{
return 1 / sin(x);
}
Expand Down Expand Up @@ -139,7 +143,7 @@ TMath::DOUBLE TMath::exp(DOUBLE x)
DOUBLE r = 0;
for (LONG n = 0; n <= 15L; n++)
{
r += pow(x, n) / fac(n);
r += pow(x, n) / facd(n);
}
return r;
}
Expand Down Expand Up @@ -223,7 +227,7 @@ TMath::DOUBLE TMath::facd(LONG n) {
}
TMath::LONG TMath::oddfac(LONG n) {
LONG r = 1;
for (LONG i = 3; i <= n; i++) {
for (LONG i = 3; i <= n; i += 2) {
r *= i;
}
return r;
Expand All @@ -235,6 +239,10 @@ TMath::DOUBLE TMath::oddfacd(LONG n) {
}
return r;
}
TMath::DOUBLE TMath::abs(DOUBLE x) {
if (x < 0) return -x;
else return x;
}
/* ========================================== DEGREE / RADIANT CONVERSION ================================*/
TMath::DOUBLE TMath::rad(DOUBLE deg)
{
Expand Down
17 changes: 17 additions & 0 deletions test/abs/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
This test checks the abs function.
*/

#include "tmath.hpp"
#include "tmath_test.hpp"

int main(int argc, char const *argv[]) {
using TMathTest::assert;
using TMath::abs;

assert(abs(0.0), 0.0, "abs(0) == 0");
assert(abs(1.0), 1.0, "abs(1) == 1");
assert(abs(-1.0), 1.0, "abs(-1) == 1");

return 0;
}
25 changes: 25 additions & 0 deletions test/cosecant/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
This test checks the cosecant, arccosecant and hyperbolic cosecant function.
*/

#include "tmath.hpp"
#include "tmath_test.hpp"

int main(int argc, char const *argv[]) {
using TMathTest::assert;
using TMath::csc;
using TMath::acsc;
using TMath::csch;
using TMath::PI;

assert(csc(PI / 2.0), 1.0, "csc(PI/2) == 1");
assert(csc(-PI / 2.0), -1.0, "csc(-PI/2) == -1");

assert(acsc(1.1884), 1.0, "acsc(1.1884) == 1");
assert(acsc(-1.1884), -1.0, "acsc(-1.1884) == -1");

assert(csch(0.88137), 1.0, "csch(0.88137) == 1.0");
assert(csch(-0.88137), -1.0, "csch(-0.88137) == -1.0");

return 0;
}
29 changes: 29 additions & 0 deletions test/cosine/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
This test checks the cosine, arccosine and hyperbolic cosine function.
*/

#include "tmath.hpp"
#include "tmath_test.hpp"

int main(int argc, char const *argv[]) {
using TMathTest::assert;
using TMath::cos;
using TMath::acos;
using TMath::cosh;
using TMath::PI;

assert(cos(0.0), 1, "cos(0) == 1");
assert(cos(PI/2.0), 0, "cos(PI/2) == 0");
assert(cos(PI), -1, "cos(PI) == -1");
assert(cos(PI * 1.5), 0, "cos(PI * 1.5) == 0");

assert(acos(0), PI * 0.5, "acos(0) == PI/2");
assert(acos(0.70711), PI * 0.25, "acos(1/sqrt(2)) == 1/4 PI");
assert(acos(-0.70711), PI * 0.75, "acos(1/sqrt(2)) == 3/4 PI");

assert(cosh(0), 1, "cosh(0) == 1");
assert(cosh(1.31696), 2, "cosh(1.31696) == 2");
assert(cosh(-1.31696), 2, "cosh(-1.31696) == 2");

return 0;
}
26 changes: 26 additions & 0 deletions test/cotangent/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
This test checks the cotangent, arccotangent and hyperbolic cotangent function.
*/

#include "tmath.hpp"
#include "tmath_test.hpp"

int main(int argc, char const *argv[]) {
using TMathTest::assert;
using TMath::cot;
using TMath::acot;
using TMath::coth;
using TMath::PI;

assert(cot(PI / 2.0), 0.0, "cot(PI/2) == 0");
assert(cot(-PI / 2.0), 0.0, "cot(-PI/2) == 0");

assert(acot(0), PI * 0.5, "acot(0) == 1/2 PI");
assert(acot(1.0), PI * 0.25, "acot(1) == 1/4 PI");
assert(acot(-1.0), PI * 0.75, "acot(-1) == 3/4 PI");

assert(coth(0.54931), 2.0, "coth(0.54931) == 2.0");
assert(coth(-0.54931), -2.0, "coth(-0.54931) == -2.0");

return 0;
}
39 changes: 39 additions & 0 deletions test/exp-log/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
This test checks the exp, ln, lg, lb and log function.
*/

#include "tmath.hpp"
#include "tmath_test.hpp"

int main(int argc, char const *argv[]) {
using TMathTest::assert;
using TMath::exp;
using TMath::E;
using TMath::ln;
using TMath::lb;
using TMath::lg;
using TMath::log;

assert(exp(0.0), 1.0, "exp(0) == 1");
assert(exp(1.0), E, "exp(1) == e");
assert(exp(2.0), E * E, "exp(2) == e*e");
assert(exp(-1.0), 1.0 / E, "exp(-1) == 1/e");

assert(ln(1), 0.0, "ln(1) == 0");
assert(ln(E), 1.0, "ln(e) == 1");
assert(ln(1.0 / E), -1.0, "ln(1/e) == -1");

assert(lb(2), 1.0, "lb(2) == 1");
assert(lb(4), 2.0, "lb(4) == 2");
assert(lb(16), 4.0, "lb(128) == 4");

assert(lg(10), 1.0, "lg(10) == 1");
assert(lg(1), 0.0, "lg(1) == 0");
assert(lg(100), 2.0, "lg(100) == 2", 0.01);

assert(log(E, E), 1.0, "log_e(e) == 1");
assert(log(1, E), 0.0, "log_e(1) == 0");
assert(log(27, 3), 3.0, "log_3(27) == 3");

return 0;
}
38 changes: 38 additions & 0 deletions test/factorial/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
This test checks the factorial functions;
*/

#include "tmath.hpp"
#include "tmath_test.hpp"

int main(int argc, char const *argv[]) {
using TMathTest::assert;
using TMath::fac;
using TMath::facd;
using TMath::oddfac;
using TMath::oddfacd;

assert(fac(1), 1, "fac(1) == 1");
assert(fac(2), 2, "fac(2) == 2");
assert(fac(3), 6, "fac(3) == 6");
assert(fac(4), 24, "fac(4) == 24");

assert(facd(1), 1.0, "facd(1) == 1.0");
assert(facd(2), 2.0, "facd(2) == 2.0");
assert(facd(3), 6.0, "facd(3) == 6.0");
assert(facd(4), 24.0, "facd(4) == 24.0");

assert(oddfac(1), 1, "oddfac(1) == 1");
assert(oddfac(2), 1, "oddfac(2) == 1");
assert(oddfac(3), 3, "oddfac(3) == 3");
assert(oddfac(4), 3, "oddfac(4) == 3");
assert(oddfac(5), 15, "oddfac(5) == 15");

assert(oddfacd(1), 1.0, "oddfacd(1) == 1.0");
assert(oddfacd(2), 1.0, "oddfacd(2) == 1.0");
assert(oddfacd(3), 3.0, "oddfacd(3) == 3.0");
assert(oddfacd(4), 3.0, "oddfacd(4) == 3.0");
assert(oddfacd(5), 15.0, "oddfacd(5) == 15.0");

return 0;
}
Loading