> ## 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.

# Kotlin Float

A `Float` in Kotlin is a basic type representing a single-precision 32-bit IEEE 754 floating-point number. At the language level, all types in Kotlin are objects (classes), but the compiler optimizes `Float` to a JVM primitive (`float`) whenever possible to eliminate memory overhead. It allocates 4 bytes of memory and provides a precision of 6 to 7 significant decimal digits.

## Technical Specifications

* **Bit Width:** 32 bits
* **Maximum Value:** `3.4028235E38` (`Float.MAX_VALUE`)
* **Minimum Value:** `-3.4028235E38` (`-Float.MAX_VALUE`)
* **Smallest Positive Non-Zero Value:** `1.4E-45` (`Float.MIN_VALUE`)
* **Default Type Inference:** By default, Kotlin infers fractional literals as `Double`. To explicitly define a `Float`, a suffix is required.

## Syntax and Instantiation

To declare a `Float` literal, you must append the `f` or `F` suffix to the numeric value. Without this suffix, the compiler treats the literal as a 64-bit `Double`, which results in a type mismatch error if explicitly typed as `Float`.

```kotlin theme={"dark"}
// Explicit type declaration with suffix
val pi: Float = 3.14159f

// Inferred type using suffix
val gravity = 9.81F 

// Scientific notation
val planckLength = 1.616e-35f 

// Compilation Error: The literal is a Double, but the variable expects a Float
// val invalidFloat: Float = 3.14 
```

## Type Conversion

Kotlin enforces strict type safety and does not support implicit widening conversions. You cannot assign an `Int`, `Long`, or `Double` directly to a `Float` variable. Explicit conversion functions must be invoked.

```kotlin theme={"dark"}
val integerValue: Int = 42
val doubleValue: Double = 3.1415926535

// Explicit conversions
val fromInt: Float = integerValue.toFloat()
val fromDouble: Float = doubleValue.toFloat() // Precision loss occurs here
```

## Memory Representation and Boxing

On the JVM, Kotlin represents a `Float` as a primitive `float` when the variable is non-nullable and not used within a generic context. If the `Float` is declared as nullable (`Float?`) or used inside a generic type (such as `List<Float>`), the compiler boxes the value into a `java.lang.Float` object.

```kotlin theme={"dark"}
val primitive: Float = 1.5f      // Compiled to JVM primitive 'float'
val boxed: Float? = 1.5f         // Compiled to JVM object 'java.lang.Float'
val generic: List<Float> = listOf(1.5f) // Boxed inside the collection
```

## Special IEEE 754 Values

Kotlin's `Float` implementation fully supports standard IEEE 754 special values for handling arithmetic edge cases:

```kotlin theme={"dark"}
val positiveInfinity: Float = Float.POSITIVE_INFINITY // e.g., 1.0f / 0.0f
val negativeInfinity: Float = Float.NEGATIVE_INFINITY // e.g., -1.0f / 0.0f
val notANumber: Float = Float.NaN                     // e.g., 0.0f / 0.0f
```

## Equality and Comparison

Kotlin evaluates floating-point equality differently depending on the static type of the operands.

When operands are statically typed as `Float` or `Float?`, the `==` operator compiles to a primitive IEEE 754 comparison (with a null check for `Float?`). Under IEEE 754 rules, `NaN` is never equal to itself, and `0.0f` is equal to `-0.0f`.

When operands are statically typed as `Any`, `Comparable`, a generic type parameter, or when explicitly invoking the `.equals()` method, Kotlin applies **total ordering** behavior. Under total ordering, `NaN` is considered equal to itself and greater than any other value, while `0.0f` is considered strictly greater than `-0.0f`.

```kotlin theme={"dark"}
val a: Float = Float.NaN
val b: Float = Float.NaN

// IEEE 754 comparison applies to Float
println(a == b) // false

// IEEE 754 comparison still applies to Float?
val nullableA: Float? = Float.NaN
val nullableB: Float? = Float.NaN
println(nullableA == nullableB) // false

// Total ordering applies when statically typed as Any
val anyA: Any = Float.NaN
val anyB: Any = Float.NaN
println(anyA == anyB) // true

// Total ordering applies when explicitly calling equals()
println(a.equals(b)) // true
```

<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 Kotlin 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>
