Precision Of Floating Point Numbers In C++ Floor Ceil Trunc Round And Setprecision

In C++, floating point numbers are represented by the float, double, and long double data types, which are used to approximate real numbers with a real number with a decimal point. The float type typically uses 32 bits, double uses 64 bits, and long double may use 64, 80 or more bits, depending on the platform.

The C++ language will provide the "set precision" manipulator from the <iomaniop> concerns in output formatting. When printing floating-point values to the console or a file, it controls the number of digits displayed after the decimal point. Developers working on applications where precise numerical accuracy is paramount might explore alternative approaches, such as using fixed-point arithmetic or specialized numerical libraries.

Floor function:

This floor function in C++ returns the largest integer less than or equal to a given floating point number.

Syntax:

It has the following syntax:

Example

Double floor(double x);

Example:

Let us take a C++ program to illustrate the floor function.

Example

#include <iostream>
#include <cmath>
using namespace std;
int main() {
 double number = 8.75;
 double result = floor(number);
 cout << "Original number: " << number << endl;
 cout << "Floor value: " << result << endl;
 return 0;
}

Output:

Explanation:

The Program uses the floor function from the <cmath> library to find the largest integer less than or equal to the given floating-point number, 8.75. The result, displayed as "Floor value" , is 8, showcasing how the function truncates the decimal part, providing the nearest whole number towards negative infinity.

Ceil function:

The ceil function in C++ returns the smallest integer greater than or equal to a given floating point number

Syntax:

It has the following syntax:

Example

Double ceil(double x)

Example:

Let us take a program to illustrate the ceil function in C++.

Example

#include <iostream>
#include <cmath>
using namespace std;
int main() {
 double number = 8.75;
 double result = ceil(number);
 cout << "Original number: " << number << endl;
 cout << "ceil value: " << result << endl;
 return 0;
}

Output:

Explanation:

The Program utilizes the ceil function from the <cmath> library to find the smallest integer greater than or equal to the given floating-point number, 8.75. The result, displayed as "ceil value" , is 9, showcasing how the function rounds the number up to the nearest whole number.

Trunc function:

The Trunc function in C++ returns the integral part of a given floating point number by truncating towards zero.

Syntax:

It has the following syntax:

Example

Double trunc(double x)

Example:

Let us take a program to illustrate the trunc function in C++.

Example

#include <iostream>
#include <cmath>
using namespace std;
int main() {
 double number = -8.75;
 double result = trunc(number);
 cout << "Original number: " << number << endl;
 cout << "Trunc value: " << result << endl;
 return 0;
}

Output:

Round function:

The Round function will rounds a given floating point number to the nearest integer.

Syntax:

It has the following syntax:

Example

Double round(double x)

Example:

Let us take a program to illustrate the round function in C++.

Example

#include <iostream>
#include <cmath>
using namespace std;
int main() {
 double number1 = 8.75;
 double result1 = round(number1);
 cout << "Original number1: " << number1 << endl;
 cout << "Rounded value1: " << result1 << endl;

 double number2 = 8.25;
 double result2 = round(number2);
 cout << "Original number2: " << number2 << endl;
 cout << "Rounded value2: " << result2 << endl;
 double number3 = 8.5;
 double result3 = round(number3);
 cout << "Original number3: " << number3 << endl;
 cout << "Rounded value3: " << result3 << endl;
 return 0;
}

Output:

Setprecision manipulator:

The setprecision manipulator in C++ sets the number of digits displayed after the decimal point when outputting floating-point numbers.

Syntax:

It has the following syntax:

Example

#include <iomanip>
std::ostream& setprecision(int n);

Example:

Let us take a program to illustrate the setprecision function in C++.

Example

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
 // Exam scores
 double exam1 = 89.542;
 double exam2 = 78.256;
 double exam3 = 95.731;
 // Calculate the average
 double average = (exam1 + exam2 + exam3) / 3.0;
 // Display the average with different levels of precision
 cout << "Original Average: " << average << endl;
 // Display with precision set to 2
 cout << fixed << setprecision(2);
 cout << "Average (2 decimal places): " << average << endl;
 // Display with precision set to 4
 cout << setprecision(4);
 cout << "Average (4 decimal places): " << average << endl;
 // Reset precision to default
 cout << setprecision(6);
 cout << "Average (default precision): " << average << endl;
 return 0;
}

Output:

Explanation:

The Program calculates the average of three exam scores and demonstrates precision control using set precision from <iomanip> . It initially displays the original average, and then sets precision to 2 and 4 decimal places, showcasing how the manipulator controls the number of digits after the decimal point. Finally, precision is reset to the default.

Conclusion:

In C++, floating-point numbers like float and double represent real numbers with varying precision. The setprecision manipulator from <iomanip> allows control over the decimal places when outputting these numbers. Functions like floor, ceil, trunc, and round help manage precision by rounding or truncating floating-point values. Despite these tools, developers must be aware of inherent approximations and consider alternatives like fixed-point arithmetic for critical applications where numerical accuracy is crucial. Understanding and addressing precision issues is vital for robust C++ programming.

Input Required

This code uses input(). Please provide values below: