/* Program Title: C++ Math Functions Demonstration */
#include <iostream> // std::cout
#include <cmath> // math functions
#include <cstdint> // fixed-size integers
#include <algorithm> // std::min, std::max, std::clamp
int main() {
/* ============================
Constants
============================ */
double pi = M_PI; // π constant
double e = M_E; // Euler's number
std::cout << "Constants:\n";
std::cout << " pi = " << pi << "\n";
std::cout << " e = " << e << "\n\n";
/* ============================
Core Functions
============================ */
double x = 2.5; // sample value
std::cout << "Core Functions:\n";
std::cout << " sqrt(2.5) = " << std::sqrt(x) << "\n"; // square root
std::cout << " cbrt(2.5) = " << std::cbrt(x) << "\n"; // cube root
std::cout << " exp(2.5) = " << std::exp(x) << "\n"; // e^x
std::cout << " log(2.5) = " << std::log(x) << "\n"; // natural log
std::cout << " log10(2.5)= " << std::log10(x)<< "\n"; // base-10 log
std::cout << " pow(2.5,3)= " << std::pow(x,3)<< "\n\n"; // x^3
/* ============================
Trigonometric Functions
============================ */
double angle = M_PI / 4; // 45 degrees in radians
std::cout << "Trigonometric Functions:\n";
std::cout << " sin(pi/4) = " << std::sin(angle) << "\n"; // sine
std::cout << " cos(pi/4) = " << std::cos(angle) << "\n"; // cosine
std::cout << " tan(pi/4) = " << std::tan(angle) << "\n"; // tangent
std::cout << " asin(0.5) = " << std::asin(0.5) << "\n"; // arcsine
std::cout << " acos(0.5) = " << std::acos(0.5) << "\n"; // arccosine
std::cout << " atan(1.0) = " << std::atan(1.0) << "\n\n"; // arctangent
/* ============================
Hyperbolic Functions
============================ */
std::cout << "Hyperbolic Functions:\n";
std::cout << " sinh(1) = " << std::sinh(1) << "\n"; // hyperbolic sine
std::cout << " cosh(1) = " << std::cosh(1) << "\n"; // hyperbolic cosine
std::cout << " tanh(1) = " << std::tanh(1) << "\n\n"; // hyperbolic tangent
/* ============================
Rounding Functions
============================ */
double y = -2.7; // negative number for rounding tests
std::cout << "Rounding Functions:\n";
std::cout << " floor(-2.7) = " << std::floor(y) << "\n"; // round down
std::cout << " ceil(-2.7) = " << std::ceil(y) << "\n"; // round up
std::cout << " round(-2.7) = " << std::round(y) << "\n"; // nearest integer
std::cout << " trunc(-2.7) = " << std::trunc(y) << "\n\n"; // remove decimals
/* ============================
Min / Max / Clamp
============================ */
double a = 10, b = 20; // values for min/max
double value = 15; // value to clamp
std::cout << "Min/Max/Clamp:\n";
std::cout << " min(10,20) = " << std::min(a,b) << "\n"; // smaller of two
std::cout << " max(10,20) = " << std::max(a,b) << "\n"; // larger of two
std::cout << " clamp(15,0,10) = " << std::clamp(value,0.0,10.0) << "\n\n"; // clamp value
/* ============================
Bitwise Math (Integers)
============================ */
uint8_t A = 0b10101010; // binary literal
uint8_t B = 0b11001100; // binary literal
std::cout << "Bitwise Math:\n";
std::cout << " A & B = 0x" << std::hex << (A & B) << std::dec << "\n"; // AND
std::cout << " A | B = 0x" << std::hex << (A | B) << std::dec << "\n"; // OR
std::cout << " A ^ B = 0x" << std::hex << (A ^ B) << std::dec << "\n"; // XOR
std::cout << " ~A = 0x" << std::hex << (uint8_t)(~A) << std::dec << "\n"; // NOT
std::cout << " A << 2 = 0x" << std::hex << (A << 2) << std::dec << "\n"; // left shift
std::cout << " B >> 3 = 0x" << std::hex << (B >> 3) << std::dec << "\n\n"; // right shift
/* ============================
Additional Useful Math
============================ */
std::cout << "Additional Math:\n";
std::cout << " abs(-3.14) = " << std::fabs(-3.14) << "\n"; // absolute value
std::cout << " fmod(10,3) = " << std::fmod(10,3) << "\n"; // remainder
std::cout << " hypot(3,4) = " << std::hypot(3,4) << "\n"; // sqrt(x²+y²)
std::cout << " ldexp(1,4) = " << std::ldexp(1,4) << "\n"; // 1 * 2^4
}
/* run:
Constants:
pi = 3.14159
e = 2.71828
Core Functions:
sqrt(2.5) = 1.58114
cbrt(2.5) = 1.35721
exp(2.5) = 12.1825
log(2.5) = 0.916291
log10(2.5)= 0.39794
pow(2.5,3)= 15.625
Trigonometric Functions:
sin(pi/4) = 0.707107
cos(pi/4) = 0.707107
tan(pi/4) = 1
asin(0.5) = 0.523599
acos(0.5) = 1.0472
atan(1.0) = 0.785398
Hyperbolic Functions:
sinh(1) = 1.1752
cosh(1) = 1.54308
tanh(1) = 0.761594
Rounding Functions:
floor(-2.7) = -3
ceil(-2.7) = -2
round(-2.7) = -3
trunc(-2.7) = -2
Min/Max/Clamp:
min(10,20) = 10
max(10,20) = 20
clamp(15,0,10) = 10
Bitwise Math:
A & B = 0x88
A | B = 0xee
A ^ B = 0x66
~A = 0xU
A << 2 = 0x2a8
B >> 3 = 0x19
Additional Math:
abs(-3.14) = 3.14
fmod(10,3) = 1
hypot(3,4) = 5
ldexp(1,4) = 16
*/