The <cmath> library in C++ provides a wide range of mathematical functions that are essential for scientific calculations, engineering projects, and any application requiring precise numerical operations. Understanding these functions is crucial for writing efficient and accurate code.
In this tutorial, we will explore key mathematical functions provided by the <cmath> library, including abs, ceil, floor, round, sqrt, pow, log, sin, cos, tan, fmod, and remainder. Each function will be explained with real-world analogies, followed by code examples and expected outputs.
The <cmath> library is part of the C++ Standard Library and includes functions for trigonometric calculations, logarithms, exponentiation, and more. These functions are optimized for performance and provide a consistent interface across different platforms.
Understanding these mathematical functions can greatly enhance your ability to solve complex problems in fields such as physics, engineering, finance, and data science.
absThe abs function returns the absolute value of an integer. The absolute value is the non-negative value of a number without regard to its sign.
Example:
1#include <iostream>2#include <cmath>34int main() {5int num = -10;6std::cout << "Absolute value of " << num << " is " << abs(num) << std::endl;7return 0;8}
Absolute value of -10 is 10
ceilThe ceil function returns the smallest integer greater than or equal to a given floating-point number.
Example:
1#include <iostream>2#include <cmath>34int main() {5double num = 3.14;6std::cout << "Ceiling of " << num << " is " << ceil(num) << std::endl;7return 0;8}
Ceiling of 3.14 is 4
floorThe floor function returns the largest integer less than or equal to a given floating-point number.
Example:
1#include <iostream>2#include <cmath>34int main() {5double num = 3.14;6std::cout << "Floor of " << num << " is " << floor(num) << std::endl;7return 0;8}
Floor of 3.14 is 3
roundThe round function rounds a floating-point number to the nearest integer.
Example:
1#include <iostream>2#include <cmath>34int main() {5double num = 3.67;6std::cout << "Rounded value of " << num << " is " << round(num) << std::endl;7return 0;8}
Rounded value of 3.67 is 4
sqrtThe sqrt function returns the square root of a non-negative floating-point number.
Example:
1#include <iostream>2#include <cmath>34int main() {5double num = 16;6std::cout << "Square root of " << num << " is " << sqrt(num) << std::endl;7return 0;8}
Square root of 16 is 4
powThe pow function returns the result of raising a base number to an exponent.
Example:
1#include <iostream>2#include <cmath>34int main() {5double base = 2;6double exponent = 3;7std::cout << base << " raised to the power of " << exponent << " is " << pow(base, exponent) << std::endl;8return 0;9}
2 raised to the power of 3 is 8
logThe log function returns the natural logarithm (base e) of a number.
Example:
1#include <iostream>2#include <cmath>34int main() {5double num = 2.718;6std::cout << "Natural logarithm of " << num << " is " << log(num) << std::endl;7return 0;8}
Natural logarithm of 2.718 is 1
sin, cos, tanThe <cmath> library provides trigonometric functions such as sin, cos, and tan. These functions take an angle in radians and return the sine, cosine, or tangent of that angle.
Example:
1#include <iostream>2#include <cmath>34int main() {5double angle = M_PI / 4; // 45 degrees6std::cout << "Sine of " << angle << " is " << sin(angle) << std::endl;7std::cout << "Cosine of " << angle << " is " << cos(angle) << std::endl;8std::cout << "Tangent of " << angle << " is " << tan(angle) << std::endl;9return 0;10}
Sine of 0.785398 is 0.707107 Cosine of 0.785398 is 0.707107 Tangent of 0.785398 is 1
fmodThe fmod function returns the floating-point remainder of dividing one number by another.
Example:
1#include <iostream>2#include <cmath>34int main() {5double numerator = 10;6double denominator = 3;7std::cout << "Remainder of " << numerator << " divided by " << denominator << " is " << fmod(numerator, denominator) << std::endl;8return 0;9}
Remainder of 10 divided by 3 is 1
remainderThe remainder function returns the floating-point remainder of dividing one number by another, similar to fmod, but with different rounding behavior.
Example:
1#include <iostream>2#include <cmath>34int main() {5double numerator = 10;6double denominator = 3;7std::cout << "Remainder of " << numerator << " divided by " << denominator << " is " << remainder(numerator, denominator) << std::endl;8return 0;9}
Remainder of 10 divided by 3 is 1
Let's create a complete program that demonstrates the use of several <cmath> functions in a practical scenario. We'll calculate the distance between two points using the Euclidean distance formula and apply some trigonometric operations.
1#include <iostream>2#include <cmath>34int main() {5double x1 = 3, y1 = 4;6double x2 = 6, y2 = 8;78// Calculate the distance between two points9double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));10std::cout << "Distance between (" << x1 << ", " << y1 << ") and (" << x2 << ", " << y2 << ") is " << distance << std::endl;1112// Calculate the angle using atan213double angle = atan2(y2 - y1, x2 - x1);14std::cout << "Angle between points is " << angle * 180 / M_PI << " degrees" << std::endl;1516return 0;17}
Distance between (3, 4) and (6, 8) is 5 Angle between points is 53.1301 degrees
| Function | Description |
|---|---|
abs | Returns the absolute value of an integer. |
ceil | Returns the smallest integer greater than or equal to a floating-point number. |
floor | Returns the largest integer less than or equal to a floating-point number. |
round | Rounds a floating-point number to the nearest integer. |
sqrt | Returns the square root of a non-negative floating-point number. |
pow | Returns the result of raising a base number to an exponent. |
log | Returns the natural logarithm (base e) of a number. |
sin | Returns the sine of an angle in radians. |
cos | Returns the cosine of an angle in radians. |
tan | Returns the tangent of an angle in radians. |
fmod | Returns the floating-point remainder of dividing one number by another. |
remainder | Returns the floating-point remainder of dividing one number by another, with different rounding behavior. |
Now that you have a solid understanding of the <cmath> library and its functions, it's time to explore string manipulation in C++ using the <string> library. This will allow you to handle text data more effectively in your programs.
Next Topic: <string> Reference