C# Tutorials - Herong's Tutorial Examples - v3.32, by Herong Yang
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) 2006 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.
Table of Contents
Logical Expressions and Conditional Statements
►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
Visual C# 2010 Express Edition
C# Compiler and Intermediate Language
Compiling C# Source Code Files
MSBuild - Microsoft Build Engine
System.Diagnostics.FileVersionInfo Class
WPF - Windows Presentation Foundation