Skip to main content

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.

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.
// 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.
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.
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:
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.
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
Master Kotlin with Deep Grasping Methodology!Learn More