Floating point data type is a built-in data type in C++ like the integer data type.This type like the integer data type can represent a number,but with fraction.So floating point data type can represent a real number.In this post we shall discuss the different types of floating point data and their differences among them.
In the program below one of the floating point data types :float data type is utilized to show that floating point type can represent a decimal point value.Note there are two more floating point data type which we will discuss later.
In case of int variable(i) the fractional value is neglected.However,for float variable up to four decimal point value is taken into account.Since int type does not support fractional value,when a floating point value is assigned to integer data type variable the fractional value is lost,the example above proves it.Another example is given below.
The fractional value is preserve in case of float variable(f2) but for int variable(i2) it is lost.So,it is always better to use floating point data type in a program involving mathematical evaluation.
The difference between the three types is shown later in this post.Unlike the int type,floating point type cannot be further divided into signed and unsigned type.So type like signed float or unsigned double is invalid.
When very large or very small value is assigned to floating point data type variable the resulting output is in power to base 10 format.The program below demonstrates this concept.
In the above output ‘e‘ stands for 10.Since floating point type is represented in power to base 10 format it allows this type to represent a number as small as 3.3621e-4932 and a number as large as 1.18973e+4932.You will see later the ranges of value different floating point types can represent.
The difference between float , double and long double is characterized mainly by the different storage size each type allocate in the memory and also the number of precision value they can represent.
Double can represent a double precision value comparing to float type while long double can represent more precision value.
Float has a size of 4 bytes ,double has a size of 8 bytes and long double has a size of 12 bytes.
A program below prints out their sizes and the minimum and maximum value they can represent.Include the library <cfloat> to get the minimum and maximum value of each type.
long double value range from 3.3621e-4932 to 1.18973e+4932.
The range of value obtained from the program is only for positive value this does not mean floating point type cannot represent a negative value,yes! they can.The absolute value(meaning the magnitude) of the range of negative value each floating point type can hold is same as that with the absolute value of the +ve range value.The negative range is shown below.
for long double the range is -3.3621e-4932 to -1.18973e+4932 .
Since long double has the largest size it can represent the smallest and largest value among all the floating point type.
We have seen what a floating point data type has in store for us,and also we have seen the different floating point data type and their range of values.No doubt,this type is more preferable over the integer data type due to the preservation of the fractional value,which other wise would not yield an accurate value.
Why you should prefer double over any other floating point data type is a post worth checking out!!