Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++

1/2 decimal equal is 0.555555555555555555555 …. An indefinite number of lengths will require the storage of infinite memory, and we usually have 4 or 8 bytes. Floating point numbers thus only store a certain number of important numbers, and the remainder is missing. The accuracy of a floating-point number describes how often significant digits without loss of data it can show. Cout has a standard precision of 6 when extracting floating point, and serializes everything after.

There are few library and methods given below which are used to produce C++ floating point with precision:

ceil():

The ceil() rounds off of the value given to the nearest integer and more than the amount determined.

Example:

Output:

floor():

Floor rounds the specific value down to the nearest integer that is less than the specific amount.

Example:

Output:

round():

Rounds with numbers provided to the nearest integer.

Example:

Output:

trunc():

After decimal place, Trunc rounds erase digit numbers.

Example:

Output:

setprecision():

Especially when combined with ‘fixed’ set precision provides correct floating-point numbers to decimal numbers provided in setprecision brackets.

Example:

Output: