Sunday, 6 October 2013

Float Data Type

Float Data Type

The float is the second most commonly used data type used in C and allows you to store decimal numbers.
You can use float data types to store results of operations involving a division operation. Before we start with an example lets learn more about it.

Why is float 4 Bytes long ?

As you may remember int is only 2 Bytes long(for 16 bit CPU, 4 Bytes for 32 bit CPU). So what makes float so special to require twice the amount of space?

In a decimal number, its important to know the position of the decimal point. Consider this example:
129 < 1290  , but 1.29 = 1.29. Similarly 1.29 < 12.9.

The placement of the decimal point decides the value of the number and this is done in the following manner.
Lets consider 3.141592 as an example.

This number can be written as 3141592 x 10-5. This value is separated as 3141592 and -5.
3141592 is called the exponent, -5 the mantissa. The compiler then makes use of these values and encodes the float representation using the IEEE 754 standard.

As we need to store the information of two integers to represent a single float it requires twice the amount of space. and hence 4 Bytes of memory.

Lets continue with the example to know more about float.

    Author: Ryan Sequeira
    Title:  Float data type formatting

#include <stdio.h>

int main()
    float n = 22.0f, d = 7.0f;
    float pi = n/d;
    //print the value of pi
    printf("The value of Pi: %18f\n", pi);    
    //print the first 2 decimals of pi
    printf("Pi with first 2 decimals: %5.2f\n", pi); 
    //print the first 20 decimals of pi
    printf("Pi with first 20 decimals: %.20f\n", pi);
    /* float prints 6 decimal places by default 
    and gives a accuracy upto 20 decimal places. */
    //print the first 25 decimals of pi
    printf("Pi with first 25 decimals: %.25f\n", pi);
    return 0;

Lets  understand how formatting works with float.

float output for %5.2f
float output for %5.2f
We use %5.2f to print the above result. In this example 5 represents the number of places to reserve for the decimal and .2 represents the number of decimal digits to output.

The number before the decimal point ( in %5.2f ) represents minimum number of blocks to reserve for the value, and the number after the decimal decides the number of places to round off the decimal.

You can try this with integers too. But since there isn't a decimal here you can only use an integer value between % and d.

Example  printf("%5d",100); will reserve 5 blocks for the value 100. You can use this formatting option to right align your results.

Now that we have learnt a lot about float lets write a small program that converts temperature in Fahrenheit to Celsius and Kelvin.

    Author: Ryan Sequeira
    Title:  Program to convert temperature in
            Fahrenheit to Celsius and Kelvin

#include <stdio.h>

int main()
   //declaring variables to store temperatures 
   float far = 0.0f;
   float cel = 0.0f;
   float kel = 0.0f;
   printf("Enter the temperature (in Fahrenheits): ");
   //formulas to convert Fahrenheit to Celsius 
   cel = (far - 32) * (5.0f/9.0f);
   //formula to convert Fahrenheit to Kelvin
   kel = (far + 459.67f) * (5.0f/9.0f);
   printf("%.2f Fahrenheits equals \n %7.2f Celsius and \n %7.2f Kelvin\n", far, cel, kel);
   return 0;

Float in a nutshell
Description Used to store decimal values. They can be negative, zero and positive.
Examples -273.987, 76.0f, 0.00, 180.45, 3.1423
Size 4 Bytes (may change based on system)
Range -3.4e38 to +3.4e38
Syntax float variable ( = value) [, var1 = val1, ...] ;
Format Specifier %f

No comments:

Post a Comment