Notes
Detailed Notes on Useful Math Functions in C++
In this video, the instructor explains several useful math-related functions in C++, most of which are available in the cmath library. Hereโs a breakdown of the concepts, examples, and explanations for each function mentioned:
1. max() Function
-
Purpose: Returns the larger of two values.
-
Syntax:
std::max(value1, value2); -
Example:
double x = 3, y = 4, z; z = std::max(x, y); // z will be 4 std::cout << z; // Outputs: 4 -
Explanation: The
max()function compares two values and returns the greater one. In this case, it compares 3 and 4 and returns 4.
2. min() Function
-
Purpose: Returns the smaller of two values.
-
Syntax:
std::min(value1, value2); -
Example:
double x = 3, y = 4, z; z = std::min(x, y); // z will be 3 std::cout << z; // Outputs: 3 -
Explanation: The
min()function compares two values and returns the smaller one. In this case, it compares 3 and 4 and returns 3.
3. pow() Function (Power)
-
Purpose: Raises a number (base) to a specified power (exponent).
-
Syntax:
std::pow(base, exponent); -
Example:
double z; z = std::pow(2, 3); // 2 raised to the power of 3 is 8 std::cout << z; // Outputs: 8 -
Explanation: The
pow()function takes two arguments: the base and the exponent. It returns the base raised to the power of the exponent. For example,pow(2, 3)calculates2^3 = 8.
4. sqrt() Function (Square Root)
-
Purpose: Returns the square root of a number.
-
Syntax:
std::sqrt(value); -
Example:
double z; z = std::sqrt(9); // The square root of 9 is 3 std::cout << z; // Outputs: 3 -
Explanation: The
sqrt()function returns the square root of the given number. In this example, it computes the square root of 9, which is 3.
5. abs() Function (Absolute Value)
-
Purpose: Returns the absolute (non-negative) value of a number.
-
Syntax:
std::abs(value); -
Example:
double z; z = std::abs(-3); // The absolute value of -3 is 3 std::cout << z; // Outputs: 3 -
Explanation: The
abs()function takes a number (which could be negative) and returns its absolute value, i.e., the distance of that number from zero. For example,abs(-3)returns3.
6. round() Function
-
Purpose: Rounds a floating-point number to the nearest integer.
-
Syntax:
std::round(value); -
Example:
double x = 3.14, z; z = std::round(x); // Rounds 3.14 to 3 std::cout << z; // Outputs: 3 -
Explanation: The
round()function rounds a floating-point number to the nearest integer. For example,round(3.14)rounds to3, whileround(3.5)would round to4.
7. ceil() Function (Ceiling)
-
Purpose: Rounds a floating-point number up to the nearest integer.
-
Syntax:
std::ceil(value); -
Example:
double x = 3.14, z; z = std::ceil(x); // Rounds 3.14 up to 4 std::cout << z; // Outputs: 4 -
Explanation: The
ceil()function always rounds up, no matter the decimal. For instance,ceil(3.14)returns4.
8. floor() Function (Floor)
-
Purpose: Rounds a floating-point number down to the nearest integer.
-
Syntax:
std::floor(value); -
Example:
double x = 3.99, z; z = std::floor(x); // Rounds 3.99 down to 3 std::cout << z; // Outputs: 3 -
Explanation: The
floor()function always rounds down, regardless of the decimal value. For example,floor(3.99)returns3.
Additional Information:
-
cmathHeader: To use these functions, you need to include thecmathheader at the beginning of your C++ program:#include <cmath>
Conclusion:
These 8 math functions are essential tools for performing basic mathematical operations in C++ programs. They can be used in a variety of applications, such as computing powers, square roots, absolute values, rounding numbers, and handling integer comparisons. Additionally, C++ offers a wide array of other mathematical functions for more complex operations, which can be explored in the C++ documentation for further learning.