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.

The float keyword in C# represents a single-precision, 32-bit floating-point value type. It is an alias for the .NET System.Single struct and conforms to the IEEE 754 standard for binary floating-point arithmetic.

Technical Specifications

  • Underlying Type: System.Single
  • Memory Size: 4 bytes (32 bits)
  • Precision: ~6 to 9 significant decimal digits
  • Approximate Range: ±1.5 x 10^−45 to ±3.4 x 10^38
  • Default Value: 0.0f

Memory Layout

The 32 bits of a float are divided into three components according to IEEE 754:
  1. Sign bit: 1 bit (0 for positive, 1 for negative)
  2. Exponent: 8 bits (biased by 127)
  3. Mantissa (Significand): 23 bits (representing the fractional part)

Syntax and Initialization

By default, a real numeric literal on the right side of an assignment is treated as a double. To initialize a float, you must append the literal suffix f or F. Omitting the suffix results in a compiler error due to the lack of an implicit conversion from double to float.
// Correct initialization using the 'f' suffix
float standardValue = 3.14f;
float negativeValue = -0.005F;

// Scientific notation
float scientificValue = 1.2e-5f; 

// Default initialization (evaluates to 0.0f)
float defaultValue = default;

Special Values

The System.Single struct provides constants to represent special floating-point states that occur during undefined or overflow arithmetic operations.
float zero = 0.0f;

// Evaluates to float.PositiveInfinity
float positiveInfinity = 1.0f / zero; 

// Evaluates to float.NegativeInfinity
float negativeInfinity = -1.0f / zero; 

// Evaluates to float.NaN (Not a Number)
float notANumber = zero / zero; 

// The smallest positive value greater than zero (approx 1.4e-45)
float epsilonValue = float.Epsilon; 

Type Conversions

Implicit Conversions: C# allows implicit conversions to float from any integral type (sbyte, byte, short, ushort, int, uint, long, ulong, and char). Note that converting from 32-bit or 64-bit integers (int, long) to float may result in a loss of precision, though the magnitude is preserved.
int integerValue = 16777217;
float implicitlyConverted = integerValue; 
// implicitlyConverted evaluates to 16777216.0f due to precision limits
Explicit Conversions: Conversions from higher-precision floating-point types (double, decimal) to float require an explicit cast. This can result in overflow (yielding infinity) or a loss of precision.
double doubleValue = 3.14159265358979;
float explicitlyCast = (float)doubleValue; 
// explicitlyCast evaluates to 3.1415927f
Master C# with Deep Grasping Methodology!Learn More