IVS-calculator
Loading...
Searching...
No Matches
mathlib Namespace Reference

Enumerations

enum  MathError {
  OK , DIVISION_BY_ZERO , NEGATIVE_FACTORIAL , INVALID_ROOT ,
  INVALID_ARGUMENT
}
 

Functions

double add (double a, double b)
 Implementation of function add.
 
double subtract (double a, double b)
 Implementation of function subtract.
 
double multiply (double a, double b)
 Implementation of function multiply.
 
double divide (double a, double b, MathError &err)
 Implementation of function divide.
 
double factorial (int a, MathError &err)
 Implementation of function factorial.
 
double power (double base, int exp)
 Implementation of function power.
 
double root (double base, int n, MathError &err)
 Implementation of function root.
 
double modulo (double a, double b, MathError &err)
 Implementation of function modulo.
 

Enumeration Type Documentation

◆ MathError

Enumerator
OK 
DIVISION_BY_ZERO 
NEGATIVE_FACTORIAL 
INVALID_ROOT 
INVALID_ARGUMENT 

Definition at line 11 of file mathlib.h.

11 {
12 OK,
17};
@ INVALID_ROOT
Definition mathlib.h:15
@ NEGATIVE_FACTORIAL
Definition mathlib.h:14
@ DIVISION_BY_ZERO
Definition mathlib.h:13
@ INVALID_ARGUMENT
Definition mathlib.h:16

Function Documentation

◆ add()

double mathlib::add ( double a,
double b )

Implementation of function add.

Add two numbers.

Parameters
afirst number
bsecond number
Returns
sum of a and b

Definition at line 9 of file mathlib.cpp.

9 {
10
11 return a + b;
12}

◆ divide()

double mathlib::divide ( double a,
double b,
MathError & err )

Implementation of function divide.

Divide two numbers.

Parameters
afirst number
bsecond number
errerror code (division by zero)
Returns
quotient of a and b

Definition at line 33 of file mathlib.cpp.

33 {
34
35 if(b == 0) {
36 err = DIVISION_BY_ZERO;
37 return 0;
38 }
39
40 err = OK;
41 return a / b;
42}

◆ factorial()

double mathlib::factorial ( int a,
MathError & err )

Implementation of function factorial.

Compute factorial of a number.

Parameters
ainput number (must be >= 0)
errerror code (negative input)
Returns
factorial of a

Definition at line 47 of file mathlib.cpp.

47 {
48
49 if(a < 0 ) {
51 return 0;
52 }
53
54 err = OK;
55
56 double result = 1;
57 for(int i = 1; i <= a; i++) {
58 result = result * i;
59 }
60
61 return result;
62}

◆ modulo()

double mathlib::modulo ( double a,
double b,
MathError & err )

Implementation of function modulo.

Compute modulo of two numbers.

Parameters
afirst number
bsecond number
errerror code (division by zero)
Returns
remainder of a divided by b

Definition at line 111 of file mathlib.cpp.

111 {
112
113 if(b == 0) {
114 err = DIVISION_BY_ZERO;
115 return 0;
116 }
117
118 err = OK;
119
120 return std::fmod(a, b);
121}

◆ multiply()

double mathlib::multiply ( double a,
double b )

Implementation of function multiply.

Multiply two numbers.

Parameters
afirst number
bsecond number
Returns
product of a and b

Definition at line 25 of file mathlib.cpp.

25 {
26
27 return a * b;
28}

◆ power()

double mathlib::power ( double base,
int exp )

Implementation of function power.

Raise a number to a natural exponent.

Parameters
baseinput number
expexponent (must be >= 0)
Returns
base raised to the power of exponent

Definition at line 67 of file mathlib.cpp.

67 {
68
69 if(exp == 0) {
70 return 1;
71 }
72
73 double result = 1;
74
75 for(int i = 0; i < exp; i++) {
76 result = result * base;
77 }
78
79 return result;
80}

◆ root()

double mathlib::root ( double base,
int n,
MathError & err )

Implementation of function root.

Compute n-th root of a number.

Parameters
baseinput number
ndegree of the root (must be > 0)
errerror code (invalid root)
Returns
n-th root of base

Definition at line 85 of file mathlib.cpp.

85 {
86
87 if(n == 0) {
88 err = INVALID_ROOT;
89 return 0;
90 }
91 if(n < 0) {
92 err = INVALID_ROOT;
93 return 0;
94 }
95 if(base < 0 && n % 2 == 0) {
96 err = INVALID_ARGUMENT;
97 return 0;
98 }
99 if(base < 0 && n % 2 != 0) {
100 err = OK;
101 return -std::pow(-base, 1.0 / n);
102 }
103
104 err = OK;
105 return std::pow(base, 1.0 / n);
106}

◆ subtract()

double mathlib::subtract ( double a,
double b )

Implementation of function subtract.

Subtract two numbers.

Parameters
afirst number
bsecond number
Returns
difference of a and b

Definition at line 17 of file mathlib.cpp.

17 {
18
19 return a - b;
20}