Skip to content

C# Half Type — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about C# Half Type. We cover key concepts, practical examples, and best practices.

You need to store thousands of floating-point values where 32-bit float wastes memory or 16-bit precision is sufficient (GPU data, ML models, audio samples). You could use float and waste space, or write a custom 16-bit float. .NET provides Half — an IEEE 754 16-bit floating-point type.

Wrong

// Using float for half-precision data
float[] samples = new float[1_000_000];
// 4 MB of memory for data that only needs 2 MB
// No validation that values fit in half precision

Output: Works, but uses twice the necessary memory.

Half[] samples = new Half[1_000_000];
samples[0] = (Half)3.14f;
samples[1] = Half.Parse("2.718");
Console.WriteLine(samples[0]); // 3.14

Output: 3.14 — stored in 2 bytes instead of 4.

Half follows IEEE 754 with 1 sign bit, 5 exponent bits, and 10 mantissa bits. Range is approximately ±65,504 with ~3.3 decimal digits of precision.

// Conversion
float f = (float)samples[0];
Half h = (Half)3.14159f; // Rounds to 3.14

// Math
Half a = (Half)1.5;
Half b = (Half)2.5;
Half c = (Half)((float)a + (float)b); // No direct arithmetic

Prevention

  • Use Half for large arrays of floating-point data when 16-bit precision is sufficient.
  • Use Half in machine learning model weights, audio samples, and GPU interop.
  • Use float or double when arithmetic precision matters — Half has limited precision.
  • Convert to float before arithmetic operations — Half does not support direct arithmetic operators.
  • Use Half with Span<Half> and Memory<Half> for efficient buffer management.
  • Use Half.MaxValue / Half.MinValue / Half.Epsilon constants.

Common Mistakes with half type

  1. Mixing let bindings with <- bindings in do notation, producing type errors
  2. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  3. Non-exhaustive pattern matches that compile with warnings then crash at runtime

These mistakes appear frequently in real-world CSHARP code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

Does Half support arithmetic operators?

No. Half does not define +, -, *, / operators. Convert to float or double first, perform arithmetic, then convert back. This is by design — 16-bit arithmetic would lose precision anyway.

What is Half used for?

Audio processing (16-bit PCM samples), GPU data (DXIL, SPIR-V), machine learning model weights, color values in graphics, and sensor data. Any scenario where memory is constrained and 16-bit precision is acceptable.

Can Half represent all 16-bit integer values exactly?

No. Half has 10 mantissa bits, so it can represent integers up to 2048 exactly. Beyond that, precision drops. Use short or ushort for exact 16-bit integer storage.

Half is used in Durga Antivirus Pro for audio signature matching. For more .NET numeric types, visit DodaTech.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro