How to resolve the algorithm Real constants and functions step by step in the C++ programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Real constants and functions step by step in the C++ programming language

Table of Contents

Problem Statement

Show how to use the following math constants and functions in your language   (if not available, note it):

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Real constants and functions step by step in the C++ programming language

Code Explanation in Detail:

Code 1:

This code demonstrates the use of standard C++ mathematical functions for various operations:

  • euler_e: It defines a static variable initialized with the mathematical constant e (approximately 2.718).
  • pi: It defines a static variable initialized with the mathematical constant π (approximately 3.14159).
  • main():
    • Prints the values of e, pi, and other mathematical computations to the console, including square root, logarithm, exponentiation, absolute value, floor, ceiling, and power.

Code 2:

This code employs the Boost C++ Libraries to demonstrate the use of more advanced mathematical functions:

  • using namespace boost::math::double_constants;: This line imports various mathematical constants and functions defined in the Boost library.
  • main():
    • Prints the values of e, π, π², √2, ln(e), and other mathematical computations to the console.

Key Concepts Demonstrated:

Mathematical Constants:

  • M_E: Represents the mathematical constant e defined in the <cmath> header.
  • M_PI: Represents the mathematical constant π defined in the <cmath> header.
  • e and pi from Boost C++ Libraries: These provide high-precision values of mathematical constants.

Mathematical Functions:

  • std::sqrt(): Computes the square root.
  • std::exp(): Computes the exponential function (e^x).
  • std::log(): Computes the natural logarithm (ln).
  • std::log10(): Computes the base-10 logarithm (log10).
  • std::abs(): Computes the absolute value.
  • std::floor(): Computes the largest integer less than or equal to the argument.
  • std::ceil(): Computes the smallest integer greater than or equal to the argument.
  • std::pow(): Computes the power function (x^y).

Boost C++ Libraries:

  • Boost C++ Libraries provide high-precision values and functions for mathematical computations, such as e, pi, pi_sqr, and root_two.

Precision Control:

  • std::setprecision(18): Controls the number of decimal places displayed for certain computations.

Source code in the cpp programming language

#include <iostream>
#include <cmath>

#ifdef M_E
static double euler_e = M_E;
#else
static double euler_e = std::exp(1); // standard fallback
#endif

#ifdef M_PI
static double pi = M_PI;
#else
static double pi = std::acos(-1);
#endif

int main()
{
  std::cout << "e = " << euler_e
            << "\npi = " << pi
            << "\nsqrt(2) = " << std::sqrt(2.0)
            << "\nln(e) = " << std::log(euler_e)
            << "\nlg(100) = " << std::log10(100.0)
            << "\nexp(3) = " << std::exp(3.0)
            << "\n|-4.5| = " << std::abs(-4.5)   // or std::fabs(-4.5); both work in C++
            << "\nfloor(4.5) = " << std::floor(4.5)
            << "\nceiling(4.5) = " << std::ceil(4.5)
            << "\npi^2 = " << std::pow(pi,2.0) << std::endl;
}


#include <iostream>
#include <iomanip>
#include <cmath>
#include <boost/math/constants/constants.hpp>

int main()
{
    using namespace boost::math::double_constants;
    std::cout << "e = " << std::setprecision(18) << e
              << "\ne³ = " << std::exp(3.0)
              << "\nπ = " << pi
              << "\nπ² = " << pi_sqr
              << "\n√2 = " << root_two
              << "\nln(e) = " << std::log(e)
              << "\nlg(100) = " << std::log10(100.0)
              << "\n|-4.5| = " << std::abs(-4.5)
              << "\nfloor(4.5) = " << std::floor(4.5)
              << "\nceiling(4.5) = " << std::ceil(4.5) << std::endl;
}


  

You may also check:How to resolve the algorithm File input/output step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Determine if a string is squeezable step by step in the Action! programming language
You may also check:How to resolve the algorithm Deepcopy step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Sorting algorithms/Shell sort step by step in the Maple programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the SparForte programming language