> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C float

The `float` keyword in C designates a single-precision floating-point scalar data type used to represent real numbers with fractional components. The C standard does not strictly mandate a specific binary representation; however, most modern compilers implement `float` using the IEEE 754 (IEC 60559) 32-bit base-2 format. Strict adherence to this standard is implementation-defined and can be verified at compile time via the `__STDC_IEC_559__` macro.

## Technical Specifications (Assuming IEEE 754)

* **Memory Size:** Typically 4 bytes (32 bits).
* **Precision:** 6 to 7 significant decimal digits.
* **Value Range:** Approximately $\pm 1.18 \times 10^{-38}$ to $\pm 3.4 \times 10^{38}$.
* **Header:** Implementation-specific limits and properties are defined in `<float.h>`.

## Syntax and Literals

By default, floating-point literals in C are of type `double`. To explicitly declare a `float` literal, you must append the `f` or `F` suffix. Omitting the suffix results in an implicit conversion (or demotion) from `double` to `float`. This demotion can incur a loss of precision, as the value is rounded to the nearest representable `float` value based on the active rounding mode.

```c theme={"dark"}
float uninitialized_var; 
float pi = 3.14159f;         // Standard decimal notation with 'f' suffix
float planck = 6.626e-34f;   // Scientific notation (e or E) with 'f' suffix

// Implicit conversion (demotion from double to float)
float implicit_conversion = 2.718; // 2.718 is a double literal
```

## Memory Representation

When implemented as an IEEE 754 32-bit single-precision float, the memory is divided into three distinct bit fields:

1. **Sign Bit (1 bit):** Bit 31. Determines if the number is positive (`0`) or negative (`1`).
2. **Exponent (8 bits):** Bits 30–23. Uses an offset binary representation (biased by 127) to represent the power of 2.
3. **Mantissa / Fraction (23 bits):** Bits 22–0. Represents the significant digits. In normalized numbers, a leading `1` is assumed and not stored, providing an effective 24 bits of precision.

## Equality Comparison and Precision Limits

Due to the inherent precision limits and rounding errors of floating-point arithmetic, direct equality comparisons using the `==` operator are highly unreliable. Two mathematically equivalent calculations may yield slightly different binary representations.

Because the distance between representable floating-point numbers scales with their magnitude, using a fixed absolute tolerance (such as `FLT_EPSILON`) is a well-known anti-pattern for values significantly larger or smaller than 1.0. For instance, the gap between adjacent floats exceeds `FLT_EPSILON` for values greater than 2.0, causing absolute comparisons to incorrectly evaluate to `false` for mathematically equivalent calculations.

Robust comparisons require checking the *relative* difference, scaling the epsilon tolerance by the magnitude of the operands, while falling back to an absolute check for values near zero.

```c theme={"dark"}
#include <math.h>
#include <float.h>
#include <stdbool.h>

bool is_equal(float a, float b) {
    float diff = fabsf(a - b);
    
    // Absolute tolerance check for exact equality or values extremely close to zero
    if (diff <= FLT_EPSILON) {
        return true;
    }
    
    // Relative tolerance check for larger magnitudes
    float abs_a = fabsf(a);
    float abs_b = fabsf(b);
    float largest = (abs_a > abs_b) ? abs_a : abs_b;
    
    // Scale the epsilon tolerance by the largest operand magnitude
    return diff <= largest * FLT_EPSILON; 
}

int main() {
    float sum = 0.1f + 0.2f;
    
    // sum == 0.3f may evaluate to false due to rounding errors
    // is_equal(sum, 0.3f) evaluates to true
    
    return 0;
}
```

## Format Specifiers and Type Promotion

When passing a `float` to a variadic function like `printf`, C applies default argument promotions, implicitly converting the `float` to a `double`. Consequently, the format specifiers `%f`, `%e`, and `%g` in `printf` actually expect and consume a `double`.

Conversely, `scanf` requires exact pointer types because no promotion occurs for pointers. The `%f` specifier in `scanf` strictly requires a `float*`.

```c theme={"dark"}
#include <stdio.h>

int main() {
    // Using 12.5f, which has an exact binary representation, 
    // to avoid demonstrating unintended precision loss artifacts.
    float val = 12.5f;
    
    // 'val' is implicitly promoted to 'double' via default argument promotion
    printf("%f\n", val);  // Decimal notation: 12.500000
    printf("%e\n", val);  // Scientific notation: 1.250000e+01
    printf("%g\n", val);  // Shortest representation: 12.5
    
    // scanf requires a pointer to float; no promotion occurs
    scanf("%f", &val);    
    
    return 0;
}
```

## Standard Macros (`<float.h>`)

The C standard library provides macros to query the architectural limits of the `float` type:

```c theme={"dark"}
#include <float.h>

float max_val = FLT_MAX;         // Maximum finite representable value
float min_val = FLT_MIN;         // Minimum normalized positive value
float epsilon = FLT_EPSILON;     // Difference between 1.0 and the next representable value
int precision_digits = FLT_DIG;  // Number of decimal digits of guaranteed precision
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
