Fundamental data type in C.

Hello coder! Welcome back!

Today we going to look into fundamental data type- float, double, and long double. 
We have looked through integer data type in the previous blog, click here to view.

Float, double, long double are used to represent fractional numbers or real numbers.

Let's look at how much memory space these data types can hold.
Float → 4 bytes → 32 bits.
Double →8 bytes → 64 bits.
Long double → 12 bytes → 96 bits.

Note:- These sizes depend on the system. Vary from one system to another system  

There are two ways to represent fractional numbers 

  1. Fixed point representation. 
  2. Floating-point representation
Let's look into examples for the explanation. We are taking only 4bits to represent a factional number

let's start with fixed-point representation.


The minimum value it can hold is -7.77
The maximum value it can hold is +7.77.

Now let's look into floating-point representation-

The formula to calculate the number is→ (0.M)*base^exponent. 
where M is the mantissa
      we take base as 10 because we are representing a decimal number.
The minimum value it can hold is -(0.7)*10^+7
The maximum value it can hold is +(0.7)*10^+7

So what is the difference?

Fixed point representation doesn't give us a large range of precision. It can only print +7.77 or -7.77 
whereas floating-point representation gives us a large range of precision. It can print either -7000000 or +7000000. 
In both the representation we use only 4 bits, yet floating point gives us more precision, because of the formula. 

Hence machines use floating-point representation to represent fractional numbers

Let's look into the difference between each data type through a program. 

From the program we can see that size of float, double, long double is 4, 8, 12 bytes respectively. 
We can also notice the precision of each data type. Float gives 7 digit precision from the starting number. You can see the mark, from that decimal place the values are printed wrong. Similarly double gives 16 digit precision from starting number. And long double gives 19 digit precision.

If you want to perform division between two floating-point number it is mandatory to feed floating-point numbers as input only then we get the result in floating-point.

We can see that if we feed integer as input, it won't provide us the required result. 
It's mandatory to feed floating-point numbers as input, then only we get the required result. 

Always feed floating-point numbers as input while performing division. It fetches the exact result.

Hopes so you got a clear idea about data types that are used to represent fractional numbers and difference between them. If any doubts drop a comment. Always ready to clear them. 

Happy learning!

Comments

Popular post