Javatpoint Logo
Javatpoint Logo

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:

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

Output:

Precision of Floating Point Numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())

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:

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

Output:

Precision of Floating Point Numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())

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:

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

Output:

Precision of Floating Point Numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())

Round() function:

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

Syntax:

It has the following syntax:

Example:

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

Output:

Precision of Floating Point Numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())

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:

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

Output:

Precision of Floating Point Numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())

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.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA