In algorithmic trading, it's often necessary to perform complex mathematical calculations. Luckily, C++ provides a variety of built-in mathematical functions and libraries that can help.
One commonly used library is cmath. This library includes functions like sqrt, exp, log, pow, as well as trigonometric functions like sin, cos, and tan.
Let's take a look at an example that demonstrates the usage of these functions:
TEXT/X-C++SRC
1#include <iostream>
2#include <cmath>
3
4using namespace std;
5
6int main() {
7 double x = 4.1;
8 double y = 2.5;
9
10 // Square root
11 double squareRoot = sqrt(x);
12
13 // Exponential
14 double exponential = exp(x);
15
16 // Natural logarithm
17 double naturalLogarithm = log(x);
18
19 // Power
20 double power = pow(x, y);
21
22 // Trigonometric functions
23 double sine = sin(x);
24 double cosine = cos(x);
25 double tangent = tan(x);
26
27 // Rounding
28 double roundDown = floor(x);
29 double roundUp = ceil(x);
30
31 // Absolute value
32 double absoluteValue = abs(x);
33
34 // Minimum and Maximum
35 double minVal = min(x, y);
36 double maxVal = max(x, y);
37
38 // Print results
39 cout << "Square root: " << squareRoot << endl;
40 cout << "Exponential: " << exponential << endl;
41 cout << "Natural logarithm: " << naturalLogarithm << endl;
42 cout << "Power: " << power << endl;
43 cout << "Sine: " << sine << endl;
44 cout << "Cosine: " << cosine << endl;
45 cout << "Tangent: " << tangent << endl;
46 cout << "Round down: " << roundDown << endl;
47 cout << "Round up: " << roundUp << endl;
48 cout << "Absolute value: " << absoluteValue << endl;
49 cout << "Minimum value: " << minVal << endl;
50 cout << "Maximum value: " << maxVal << endl;
51
52 return 0;
53}xxxxxxxxxx53
}using namespace std;int main() { double x = 4.1; double y = 2.5; // Square root double squareRoot = sqrt(x); // Exponential double exponential = exp(x); // Natural logarithm double naturalLogarithm = log(x); // Power double power = pow(x, y); // Trigonometric functions double sine = sin(x); double cosine = cos(x); double tangent = tan(x); // Rounding double roundDown = floor(x); double roundUp = ceil(x);OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment



