Open In App

C++ 20 – <numbers> Header

Last Updated : 15 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

C++20 has a recently developed header file labeled that incorporates mathematical functions and constants. Its purpose is to provide standard library support for mathematical operations, simplifying the process for C++ programmers to incorporate mathematical functions and constants into their programming. Our focus in this essay is the header file of C++20 and its numerous features.

Syntax:

#include <numbers>

Example 1:

C++




// C++ Program to demonstrate the use of the <numbers>
// header.
#include <iostream>
#include <numbers>
  
int main()
{
    std::cout << "Pi: " << std::numbers::pi << std::endl;
    std::cout << "e: " << std::numbers::e << std::endl;
    std::cout << "Sqrt of 2: " << std::numbers::sqrt2
              << std::endl;
    return 0;
}


Output:

Pi: 3.14159
e: 2.71828
Sqrt of 2: 1.41421

Explanation: In this example, Within the header file, one can find a plethora of mathematical constants, such as pi, e, and the square root of 2. These constants are designated as inline variables within their corresponding types.

Example 2:

C++




// C++ Program to demonstrate the use of the <numbers>
// header.
#include <cmath>
#include <iostream>
#include <numbers>
  
int main()
{
    std::cout << "sin(pi/6): "
              << std::sin(std::numbers::pi / 6)
              << std::endl;
    std::cout << "cosh(2): " << std::cosh(2) << std::endl;
    std::cout << "exp(1): " << std::exp(1) << std::endl;
    return 0;
}


Output:

sin(pi/6): 0.5
cosh(2): 3.7622
exp(1): 2.71828

Explanation: In this example, Templates are used in the header file to define a variety of mathematical functions, such as exponential functions, trigonometric functions, and hyperbolic functions. These functions are versatile and can be used for any type that supports the necessary operations.

Advantages of using <numbers> Header are:

  • Standardization: The header simplifies the use of mathematical operations for programmers with its standard library support. It maintains uniformity in the implementation and functionality of these functions, irrespective of the platforms or compilers being used.
  • Performance: The header offers math functions that are optimized for quicker execution than user-created ones. These functions greatly enhance performance and speed up calculations, particularly in apps where processing speed is crucial.
  • Ease of Use: Rephrased text: By utilizing the header named, programmers can conveniently access a comprehensive set of mathematical functions and constants. This simplifies the process of coding with math operations, as it allows programmers to focus on the logic of their algorithm rather than the technicalities of mathematical implementation.
  • Increased Safety: By utilizing the header, one can access mathematical constants and call mathematical functions through type-safe interfaces. This is advantageous as it reduces common programming mistakes such as utilizing incorrect data types or supplying incorrect arguments during function calls.
  • More Expressive: The updated header presents an assortment of fresh mathematical functions that were once absent from the standard library, including Bessel functions and inverse hyperbolic functions. This allows for greater flexibility and brevity in coding complex mathematical notions.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads