Increasing Precision With Setprecision in C++

# Increasing Precision With Setprecision in C++

4 mins read1.1K Views Comment
Updated on Feb 18, 2023 23:03 IST

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.

## 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.

## Syntax of Setprecision C++ Function

The syntax of the setprecision function from the iomanip library in C++ is as follows:

` `
`setprecision(int n);Copy code`

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;Copy code`

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 <iostream>\n \n #include \n \n <iomanip>\n \n \n int main()\n {\n double value = 3.1415926535;\n std::cout << "Default precision: " << value << std::endl;\n std::cout << "Precision set to 3: " << std::setprecision(3) << value << std::endl;\n std::cout << "Precision set to 5: " << std::setprecision(5) << value << std::endl;\n return 0;\n }\n \n </iomanip>\n \n </iostream>Copy code`

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++:

1. 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.
2. 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.
3. 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.
4. 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.
5. 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 <iostream>\n \n #include \n \n <iomanip>\n \n \n int main() {\n double pi = 3.1415926535;\n std::cout << std::fixed << std::setprecision(2) << pi << std::endl;\n return 0;\n }\n \n </iomanip>\n \n </iostream>Copy code`

Output:

` `
`3.14Copy code`

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 <iostream>\n \n #include \n \n <iomanip>\n \n \n int main() {\n double x = 123.456;\n double y = 789.123;\n double result = x + y;\n \n std::cout << std::setprecision(10) << "x = " << x << std::endl;\n std::cout << std::setprecision(10) << "y = " << y << std::endl;\n std::cout << std::setprecision(10) << "result = " << result << std::endl;\n return 0;\n }\n \n </iomanip>\n \n </iostream>Copy code`

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.579000000Copy code`

Example 3: Here’s an example that demonstrates the default precision, maximum precision, and setting the precision using setprecision() to 5 digits:

` `
`#include \n <iostream>\n \n #include \n \n <iomanip>\n \n #include \n \n <limits>\n \n \n int main() {\n double x = 123.456;\n \n // default precision\n std::cout << "Default precision: " << x << std::endl;\n \n // maximum precision\n std::cout << "Maximum: " << std::setprecision(std::numeric_limits\n \n <double>\n ::digits10 + 1) << x << std::endl;\n \n // setting precision to 5 digits\n std::cout << "Precision set to 5 digits: " << std::setprecision(5) << x << std::endl;\n return 0;\n }\n \n </double>\n \n </limits>\n \n </iomanip>\n \n </iostream>Copy code`

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.46Copy code`

### 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().