C++ float Keyword
The float keyword in C++ is used to declare variables of type single-precision floating-point. It is a built-in data type that can store decimal numbers with less precision compared to the double type but occupies less memory.
The size of a float typically depends on the system and compiler but is generally 4 bytes (32 bits) on most modern systems. It is commonly used in applications where memory efficiency is more critical than precision.
Syntax
</>
Copy
float variable_name = value;
- float
- The keyword used to declare a single-precision floating-point variable.
- variable_name
- The name of the variable being declared.
- value
- An optional initial value for the variable. It can be a decimal number or an integer.
Examples
Example 1: Declaring and Initializing a float Variable
In this example, we will learn how to declare and initialize a float variable and display its value.
</>
Copy
#include <iostream>
using namespace std;
int main() {
float pi = 3.14f; // Declare and initialize a float variable
cout << "Value of pi: " << pi << endl;
return 0;
}
Output:
Value of pi: 3.14
Explanation:
- The variable
piis declared as afloatand initialized with the value3.14f. - The
fsuffix indicates that the literal is of typefloat. - The
coutstatement prints the value ofpito the console.
Example 2: Performing Arithmetic Operations with float
In this example, we will learn how to do arithmetic operations using float variables.
</>
Copy
#include <iostream>
using namespace std;
int main() {
float num1 = 5.5f, num2 = 2.2f;
float sum = num1 + num2;
float product = num1 * num2;
cout << "Sum: " << sum << endl;
cout << "Product: " << product << endl;
return 0;
}
Output:
Sum: 7.7
Product: 12.1
Explanation:
- The variables
num1andnum2are declared asfloatand initialized with decimal values. - Arithmetic operations are performed, and the results are stored in
sumandproduct. - The results are printed using
cout.
Example 3: Comparing Precision of float and double
In this example, we will learn about the precision difference between float and double types.
</>
Copy
#include <iostream>
#include <iomanip> // For std::setprecision
using namespace std;
int main() {
float floatVal = 3.14159265358979323846f; // Single-precision floating-point
double doubleVal = 3.14159265358979323846; // Double-precision floating-point
cout << fixed << setprecision(20);
cout << "Float value: " << floatVal << endl;
cout << "Double value: " << doubleVal << endl;
return 0;
}
Output:
Float value: 3.14159274101257324219
Double value: 3.14159265358979311600
Explanation:
- The
floattype has less precision, so its representation deviates after 6-7 decimal places. - The
doubletype has higher precision, accurately representing values up to 15-16 decimal places. - The output shows the difference in precision between the two types.
Key Points about float Keyword
- The
floatkeyword is used to declare single-precision floating-point variables. - A
floatvariable typically occupies 4 bytes (32 bits) in memory. - The precision of
floatis less than that ofdouble, making it suitable for memory-constrained applications. - Use the
fsuffix for float literals to explicitly specify their type. - When precision is critical, consider using
doubleinstead offloat.
