Precision of Floating-Point Data Types

This section provides a tutorial example on how to compare precisions of floating-point data types: 'float', 'double', and 'decimal'

Before looking at the output of presented in this section, please take a guess at final values of these expressions:

If you are an entry level programmer, you may guess that:

If you are an experienced programmer, you may guess that:

But both answers are wrong. In C#, you will get:

The following tutorial example demonstrates this answer in a slightly different way:

// Precision.cs
// Copyright (c) 2015, HerongYang.com, All Rights Reserved.

using  System;
class Precision {
   public static void Main() {
      float f;
      double d;
      decimal m;
      for (int i=1; i<=2; i++) {
         f = (float)i/3;
         d = (double)i/3;
         m = (decimal)i/3;
         Console.WriteLine("Testing {0}/3:", i);
         Console.WriteLine(" f = {0}", f);
         Console.WriteLine(" d = {0}", d);
         Console.WriteLine(" m = {0}", m);
         Console.WriteLine(" f*3 = {0}", f*3);
         Console.WriteLine(" d*3 = {0}", d*3);
         Console.WriteLine(" m*3 = {0}", m*3);
         Console.WriteLine(" (double)f*3 = {0}", (double)f*3);
         Console.WriteLine(" (decimal)f*3 = {0}", (decimal)f*3);
         Console.WriteLine(" (decimal)d*3 = {0}", (decimal)d*3);
         Console.WriteLine(" (double)((float)i/3)*3 = {0}", 
            (double)((float)i/3)*3);
      }
   }
}

See next section for the output of this example and discussion.

Last update: 2015.

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

 Data Type and Variables

 Logical Expressions and Conditional Statements

 Arrays and Loop Statements

 Data Type Features

Floating-Point Data Types

Precision of Floating-Point Data Types

 Precision of Floating-Point Data Types - Test

 Performance of Floating-Point Data Types

 Performance of Floating-Point Data Types - Test

 IEEE 754 Standards - "float" and "double"

 IEEE 754 Standards - "float" and "double" - Test

 Binary Representation of "decimal"

 Accuracy of "decimal" Data Type

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 MSBuild - Microsoft Build Engine

 Memory Usages of Processes

 Multithreading in C#

 Async Feature from C# 5

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

 WPF - Windows Presentation Foundation

 Partial Classes and Partial Methods

 Outdated Tutorials

 References

 PDF Printing Version