Geek Slack

Learn C++
About Lesson


C++ Math


C++ Math

C++ provides a variety of mathematical functions and operators to perform mathematical calculations. This chapter covers the basic math operators, functions, and how to use them effectively in your programs.

1. Basic Arithmetic Operators

C++ supports the following basic arithmetic operators:

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • %: Modulus

Example:

#include <iostream>

int main() {
    int a = 10;
    int b = 3;

    std::cout << "Addition: " << (a + b) << std::endl;
    std::cout << "Subtraction: " << (a - b) << std::endl;
    std::cout << "Multiplication: " << (a * b) << std::endl;
    std::cout << "Division: " << (a / b) << std::endl;
    std::cout << "Modulus: " << (a % b) << std::endl;

    return 0;
}

2. Increment and Decrement Operators

Increment and decrement operators are used to increase or decrease the value of a variable by 1:

  • ++: Increment
  • --: Decrement

Example:

#include <iostream>

int main() {
    int count = 5;
    count++; // Increment
    std::cout << "Increment: " << count << std::endl;

    count--; // Decrement
    std::cout << "Decrement: " << count << std::endl;

    return 0;
}

3. Math Functions

C++ includes a set of standard math functions defined in the cmath library:

  • abs(): Absolute value
  • sqrt(): Square root
  • pow(): Power
  • round(): Rounding to the nearest integer
  • ceil(): Rounding up to the nearest integer
  • floor(): Rounding down to the nearest integer
  • sin(), cos(), tan(): Trigonometric functions

Example:

#include <iostream>
#include <cmath>

int main() {
    double x = -5.5;
    double y = 2.0;
    
    std::cout << "Absolute value of x: " << abs(x) << std::endl;
    std::cout << "Square root of y: " << sqrt(y) << std::endl;
    std::cout << "2 raised to the power of y: " << pow(2, y) << std::endl;
    std::cout << "Round 3.6: " << round(3.6) << std::endl;
    std::cout << "Ceiling of 3.1: " << ceil(3.1) << std::endl;
    std::cout << "Floor of 3.9: " << floor(3.9) << std::endl;
    std::cout << "Sine of 45 degrees (in radians): " << sin(M_PI / 4) << std::endl;
    std::cout << "Cosine of 45 degrees (in radians): " << cos(M_PI / 4) << std::endl;
    std::cout << "Tangent of 45 degrees (in radians): " << tan(M_PI / 4) << std::endl;

    return 0;
}

4. Random Numbers

C++ provides facilities to generate random numbers using the cstdlib and ctime libraries:

  • rand(): Generates a random number
  • srand(): Sets the seed for the random number generator

Example:

#include <iostream>
#include <cstdlib>
#include <ctime>

int main() {
    std::srand(std::time(0)); // Seed for random number generation

    int random_number = std::rand() % 100; // Random number between 0 and 99
    std::cout << "Random Number: " << random_number << std::endl;

    return 0;
}

5. Using the cmath Library

To use math functions, include the cmath library. Here’s how you can utilize some of the math functions:

#include <iostream>
#include <cmath>

int main() {
    double value = 2.5;
    std::cout << "Square root of " << value << " is " << sqrt(value) << std::endl;
    std::cout << "2 raised to the power of " << value << " is " << pow(2, value) << std::endl;
    std::cout << "Ceiling value of " << value << " is " << ceil(value) << std::endl;
    std::cout << "Floor value of " << value << " is " << floor(value) << std::endl;

    return 0;
}

Conclusion

In this chapter, we covered the essential math operators and functions available in C++. We explored basic arithmetic operations, increment and decrement operators, mathematical functions, random number generation, and the usage of the cmath library. With these tools, you can perform a wide range of mathematical calculations and manipulations in your C++ programs.

Join the conversation