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

Commit

Permalink
Split up tmath.cpp into units
Browse files Browse the repository at this point in the history
  • Loading branch information
Lennart Espe committed Apr 15, 2016
1 parent 8fa75da commit 27217d4
Show file tree
Hide file tree
Showing 18 changed files with 478 additions and 437 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,7 @@ $RECYCLE.BIN/

# Windows shortcuts
*.lnk

build/*
objects/*
lib/*
56 changes: 36 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
# Compiler flags
CFLAGS=-I./include -std=c++11 -Wall
CFLAGS_LIB=-I./include -std=c++11 -c
CFLAGS_TEST=./build/libtmath.a test/tmath_test.cpp

all: lib test
@echo Done.

lib: build_folder tmath.o
ar rcs build/libtmath.a build/tmath.o

# build directory
BDIR=build
# object directory
ODIR=objects
# source directory
SDIR=src
# include directory
IDIR=include
# test directory
TDIR=test

LIB=$(BDIR)/libtmath.a
INC=-I$(IDIR)
CFLAGS=-std=c++11 -Wall
CFLAGS_TEST=$(INC) $(LIB) test/tmath_test.cpp

_OBJECTS= abs.o cosecant.o cosine.o cotangent.o degrad.o equality.o \
explog.o factorial.o fcm.o power.o roots.o secant.o sine.o \
tangent.o vector.o
OBJECTS=$(patsubst %,$(ODIR)/%,$(_OBJECTS))

all: folders $(LIB) test

folders:
@mkdir -p $(BDIR) $(ODIR)

$(ODIR)/%.o: $(SDIR)/%.cpp
$(CC) -c $(INC) -o $@ $< $(CFLAGS)

$(LIB): $(OBJECTS)
ar rvs $(LIB) $^

clean:
rm -rf $(BDIR) $(ODIR)

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 test_vectors
@echo all tests passed

build_folder:
@mkdir -p build

test_folder:
@mkdir -p build/test

Expand Down Expand Up @@ -68,10 +90,4 @@ test_exp_log: test_folder

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

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

clean: build_folder
rm build -f -r
@build/test/vectors
6 changes: 6 additions & 0 deletions src/abs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "tmath.hpp"

TMath::DOUBLE TMath::abs(const DOUBLE &x) {
if (x < 0) return -x;
else return x;
}
15 changes: 15 additions & 0 deletions src/cosecant.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "tmath.hpp"

/* ================================ COSECANT ======================================== */
TMath::DOUBLE TMath::csc(const DOUBLE &x)
{
return 1 / sin(x);
}
TMath::DOUBLE TMath::acsc(const DOUBLE &x)
{
return asin(1 / x);
}
TMath::DOUBLE TMath::csch(const DOUBLE &x)
{
return 1 / sinh(x);
}
20 changes: 20 additions & 0 deletions src/cosine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "tmath.hpp"

/* ================================ COSINE ======================================== */
TMath::DOUBLE TMath::cos(const DOUBLE &_x)
{
const DOUBLE x = mod(_x + PI, 2 * PI) - PI;
DOUBLE r = 0;
for (LONG n = 0; n <= 8L; n++) {
r += pow(DOUBLE(-1.0), n) * pow(x, 2 * n) / fac(2 * n);
}
return r;
}
TMath::DOUBLE TMath::acos(const DOUBLE &x)
{
return PI / 2 - asin(x);
}
TMath::DOUBLE TMath::cosh(const DOUBLE &x)
{
return 0.5 * (exp(x) + exp(-x));
}
15 changes: 15 additions & 0 deletions src/cotangent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "tmath.hpp"

/* ================================ COTANGENT ======================================== */
TMath::DOUBLE TMath::cot(const DOUBLE &x)
{
return cos(x) / sin(x);
}
TMath::DOUBLE TMath::acot(const DOUBLE &x)
{
return PI / 2 - atan(x);
}
TMath::DOUBLE TMath::coth(const DOUBLE &x)
{
return cosh(x) / sinh(x);
}
10 changes: 10 additions & 0 deletions src/degrad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "tmath.hpp"

TMath::DOUBLE TMath::rad(const DOUBLE &deg)
{
return PI / 180.0 * deg;
}
TMath::DOUBLE TMath::deg(const DOUBLE &rad)
{
return 180.0 / PI * rad;
}
8 changes: 8 additions & 0 deletions src/equality.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "tmath.hpp"

TMath::DOUBLE TMath::equal(const DOUBLE &x, const DOUBLE &y) {
return equal(x, y, EQUAL_EPSILON);
}
TMath::DOUBLE TMath::equal(const DOUBLE &x, const DOUBLE &y, const DOUBLE &eps) {
return abs(x - y) < eps;
}
34 changes: 34 additions & 0 deletions src/explog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "tmath.hpp"

/* =============================== EXPONENTIAL FUNCTION, SQRT, LOGARITHM ======================= */
TMath::DOUBLE TMath::exp(const DOUBLE &x)
{
DOUBLE r = 0;
for (LONG n = 0; n <= 15L; n++)
{
r += pow(x, n) / facd(n);
}
return r;
}
TMath::DOUBLE TMath::ln(const DOUBLE &_x)
{
const DOUBLE x = (_x - 1) / (_x + 1);
DOUBLE r = 0;
for (LONG n = 0; n <= 100L; n++)
{
r += 2 * pow(x, 2 * n + 1) / (2 * n + 1);
}
return r;
}
TMath::DOUBLE TMath::lg(const DOUBLE &x)
{
return ln(x) / ln(10);
}
TMath::DOUBLE TMath::lb(const DOUBLE &x)
{
return ln(x) / ln(2);
}
TMath::DOUBLE TMath::log(const DOUBLE &x, const DOUBLE &n)
{
return ln(x) / ln(n);
}
31 changes: 31 additions & 0 deletions src/factorial.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "tmath.hpp"

TMath::LONG TMath::fac(const LONG &n) {
LONG r = 1;
for (LONG i = 2; i <= n; i++)
{
r *= i;
}
return r;
}
TMath::DOUBLE TMath::facd(const LONG &n) {
DOUBLE r = 1;
for (LONG i = 2; i <= n; i++) {
r *= DOUBLE(i);
}
return r;
}
TMath::LONG TMath::oddfac(const LONG &n) {
LONG r = 1;
for (LONG i = 3; i <= n; i += 2) {
r *= i;
}
return r;
}
TMath::DOUBLE TMath::oddfacd(const LONG &n) {
DOUBLE r = 1;
for (LONG i = 3; i <= n; i += 2) {
r *= DOUBLE(i);
}
return r;
}
30 changes: 30 additions & 0 deletions src/fcm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "tmath.hpp"

/* ================================= FLOOR, CEIL AND MODULO ======================================== */
TMath::LONG TMath::floor(const DOUBLE &x)
{
LONG truncated = LONG(x);
if (x < 0) {
if (truncated > x) {
return truncated - 1;
} else {
return truncated;
}
}
else {
return truncated;
}
}
TMath::LONG TMath::ceil(const DOUBLE &x)
{
LONG truncated = LONG(x);
if (x < 0) {
return truncated;
} else {
return truncated + 1;
}
}
TMath::DOUBLE TMath::mod(const DOUBLE &x, const DOUBLE &y)
{
return y * ((x / y) - floor(x / y));
}
34 changes: 34 additions & 0 deletions src/power.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "tmath.hpp"

/* =================================== POWER FUNCTIONS =====================================================*/
TMath::DOUBLE TMath::pow(const DOUBLE &x, const LONG &n)
{
if (n < 0) {
return 1 / pow(x, -n);
}

DOUBLE r = 1;
for (LONG i = 1; i <= n; i++)
{
r *= x;
}
return r;
}

TMath::LONG TMath::pow(const LONG &x, const LONG &n)
{
if (n < 0) {
return 1 / pow(x, -n);
}

LONG r = 1;
for (LONG i = 1; i <= n; i++)
{
r *= x;
}
return r;
}
TMath::DOUBLE TMath::pow(const DOUBLE &x, const DOUBLE &n)
{
return exp(n * ln(x));
}
14 changes: 14 additions & 0 deletions src/roots.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "tmath.hpp"

TMath::DOUBLE TMath::sqrt(const DOUBLE &x)
{
return root(x, 2);
}
TMath::DOUBLE TMath::root(const DOUBLE &x, const DOUBLE &n)
{
if (x > 0) {
return pow(x, 1 / n);
} else {
return 0;
}
}
15 changes: 15 additions & 0 deletions src/secant.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "tmath.hpp"

/* ================================ SECANT ======================================== */
TMath::DOUBLE TMath::sec(const DOUBLE &x)
{
return 1 / cos(x);
}
TMath::DOUBLE TMath::asec(const DOUBLE &x)
{
return acos(1 / x);
}
TMath::DOUBLE TMath::sech(const DOUBLE &x)
{
return 1 / cosh(x);
}
32 changes: 32 additions & 0 deletions src/sine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "tmath.hpp"

/* ================================ SINE ======================================== */
TMath::DOUBLE TMath::sin(const DOUBLE &_x)
{
const DOUBLE x = mod(_x + PI, 2 * PI) - PI;
DOUBLE r = 0;
for (LONG n = 0; n <= 8L; n++) {
r += pow(DOUBLE(-1), n) * pow(x, 2 * n + 1) / fac(2 * n + 1);
}
return r;
}
TMath::DOUBLE TMath::asin(const DOUBLE &x)
{
DOUBLE r = 0;
DOUBLE delta = 1;
for (LONG n = 1; delta > 1e-6; n++)
{
LONG odd = 2 * n - 1;
DOUBLE oddf = oddfacd(odd - 2);
DOUBLE f = facd(odd);
DOUBLE p = pow(x, odd);
DOUBLE d = p / f * oddf * oddf;
delta = abs(d);
r += p / f * oddf * oddf;
}
return r;
}
TMath::DOUBLE TMath::sinh(const DOUBLE &x)
{
return 0.5 * (exp(x) - exp(-x));
}
24 changes: 24 additions & 0 deletions src/tangent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "tmath.hpp"

/* ================================ TANGENT ======================================== */
TMath::DOUBLE TMath::tan(const DOUBLE &x)
{
return sin(x) / cos(x);
}
TMath::DOUBLE TMath::atan(const DOUBLE &x)
{
DOUBLE r = 0;
DOUBLE delta = 1;
for (LONG n = 0; delta > 1e-4; n++)
{
LONG odd = 2 * n + 1;
DOUBLE d = DOUBLE(pow(-1LL, n)) * pow(x, odd) / DOUBLE(odd);
delta = abs(d);
r += d;
}
return r;
}
TMath::DOUBLE TMath::tanh(const DOUBLE &x)
{
return sinh(x) / cosh(x);
}
Loading

0 comments on commit 27217d4

Please sign in to comment.