A little-known feature of C++ is that the
cmath
library actually provides many mathematical constants that you can make use of in your quantitative finance programs.
To include the mathematical constants, you need to use a
#define
macro called
_USE_MATH_DEFINES
and add it before importing the
cmath
library:
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
int main() {
std::cout << M_PI << " " << M_E << " " << M_SQRT2 << endl;
return 0;
There are quite a few constants on offer. See if you can spot the ones that will be useful in quantitative finance:
Mathematical Expression
C++ Symbol
Decimal Representation
3.14159265358979323846
M_PI_2
1.57079632679489661923
M_PI_4
0.785398163397448309616
M_1_PI
0.318309886183790671538
M_2_PI
0.636619772367581343076
2/sqrt(pi)
M_2_SQRTPI
1.12837916709551257390
sqrt(2)
M_SQRT2
1.41421356237309504880
1/sqrt(2)
M_SQRT1_2
0.707106781186547524401
2.71828182845904523536
log_2(e)
M_LOG2E
1.44269504088896340736
log_10(e)
M_LOG10E
0.434294481903251827651
log_e(2)
M_LN2
0.693147180559945309417
log_e(10)
M_LN10
2.30258509299404568402
Note that it is not best practice within C++ to use #defines for mathematical constants! Instead, as an example, you should use
const double pi = 3.14159265358979323846;
. The #defines are a legacy feature of C.
Join the QSAlpha research platform that helps fill your strategy research pipeline, diversifies your portfolio and improves your risk-adjusted returns for increased profitability.
Find Out More
Join the Quantcademy membership portal that caters to the rapidly-growing retail quant trader community and learn how to increase your strategy profitability.
Find Out More
How to find new trading strategy ideas and objectively assess them for your portfolio using a Python-based backtesting engine.
Find Out More
How to implement advanced trading strategies using time series analysis, machine learning and Bayesian statistics with R and Python.
Find Out More