C++ Float: 7 Examples to Learn

C++ Float data type

  • The float is one of the available data types in C++.
  • This is used to store floating point numbers in variables.
  • It’s a numeric literal – in the exponent or fractional form. For example, 10.5.
  • The suffix f or F (capital or small letter) is used at the end of the floating value.
  • If no suffix is provided, the C++ compiler takes it as the Double type value.
  • The float takes four bytes (or 32 bits) in the memory. The Double data type takes eight bytes (or 64 bits)
  • Min Value = -3.4 E+38 (may differ)
  • Max value = +3.4 E+38 (may differ)
  • Its precision is 7 decimal digits, generally.

An example of declaring a float data type variable

In the first example of float data type, we have declared four variables. Have a look:

#include<iostream>
#include<cstdlib>
using namespace std;

int main(){


// Declaring and assigning float variables

float f1 = 55.0f;
float f2 = -55.5f;
float f3 = 99.994524f;
float f4 = 4E-6f; //An Exponential Number

//Displaying float variable values

 cout << "First Float Variable Value  = " << f1 << endl;

 cout << "Second Float Variable Value  = " << f2 << endl;

 cout << "Third Float Variable Value  = " << f3 << endl;

 cout << "Fourth Float Variable Value  = " << f4 << endl;

                return 1;
}

Output:

CPP-float

Using sizeof() operator to check memory float takes

We have five variables in this example. The last one is a double type. We will display the size of each variable (the memory it takes) by using the sizeof() operator:

#include<iostream>
#include<cstdlib>
using namespace std;

int main(){

// Declaring and assigning float variables and a double
float f1 = 55.0f;
float f2 = -55.5f;
float f3 = 99.994524f;
float f4 = 4E-6f; //An Exponential Number
double f5 = 55.0;

//Displaying variable size

 cout << "Size of First Float Variable  = " << sizeof(f1) << endl;

 cout << "Size of Second Float Variable  = " << sizeof(f2) << endl;

 cout << "Size of Third Float Variable  = " << sizeof(f3) << endl;

 cout << "Size of Fourth Float Variable  = " << sizeof(f4) << endl;

 cout << "Size of Fifth Double Variable  = " << sizeof(f5) << endl;
return 1;

}

Output:

CPP-float-size

You can see, the double variable type shows 8 while float type variables display 4 size (in byets).

What if the float is assigned to an int type variable

Suppose, we have a float type variable with a value in decimal points. We assign its value to another variable of int type. See what it displays:

#include<iostream>
#include<cstdlib>
using namespace std;

int main(){

// creating variable
float num1 = 55.55f;
int x;
x = num1;

//Displaying variable values

 cout << "Float variable value  = " << num1 << endl;

 cout << "Int variable value  = " << x << endl;

                return 1;

}

Output:

CPP-float-int

So, the result is not an error. In fact, int type variable yielded a truncated value and this is before the decimal part.

Comparing float with double and long double variables

Let us assign values with big precision to float, double, and long double variables (Since all can store floating-point values).

#include<iostream>
#include<cstdlib>
using namespace std;

int main(){

// Creating variables
float f1 = 5.4514247545414f;
double f2 = 5.4514247545414;
long double f3 = 5.4514247545414L;

//Displaying variable size

 cout << "Value of Float Variable  = " << f1 << endl;

 cout << "Value of Double Variable  = " << f2 << endl;

 cout << "Value of Long Double Variable  = " << f3 << endl;

                return 1;

}

Output:

CPP-float--double-long

Our compiler allows six digits, so it outputs the same. The result may vary depending on the compiler.

So what about all three variable sizes in bytes?

#include<iostream>
#include<cstdlib>
using namespace std;

int main(){

// Creating variables
float f1 = 5.4514247545414;
double f2 = 5.4514247545414;
long double f3 = 5.4514247545414L;

//Displaying variable size
 cout << "Size of Float Variable  = " << sizeof(f1) << endl;
 cout << "Size of Double Variable  = " << sizeof(f2) << endl;
 cout << "Size of Long Double Variable  = " << sizeof(f3) << endl;

                return 1;

}

CPP-f--double-long-size

So, it’s 4 for float, 8 for double, and 16 for the long double variable.

Note that, a long double can also take 12 bytes – it depends on the operating system.

Setting precision by setprecision() function example

As we have seen in the above example, the compiler allowed only six digits. We can set the precision by using the setprecision() function.

This is defined in the iomanip header file that you have to include in the CPP file, in order to use it.

In the example, we have used the same values for float, double and long double variables.

The precision is set as 11 by using the setprecision function.

See the code and output:

#include<iostream>
#include <iomanip>
#include<cstdlib>
using namespace std;

int main(){

// Creating variables
float f1 = 5.4514247545f;
double f2 = 5.4514247545;
long double f3 = 5.4514247545L;

    cout << setprecision(11);

//Displaying variable size

 cout << "Value of Float Variable  = " << f1 << endl;

 cout << "Value of Double Variable  = " << f2 << endl;

 cout << "Value of Long Double Variable  = " << f3 << endl;

                return 1;

}

The output:

CPP-float-precision

  • From the output, you can see the value of double and long double is displayed as intended. However, the float value is different. The reason is, float precision is 7 – so till 7 digits, it showed the correct value while the remaining four digits are garbage values.
  • FYI, the double has a 15 to 18 precision value.
  • Similarly, the long double has a minimum of 15, 18, or 33 significant digits.

A program that shows Min and Max limits from floats and other data types

In the C++ program below, the limits of a few data types are displayed. If you execute this code, the value may vary to the system where you are running.

These data types include:

  • Float
  • Int
  • Short
  • Double
  • Long double
#include <iostream>
#include <limits>

using namespace std;

int main(){
   cout << "short:" << endl;
   cout << "min: " << numeric_limits<int>::min() << endl;
   cout << "max: " << numeric_limits<int>::max() << "\n\n";

   cout << "float:" << endl;
   cout << "min: " << numeric_limits<float>::min() << endl;
   cout << "max: " << numeric_limits<float>::max() << "\n\n";

   cout << "Double:" << endl;
   cout << "min: " << numeric_limits<double>::min() << endl;
   cout << "max: " << numeric_limits<double>::max() << "\n\n";

   cout << "Long Double:" << endl;
   cout << "min: " << numeric_limits<long double>::min() << endl;
   cout << "max: " << numeric_limits<long double>::max() << "\n\n";


   cout << "unsigned short:" << endl;
   cout << "min: " << numeric_limits<unsigned short>::min() << endl;
   cout << "max: " << numeric_limits<unsigned short>::max() << "\n\n";


   cout << "unsigned int:" << endl;
   cout << "min: " << numeric_limits<unsigned int>::min() << endl;
   cout << "max: " << numeric_limits<unsigned int>::max() << endl;
      return 1;

}

Output:

CPP-float-limits

 

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!