stdfloat
#include <numeric>
#include <stdexcept>
#include <vector>
int main() {
// 1. Calculate the average of a vector of numbers
std::vector<double> numbers = {1.0, 2.0, 3.0, 4.0, 5.0};
double average = std::accumulate(numbers.begin(), numbers.end(), 0.0) / numbers.size();
std::cout << "Average: " << average << std::endl;
// 2. Find the minimum and maximum values in a vector of numbers
double min_value = *std::min_element(numbers.begin(), numbers.end());
double max_value = *std::max_element(numbers.begin(), numbers.end());
std::cout << "Minimum value: " << min_value << std::endl;
std::cout << "Maximum value: " << max_value << std::endl;
// 3. Check if a number is within a specified range
double number = 3.5;
double lower_bound = 2.0;
double upper_bound = 5.0;
bool is_within_range = (number >= lower_bound && number <= upper_bound);
std::cout << "Is number within range? " << (is_within_range ? "Yes" : "No") << std::endl;
// 4. Round a number to the nearest integer
double rounded_number = std::round(number);
std::cout << "Rounded number: " << rounded_number << std::endl;
// 5. Truncate a number to the integer part
double truncated_number = std::trunc(number);
std::cout << "Truncated number: " << truncated_number << std::endl;
// 6. Calculate the square root of a number
double square_root = std::sqrt(number);
std::cout << "Square root: " << square_root << std::endl;
// 7. Calculate the absolute value of a number
double absolute_value = std::abs(number);
std::cout << "Absolute value: " << absolute_value << std::endl;
// 8. Check if a number is finite
bool is_finite = std::isfinite(number);
std::cout << "Is number finite? " << (is_finite ? "Yes" : "No") << std::endl;
// 9. Check if a number is infinite
bool is_infinite = std::isinf(number);
std::cout << "Is number infinite? " << (is_infinite ? "Yes" : "No") << std::endl;
// 10. Check if a number is a NaN
bool is_nan = std::isnan(number);
std::cout << "Is number NaN? " << (is_nan ? "Yes" : "No") << std::endl;
// 11. Convert a string to a float
std::string number_string = "3.14";
float float_number;
try {
float_number = std::stof(number_string);
std::cout << "Float number: " << float_number << std::endl;
} catch (std::invalid_argument& e) {
std::cerr << "Error converting string to float: " << e.what() << std::endl;
}
// 12. Convert a float to a string
std::string float_string = std::to_string(float_number);
std::cout << "Float string: " << float_string << std::endl;
// 13. Check if two floats are equal
float float1 = 1.0f;
float float2 = 1.00000001f;
bool are_equal = std::fequal(float1, float2);
std::cout << "Are floats equal? " << (are_equal ? "Yes" : "No") << std::endl;
// 14. Check if two floats are approximately equal (within a tolerance)
bool are_approximately_equal =
std::fabs(float1 - float2) < std::numeric_limits<float>::epsilon();
std::cout << "Are floats approximately equal? "
<< (are_approximately_equal ? "Yes" : "No") << std::endl;
// 15. Compare two floats
int comparison_result = std::fpclassify(float1 - float2);
switch (comparison_result) {
case FP_NAN:
std::cout << "Floats are NaN" << std::endl;
break;
case FP_INFINITE:
std::cout << "Floats are infinite" << std::endl;
break;
case FP_ZERO:
std::cout << "Floats are zero" << std::endl;
break;
case FP_SUBNORMAL:
std::cout << "Floats are subnormal" << std::endl;
break;
case FP_NORMAL:
std::cout << "Floats are normal" << std::endl;
break;
}
// 16. Convert a float to a double
double double_number = static_cast<double>(float_number);
std::cout << "Double number: " << double_number << std::endl;
// 17. Convert a double to a float
float float_number_from_double = static_cast<float>(double_number);
std::cout << "Float number from double: " << float_number_from_double << std::endl;
// 18. Calculate the sine of a number
double sine_value = std::sin(number);
std::cout << "Sine: " << sine_value << std::endl;
// 19. Calculate the cosine of a number
double cosine_value = std::cos(number);
std::cout << "Cosine: " << cosine_value << std::endl;
// 20. Calculate the tangent of a number
double tangent_value = std::tan(number);
std::cout << "Tangent: " << tangent_value << std::endl;
// 21. Calculate the inverse sine of a number
double arcsine_value = std::asin(number);
std::cout << "Arcsine: " << arcsine_value << std::endl;
// 22. Calculate the inverse cosine of a number
double arccosine_value = std::acos(number);
std::cout << "Arccosine: " << arccosine_value << std::endl;
// 23. Calculate the inverse tangent of a number
double arctangent_value = std::atan(number);
std::cout << "Arctangent: " << arctangent_value << std::endl;
// 24. Calculate the hyperbolic sine of a number
double sinh_value = std::sinh(number);
std::cout << "Sinh: " << sinh_value << std::endl;
// 25. Calculate the hyperbolic cosine of a number
double cosh_value = std::cosh(number);
std::cout << "Cosh: " << cosh_value << std::endl;
// 26. Calculate the hyperbolic tangent of a number
double tanh_value = std::tanh(number);
std::cout << "Tanh: " << tanh_value << std::endl;
// 27. Calculate the inverse hyperbolic sine of a number
double arcsinh_value = std::asinh(number);
std::cout << "Arcsinh: " << arcsinh_value << std::endl;
// 28. Calculate the inverse hyperbolic cosine of a number
double arccosh_value = std::acosh(number);
std::cout << "Arccosh: " << arccosh_value << std::endl;
// 29. Calculate the inverse hyperbolic tangent of a number
double arctanh_value = std::atanh(number);
std::cout << "Arctanh: " << arctanh_value << std::endl;
// 30. Calculate the exponential function of a number
double exp_value = std::exp(number);
std::cout << "Exp: " << exp_value << std::endl;
// 31. Calculate the logarithm of a number (base 10)
double log_value = std::log10(number);
std::cout << "Log10: " << log_value << std::endl;
// 32. Calculate the natural logarithm of a number
double ln_value = std::log(number);
std::cout << "Ln: " << ln_value << std::endl;
// 33. Calculate the power of a number
double pow_value = std::pow(number, 2.0);
std::cout << "Power: " << pow_value << std::endl;
// 34. Calculate the remainder of a division
double remainder_value = std::remainder(number, 2.0);
std::cout << "Remainder: " << remainder_value << std::endl;
// 35. Check if a number is odd or even
bool is_odd = (std::fmod(number, 2.0) != 0.0);
std::cout << "Is number odd? " << (is_odd ?