×

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

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:

// C++ program to demonstrate working of ceil()
// in C/C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
    double a = 2.411, b = 2.500, c = 2.611;
    cout << ceil(a) << endl;
    cout << ceil(b) << endl;
    cout << ceil(c) << endl;
    double x1 = -4.411, y1 = -4.500, z1 = -4.611;
    cout << ceil(x1) << endl;
    cout << ceil(y1) << endl;
    cout << ceil(z1) << endl;
    return 0;
}

Output:

Precision of floating point numbers Using these functions floor()

floor():

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

Example:

/* C++ program to illustrate floor() function in C++*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
          double x0 =3.411, y0 =3.500, z0 =3.711;
          cout << floor(x0) << endl;
          cout << floor(y0) << endl;
          cout << floor(z0) << endl;
          double i01 =-3.411, j02 =-3.500, k03 =-3.611;
          cout << floor(i01) << endl;
          cout << floor(j02) << endl;
          cout << floor(k03) << endl;
          return 0;
}

Output:

Precision of floating point numbers Using these functions round()

round():

Rounds with numbers provided to the nearest integer.

Example:

// C++ program to demonstrate working of round()
// in C/C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
          double p1 = 4.411, p2 = 4.500, p3 = 4.611;
          cout << round(p1) << endl;
          cout << round(p2) << endl;
          cout << round(p3) << endl;
          double x1 = -4.411, x2 = -4.500, x3 = -4.611;
          cout << round(x1) << endl;
          cout << round(x2) << endl;
          cout << round(x3) << endl;
          return 0;
}

Output:

Precision of floating point numbers Using these functions floor()

trunc(): 

After decimal place, Trunc rounds erase digit numbers.

Example:

// C++ program to demonstrate working of trunc()
// in C/C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
          double y01 = 5.411, y02 = 5.500, y03 = 5.611;
          cout << trunc(y01) << endl;
          cout << trunc(y02) << endl;
          cout << trunc(y03) <<endl;
          double s01 = -5.411, s02 = -5.500, s03 = -5.611;
          cout << trunc(s01) <<endl;
          cout << trunc(s02) <<endl;
          cout << trunc(s03) <<endl;
          return 0;
}

Output:

Precision of floating point numbers Using these functions floor()

setprecision(): 

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

Example:

// C++ program to demonstrate working of setprecision()
// in C/C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
          double pi = 4.14159, npi = -4.14159;
          cout << fixed << setprecision(0) << pi <<" "<<npi<<endl;
          cout << fixed << setprecision(1) << pi <<" "<<npi<<endl;
          cout << fixed << setprecision(2) << pi <<" "<<npi<<endl;
          cout << fixed << setprecision(3) << pi <<" "<<npi<<endl;
          cout << fixed << setprecision(4) << pi <<" "<<npi<<endl;
          cout << fixed << setprecision(5) << pi <<" "<<npi<<endl;
          cout << fixed << setprecision(6) << pi <<" "<<npi<<endl;
          cout << fixed << setprecision(7) << pi <<" "<<npi<<endl;
          cout << fixed << setprecision(8) << pi <<" "<<npi<<endl;
          cout << fixed << setprecision(9) << pi <<" "<<npi<<endl;
          cout << fixed << setprecision(10) << pi <<" "<<npi<<endl;
          cout << fixed << setprecision(11) << pi <<" "<<npi<<endl;
          cout << fixed << setprecision(12) << pi <<" "<<npi<<endl;
          cout << fixed << setprecision(13) << pi <<" "<<npi<<endl;
}

Output:

Precision of floating point numbers Using these functions floor()

Related Topics

C++ For loop

C++ loop Statement C++ Loop statement allows us to repeat the execution of a statement or group of statements multiple times. The statement(s) repeat execution within loop until the condition of loop...

2 minutes read.

C++ Continue in Do-while loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the start...

4 minutes read.

C++ Comments

Comment/Remark A Comment or a Remark is text that is ignored by the compiler yet is beneficial to programmers. Code is usually annotated with comments for future reference. They are treated...

3 minutes read.

C++ Try-Catch

Every useful program will eventually encounter unexpected outcomes. By entering data that are incorrect, users might create mistakes. Sometimes the program's creator didn't consider all of the options or was...

7 minutes read.

strcat() vs strncat() in C++

In this tutorial, we will explore about strcat() and strncat() in the most usable language C++. We will also look at the difference between them. strcat() C++ is a computer language with...

4 minutes read.

How to create a directory or folder in C/C++?

A directory is to lists all files and subdirectories in a directory, set of the files will be kept in the directory, which is a location. A subdirectory is a...

5 minutes read.

Single Handling in C++

Introduction: Single handling in C++ refers to a technique for processing multiple events or requests with a single function or handler rather than creating separate functions for each task. This allows...

5 minutes read.

Structure of C++ Program

Many people believe that C++, an object-oriented programming (OOP) language, is the finest language for developing demanding applications. A superset of the C language is C++. Java, a closely comparable...

4 minutes read.

C++ Overloading

C++ Overloading is a condition when two or more members have the same name with different parameter type or a different number of parameter. C++ overloading is two types: Function...

1 minute read.

Dynamic Memory Allocation in C++

In some programming situations, the number of data items changes as the program is running, which is known as dynamic data or input. Consider a real-world situation where a program...

3 minutes read.

Templates in C++ vs Generics in Java

As the title suggests, there is no rivalry or there is no cut comparison between generics and templates in Java and C++, respectively. The main aim of this article is...

4 minutes read.

C++ Object Class

C++ Object Class Overview: C++ is a high-level programming language and an object-oriented programming language. An object-oriented language always has some properties of classes and objects. In this article, we...

4 minutes read.

C++ Pointer

Pointer is a derived data type that stores the address of a variable. A pointer is used for memory management and dynamic memory allocation. Pointer works on the address of data rather than...

2 minutes read.

Smart pointers in C++

What are Pointers ? Pointers are often used to keep track of a variable's address. A null value can be assigned to a pointer. Pass by reference can be used to...

6 minutes read.

Dynamic _Cast in C++

C++ is one of the most powerful programming languages. We can write object-oriented and structured programming with the help of C++. In this article, we will learn about the dynamic...

3 minutes read.

Exception Handling in C++ vs Java

Nowadays, exception handling is a feature found in virtually all object-oriented languages. We can also find this type of feature in Java and C++. The try-catch and block is required...

3 minutes read.

Constructor Vs Destructor in C++

C++ Constructor A function Object () { [native code] } is a member function that shares the same name as the class. It is called automatically whenever a class object is...

3 minutes read.

C++ String Concatenation

C++ String Concatenation In this section, we will learn about C ++ String Concatenation, what it does, how it works, and will also see its programs. What is the String Concatenation? The + operator...

3 minutes read.

C++ Program For FCFS (First Come First Serve)

The most basic scheduling technique is FCFS, often known as "FIFO (First In, First Out)". In this procedure, the first one is utilized and executed first, while the second one...

4 minutes read.

Division in C++

C++ Division Arithmetic Operation In C++ the arithmetic operator / is used for division. This operator takes two operands and returns the result of dividing the left operand by the right...

3 minutes read.