#include <iostream>
#include <cmath>
#include <limits>
#include <iomanip>
#include <type_traits>
#define PI 3.14159265358979323846
template<typename T>
T square(T x) {
static_assert(std::is_floating_point<T>::value, "Template parameter must be a float or double");
return x * x;
}
namespace MathUtils {
inline float cube(float x) {
return x * x * x;
}
typedef double (*MathFunc)(double);
auto sqrtLambda = [](double x) -> double {
return std::sqrt(x);
};
}
enum class RoundingMode {
UP,
DOWN,
NEAREST
};
double roundToNearest(double value, RoundingMode mode = RoundingMode::NEAREST) {
switch (mode) {
case RoundingMode::UP:
return std::ceil(value);
case RoundingMode::DOWN:
return std::floor(value);
case RoundingMode::NEAREST:
default:
return std::round(value);
}
}
template<typename... Args>
double sum(Args... args) {
return (args + ...);
}
struct FloatPrinter {
void print(float value) const {
std::cout << "Float value: " << value << std::endl;
}
};
union FloatIntUnion {
float f;
int i;
};
void checkInfinity(float value) {
if (std::isinf(value)) {
throw std::overflow_error("Value is infinity");
}
}
int main() {
try {
float a = true ? 1.23f : 0.0f; double b = 4.56;
long double c = 7.89L;
double circumference = 2 * PI * b;
double bSquared = square(b);
float aCubed = MathUtils::cube(a);
MathUtils::MathFunc sqrtFunc = MathUtils::sqrtLambda;
double sqrtB = sqrtFunc(b);
double roundedValue = roundToNearest(circumference, RoundingMode::UP);
double totalSum = sum(a, b, c);
FloatPrinter printer;
printer.print(a);
FloatIntUnion fiUnion;
fiUnion.f = a;
std::cout << "Union int representation of float: " << fiUnion.i << std::endl;
checkInfinity(std::numeric_limits<float>::infinity());
} catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
return 0;
}
double IntegrationOfFunctions::calculate_trapezoid_integral(const Vector<double>& x, const Vector<double>& y)
{
int n = x.get_size();
double trapezoid_integral = 0;
for(int i = 0; i < n-1; i++)
{
trapezoid_integral += 0.5*(x[i+1]-x[i])*(y[i+1]+y[i]);
}
return(trapezoid_integral);
}