×

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

Virtual base class in C++

Consider in a C++ program, there are 4 classes named class A, class B, class C, and class D. If class B and class c inherit properties from class A....

3 minutes read.

List back () function in C++ STL

The list::back () function of the C++ STL returns a direct reference to the last element in the list container. This function varies from list::end (), which just returns an...

2 minutes read.

Array of Vectors in C++ STL

Prerequisites: C++ STL Arrays and C++ STL Vector. A group of items kept in consecutive memory region is known as an array. It is to group similar objects of the same...

4 minutes read.

Initialize Vector in C++

Initialize Vector in C++  The following comparison operators are defined for vector and those are given below. ==, <, <=, !=, >,>=  This allows you to access the element of a vector using...

3 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++ String

A string is a collection of characters. C++ programming language supports both C string as well as standard C++ library string. In C++, string is an object of std::string class. C Style String The C style...

5 minutes read.

C++ Friend function

A friend function has the right to access all private and protected members of a class although it is defined outside that class' scope. Syntax class className{     ......     friend retyrn_type function_Name(argument);     .......   }   return_type function_Name(argument){     ......   } C++ friend function Example #include <iostream>   using namespace std;   class Length   {       private:           int meter;       public:           Length(): meter(5) { }           friend int addMethod(Length); //friend function declaration   };   int addMethod(Length l) // friend function definition   {       l.meter += 10; //accessing private data from non-member function       return l.meter;   }   int main()   {       Length L;       int totallength;       totallength=addMethod(L);       cout<<"Length: "<< totallength;       return 0;   } Output: Length: 15   C++ friend function...

1 minute read.

Type conversion in C++

Type conversion is the process of changing the type of variables in a program. The ultimate goal of type conversions is to allow variables of a single data type operate with...

7 minutes read.

fread() Function in C++ Programming

C++ language is used to make high-performance applications that can work efficiently, and it is one of the world's most popular languages. It is an object-oriented and high-level programming language;...

3 minutes read.

How to calculate size of string in C++

What is string in C++? In C++, a string is a sequence of characters. The string data type is part of the Standard Template Library (STL) and is defined in the...

4 minutes read.

Delete Operator in C++

Overview We can reserve allocation for a variable or an array at runtime in C++. Dynamic memory allocation is the term for this. After utilising a variable in C++, we must...

4 minutes read.

C++ Iterators

What are iterators ? Iterators are among the four foundations of the C++ Standard Template Library, also known as the STL. The memory address of the STL container classes is pointed...

15 minutes read.

Use of Inheritance in C++

What is inheritance in c++? Inheritance is fundamental in object-oriented programming (OOP) languages like C++. It allows a programmer to create a new class (called a derived class) that inherits the...

4 minutes read.

C++ Installation

Let's install C++ setup to start programming in C++. C++ setup contains C++ compiler which is required in your system. There are lots of C++ compilers available, you must choose...

1 minute read.

Iostream in C++

Using Iostream in C++, we can perform input and output operation capabilities. This represents input and output, and the stream is used to carry out this capability. A stream is...

4 minutes read.

How to find the length of the vector in C++

Like dynamic arrays, vectors can automatically adjust their size when an element is added or removed, and the container manages its storage. Because vector items are stored in contiguous storage, iterators...

3 minutes read.

Name Mangling and extern in C++

Name Mangling and Function Overloading: Function overloading is a feature offered by C++. As long as each function accepts various parameters, we can use this to write many functions with the...

4 minutes read.

Reverse a String using Stack C/C++

Reverse the given string using stack. To turn "tutorialandexample" into "elpmaxednalairotut," for instance. Here is a straightforward stack-based technique for reversing strings. Algorithm: 1) Make a stack that is empty. 2) Push each character...

3 minutes read.

Hexadecimal to Decimal in C++

In computers, hexadecimal numbers are represented with base 16 and decimal numbers are represented with base 10 and values 0-9, whereas hexadecimal numbers have digits ranging from 0 to 15,...

3 minutes read.

Pthreads or POSIX Threads in C++

The thread API for C/C++ is implemented by pthreads or POSIX threads. It enables the multithreading system, which enables parallel and distributed processing, and the creation of new concurrent process...

3 minutes read.