Float in C
The Floating point numbers are represented by the datatype float. It is a 32-bit IEEE 754 single precision floating point integer (sign is 1 bit, exponent is 8 bits, and value is 23* bits). The precision is six decimal digits.
Programming languages like C heavily rely on floating-point numbers. They make it possible to represent decimal numbers with fractional components. Such numbers are handled by floats in C, enabling a variety of numerical computations.
Floating-Point Representation:
In this representation:
- The sign bit determines whether the number is positive or negative.
- The exponent denotes the scale or magnitude of the number.
- The fraction represents the significant digits that make up the decimal portion.
Declaration and Initialization:
We need to declare and initialize float variables to use them.
float float_var; // Declaration of float variable float_var = 1.23; // Initialization of float variable You can also declare and initialize a float variable in a single line: float float_var = 1.23; // Declaration and initialization
Program to declare and initialize float variables
#include<stdio.h> int main () { float var; var = 10.00; printf ("The rate of interest is %f \n," var); return 0; }
Output:
A float variable named "var" is declared.
It gives "var" the value of 10.00. The format the specifier "%f" is used in conjunction with the printf() function to print the value of "var" as the float with six decimal places.
The program returns 0, indicating that it has run successfully.
Arithmetic Operations:
We can perform various arithmetic operations float type variables like other numerical data types in the C.
float number_1 = 2.5; float number_2 ; number_2 = 1.3; float addition; addition = number_1 + number_2; float difference; difference = number_1 - number_2; float product =number_1 * number_2; float quotient =number_1 / number_2;
Type Casting:
C provides typecasting to facilitate such conversions. Implicit type casting occurs automatically, while explicit type casting requires explicit notation.
Example:
float myFloat = 3.14; int myInt = (int)myfloat;
// Explicitly casting a float to int that is converting float data type to int data type
Comparisons and Conditional Statements:
Precision limitations need to be considered when comparing float values due to the nature of floating-point representation. Comparing floats for equality is generally not recommended due to potential rounding errors. Instead, it is better to compare them within a range.
float number_1 =10.1; float number_2 =10.2; if ((number_1 - number_2)<0.0001) { // The values are considered equal } else { // The values are not equal }
Input and Output:
To take input and print the float values output in C, scanf() for input and printf() for output.
For example:
#include <stdio.h> Int main () { float myFloat; printf ("Enter a float value: "); scanf("%f", &myFloat); printf ("The float value entered is: %f\n," myFloat); return 0; }
Output:
Floats in C provide a flexible way to handle decimal numbers with fractional parts. Understanding their representation, declaration, initialization, arithmetic operations, type casting, comparisons, and input/output operations is essential for practical numerical computations in C. While working with floats, it's important to consider the precision limitations and potential rounding errors.
Advantages:
The float data type in C offers several advantages that make it valuable in specific scenarios:
- Precision and Range: The float data type allows for representing a wide range of decimal numbers with reasonable precision. It provides a compromise between storage efficiency and numerical accuracy. Floats can handle values with a range of approximately ±1.18 × 10^-38 to ±3.4 × 10^38, depending on the implementation.
- Memory Efficiency: Floats occupy less memory than double-precision (double) floats, making them suitable when storage efficiency is crucial. If a higher degree of precision is not required for a specific application, using float instead of double can conserve memory.
Disadvantage:
While the float data type in C offers advantages, it also has some limitations and disadvantages:
- Limited Precision: Floats have limited precision compared to double-precision (double) floats. They typically provide around 6-7 decimal digits of precision. As a result, calculations involving floats can introduce rounding errors, especially when performing repeated or complex operations. This limitation can impact the accuracy of specific applications, such as financial calculations or scientific simulations, that require high precision.
- Loss of Significant Digits: Floats are subject to a loss of significant digits during calculations. This loss occurs due to the finite precision of the representation, leading to inaccuracies in the result. Accumulation of these inaccuracies can propagate and affect the overall accuracy of computations.