Increasing Precision With Setprecision in C++
The setprecision is a function in C++ that is part of the iomanip library. It is used to control the precision of floating-point numbers in the output stream.
In this article, we will be discussing the details of Setprecision C++ function, its syntax, use and examples.
Table of Contents
- What is Setprecision C++ function?
- Syntax
- How does Setprecision C++ function work?
- Common use
- Examples
What is Setprecision C++ function?
Setprecision C++ function takes a single argument, which is an integer representing the number of significant digits required in the output. When used in conjunction with the fixed manipulator, setprecision can be used to control the number of decimal places displayed in the output. By specifying the precision of floating-point numbers, setprecision helps to prevent loss of information during the output process.
Best-suited C++ courses for you
Learn C++ with these high-rated online courses
Syntax of Setprecision C++ Function
The syntax of the setprecision function from the iomanip library in C++ is as follows:
setprecision(int n);
where n is an integer value that specifies the number of decimal places to be displayed.
The setprecision function is typically used in combination with the std::fixed or std::scientific manipulators to control the format of the output.
For example:
std::cout << std::fixed << std::setprecision(2) << 3.1415926535;
This code sets the precision of the output to 2 decimal places and uses the std::fixed manipulator to control the output format. The resulting output will be 3.14.
Explore free C++ courses
How Does Setprecision C++ Function Work?
The setprecision function in C++ is part of the standard library and is used to set the precision of output stream manipulation. The setprecision function takes an integer argument that specifies the number of decimal places to be displayed.
Here’s an example of how you can use the setprecision function:
#include \n \n <iostream>\n \n \n \n #include \n \n \n \n <iomanip>\n \n \n \n \n \n int main()\n \n {\n \n double value = 3.1415926535;\n \n std::cout << "Default precision: " << value << std::endl;\n \n std::cout << "Precision set to 3: " << std::setprecision(3) << value << std::endl;\n \n std::cout << "Precision set to 5: " << std::setprecision(5) << value << std::endl;\n \n return 0;\n \n }\n \n \n \n </iomanip>\n \n \n \n </iostream>
In this example, the setprecision function is used to set the precision of the value output to 3 and 5 decimal places. The setprecision function returns the output stream, which can be combined with other manipulators, such as fixed or scientific, to control the format of the output. The setprecision function affects only the output of floating-point numbers and has no effect on integer values.
Common Use Cases of Setprecision() Function
Here are some common use cases for the setprecision function in C++:
- Formatting floating-point numbers: The setprecision function can be used to control the number of decimal places displayed for floating-point numbers. This is useful when you want to display floating-point numbers in a specific format, such as monetary amounts or scientific calculations.
- Improving readability: The setprecision function can be used to improve the readability of output by controlling the number of decimal places displayed. This is useful when displaying large or complex numbers, as it helps to avoid clutter and makes the output easier to understand.
- Customizing output: The setprecision function can be used in conjunction with other output stream manipulators to customize the format of the output. For example, you can use setprecision with fixed or scientific to control the format of floating-point numbers.
- Debugging: The setprecision function can be used to isolate specific parts of floating-point numbers for debugging purposes. By controlling the number of decimal places displayed, you can easily identify and fix errors in your calculations.
- Specifying precision: The setprecision function can be used to specify the precision of floating-point numbers in calculations, such as when working with financial or scientific applications. This is useful for ensuring consistent and accurate results.
Examples of setprecision() Function
Example 1: Here is a simple example of using the setprecision function in C++ to customize the output.
#include \n \n <iostream>\n \n \n \n #include \n \n \n \n <iomanip>\n \n \n \n \n \n int main() {\n \n double pi = 3.1415926535;\n \n std::cout << std::fixed << std::setprecision(2) << pi << std::endl;\n \n return 0;\n \n }\n \n \n \n </iomanip>\n \n \n \n </iostream>
Output:
3.14
In this example, the value of pi is set to 3.1415926535. The std::fixed manipulator is used to set the format of the output to fixed-point notation, and the std::setprecision function is used to set the precision of the output to 2 decimal places.
Example 2: Here is a simple example of using the setprecision function in C++ for debugging:
#include \n \n <iostream>\n \n \n \n #include \n \n \n \n <iomanip>\n \n \n \n \n \n int main() {\n \n double x = 123.456;\n \n double y = 789.123;\n \n double result = x + y;\n \n \n \n std::cout << std::setprecision(10) << "x = " << x << std::endl;\n \n std::cout << std::setprecision(10) << "y = " << y << std::endl;\n \n std::cout << std::setprecision(10) << "result = " << result << std::endl;\n \n return 0;\n \n }\n \n \n \n </iomanip>\n \n \n \n </iostream>
In this example, the value of x is set to 123.456, and the value of y is set to 789.123. The result variable is calculated as the sum of x and y.
The std::setprecision function is used to set the precision of the output to 10 decimal places, which can be useful for debugging purposes. By using high precision, you can easily see the exact values of x, y, and the result, which can help you to identify and fix any errors in your calculations.
The output of this program will be:
x = 123.456000000y = 789.123000000result = 912.579000000
Example 3: Here’s an example that demonstrates the default precision, maximum precision, and setting the precision using setprecision() to 5 digits:
#include \n \n <iostream>\n \n \n \n #include \n \n \n \n <iomanip>\n \n \n \n #include \n \n \n \n <limits>\n \n \n \n \n \n int main() {\n \n double x = 123.456;\n \n \n \n // default precision\n \n std::cout << "Default precision: " << x << std::endl;\n \n \n \n // maximum precision\n \n std::cout << "Maximum: " << std::setprecision(std::numeric_limits\n \n \n \n <double>\n \n ::digits10 + 1) << x << std::endl;\n \n \n \n // setting precision to 5 digits\n \n std::cout << "Precision set to 5 digits: " << std::setprecision(5) << x << std::endl;\n \n return 0;\n \n }\n \n \n \n </double>\n \n \n \n </limits>\n \n \n \n </iomanip>\n \n \n \n </iostream>
In this example, x is set to 123.456.
The default precision is demonstrated by simply printing x with std::cout. The output will show the default number of decimal places, which is usually 6 digits.
The maximum precision is demonstrated by using std::setprecision and passing the value of std::numeric_limits<double>::digits10 + 1. This sets the precision to the maximum number of decimal places that a double can represent.
Finally, the precision is set to 5 digits by using std::setprecision(5).
The output of this program will be:
Default precision: 123.456Maximum precision: 123.45600000000000277500Precision set to 5 digits: 123.46
Conclusion
The setprecision manipulator in C++ is utilized to control the precision of floating-point numbers after the decimal in the output stream. This function is defined within the header file. The setprecision() helps to prevent loss of information by allowing the specification of the number of significant digits required in the output. To manipulate the decimal places displayed in the output, the fixed keyword should be used prior to utilizing setprecision().
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