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:

  1. The variable pi is declared as a float and initialized with the value 3.14f.
  2. The f suffix indicates that the literal is of type float.
  3. The cout statement prints the value of pi to 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:

  1. The variables num1 and num2 are declared as float and initialized with decimal values.
  2. Arithmetic operations are performed, and the results are stored in sum and product.
  3. 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:

  1. The float type has less precision, so its representation deviates after 6-7 decimal places.
  2. The double type has higher precision, accurately representing values up to 15-16 decimal places.
  3. The output shows the difference in precision between the two types.

Key Points about float Keyword

  1. The float keyword is used to declare single-precision floating-point variables.
  2. A float variable typically occupies 4 bytes (32 bits) in memory.
  3. The precision of float is less than that of double, making it suitable for memory-constrained applications.
  4. Use the f suffix for float literals to explicitly specify their type.
  5. When precision is critical, consider using double instead of float.