SetPrecision in C++ With Examples
SetPrecision is an in-built C++ tool to manipulate floating-point values. Learn about how this function works with two easy examples.
SetPrecision in C++ is a built-in tool to modify floating-point numbers. It controls the number of decimal places displayed for floating-point values. Whether you’re working with mathematical calculations, scientific simulations, or financial models, setprecision() is a valuable tool for ensuring that your output is accurate and informative.
This tutorial will discuss the setprecision() function in C++ and explore how it works through code examples. We will be covering the following sections:
SetPrecision in C++
In C++, the setprecision function, given by setprecision(), is part of the <iomanip> library and is used to set the precision (number of digits after the decimal point) for output operations performed by the cout object.
The setprecision() function takes an integer argument. It specifies the number of digits to display after the decimal point.
Syntax
The syntax to declare setprecision() in C++ is below.
setprecision(int n)
As you can see, the setprecision() function requires only one argument, represented by n. This argument specifies the number of significant digits displayed for a floating-point number. The argument must be an integer and is necessary for the function to work.
Return Value
The return value of the setprecision() function in C++ is not specified. The purpose of this function is to act as a stream manipulator, modifying the output stream and not returning a value.
Exceptions
The setprecision() function does not typically generate exceptions. If the function throws an exception, the output stream remains in a valid state.
Best-suited C++ courses for you
Learn C++ with these high-rated online courses
How SetPrecision in C++ Works
The setprecision() function in C++ controls the number of significant digits displayed after the decimal point for floating-point numbers in the output stream. You can prevent information loss by setting a precise value with n significant digits.
For instance, when storing the value of pi with an infinite number of digits, some digits after the decimal may automatically reduce in a 4-bit or 8-bit memory architecture.
Using the setprecision() function with an argument of n, the value of pi should appear with n significant digits without information loss.
For example, setprecision(3) would display the value of pi as 3.14. To ensure that the correct number of decimal places show in the output, use the fixed keyword should before the setprecision() function. This will display the required number of decimal places in the output.
Examples of Using SetPrecision() in C++
Here are two examples.
Example 1
#include <iostream>#include <iomanip> int main() { double value = 123.456789; std::cout << "Default precision: " << value << std::endl; std::cout << std::setprecision(3) << "Precision set to 3: " << value << std::endl; std::cout << std::setprecision(5) << "Precision set to 5: " << value << std::endl; return 0;}
In this example, value is a floating-point number with a value of 123.456789. The cout stream displays the value of the variable value. The default precision is set to 3 and 5 using the setprecision() function. The output of this program will be:
Default precision: 123.457 Precision set to 3: 123 Precision set to 5: 123.46
The setprecision() function sets the number of significant digits after the decimal point for the output of value. The function returns an object of type ios_base&, which is used to set the precision for subsequent output operations performed by the cout object.
Example 2
#include <iostream>#include <iomanip>#include <cmath> int main() { double pi = 3.14159265358979; std::cout << std::fixed << std::setprecision(2); std::cout << "Pi with 2 decimal places: " << pi << std::endl; std::cout << "Sine of Pi/2 with 4 decimal places: " << std::sin(pi / 2) << std::endl; return 0;}
In this example, pi is a variable that stores the value of pi with a high degree of precision. The fixed manipulator displays the value with a fixed number of decimal places, and setprecision() is used to set the number of decimal places to 2. The sin function from the <cmath> library is used to calculate the sine of pi / 2 with a precision of 4 decimal places.
The output of this program will be:
Pi with 2 decimal places: 3.14 Sine of Pi/2 with 4 decimal places: 1.00
As you can see, the setprecision() function controls the number of decimal places displayed for both pi and the result of the sin function. The fixed manipulator is used to ensure that the decimal point does not change position for the output of pi and the sin function.
Endnotes
In conclusion, the setprecision() function in C++ is a powerful tool for controlling the number of decimal places displayed in the output of floating-point numbers. The function is easy to use and can help prevent information loss by allowing you to set a specific number of significant digits for floating-point numbers.
Hope this article helped you grasp the concept of setprecision() in C++. Explore our C++ articles to learn more about the language and consolidate your knowledge of the fundamentals.
Contributed by – Prerna Singh
Read More
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio