C# Tutorials - Herong's Tutorial Examples - v3.32, by Herong Yang
IEEE 754 Standards - "float" and "double" - Test
This section provides a tutorial example on how to convert a 'float' number into the IEEE 754 binary expression format.
Now, let's see a program I wrote to convert a "float" number into the IEEE 754 expression format:
// IeeeFloat.cs
// Copyright (c) 2006 HerongYang.com. All Rights Reserved.
using System;
public class IeeeFloat {
private const int fraction_size = 23;
private const int bias = 127;
private float original_value;
private float value;
private int sign;
private long exponent;
private int lead;
private int[] fraction;
public IeeeFloat(float v) {
original_value = v;
value = original_value;
sign = 1;
lead = 0;
exponent = 0;
fraction = new int[fraction_size];
int i;
for (i=0; i<fraction_size; i++) {
fraction[i] = 0;
}
// of course, working on the sign first
if (value==0.0f) {
sign = 1;
} else if (value==-0.0f) { // not sure if this detect the -0
sign = -1;
value = -1.0f*value;
} else if (value<0.0f) {
sign = -1;
value = -1.0f*value;
}
if (value>0.0f) {
// now, the exponent part
while (value>=2.0f) {
exponent++;
value = value/2.0f;
}
while (value<1.0f && exponent>-bias) {
exponent--;
value = value*2.0f;
}
// the implicit leading bit
if (value>=1.0f) {
value = value-1.0f;
value = value*2.0f;
lead = 1;
} else {
lead = 0;
}
// time for the fraction part
for (i=0; i<fraction_size; i++) {
if (value>=1.0f) {
fraction[i] = 1;
value = value-1.0f;
} else {
fraction[i] = 0;
}
value = value*2.0f;
}
}
}
public string toBinaryExpression() {
string str = "";
if (sign<0) str = str + "-";
str = str + "b(" + lead;
str = str + ".";
for (int i=0; i<fraction_size; i++) {
str = str + fraction[i];
}
str = str + ")*2**(" + exponent + ")";
return str;
}
public void outputDebugText() {
int i;
Console.WriteLine("======");
Console.WriteLine("Converting a real number to IEEE Standard");
Console.WriteLine("Floating Point with Single Precision:");
Console.WriteLine("");
Console.WriteLine(" Value: {0}",original_value);
Console.WriteLine(" Sign: {0}",sign);
Console.WriteLine(" Exponent: {0}",exponent);
Console.Write(" Fraction: ");
Console.Write("{0}.",lead);
for (i=0; i<fraction_size; i++) {
Console.Write("{0}",fraction[i]);
}
Console.WriteLine("");
Console.Write(" Position: ");
Console.Write("{0} ",' ');
for (i=0; i<fraction_size; i++) {
Console.Write("{0}",i%10);
}
Console.WriteLine("");
Console.WriteLine("");
}
public static void Main() {
float v;
IeeeFloat x;
v = 0.0f;
x = new IeeeFloat(v);
Console.WriteLine("{0}, {1}", v, x.toBinaryExpression());
v = 1.0f;
x = new IeeeFloat(v);
Console.WriteLine("{0}, {1}", v, x.toBinaryExpression());
v = 2.0f;
x = new IeeeFloat(v);
Console.WriteLine("{0}, {1}", v, x.toBinaryExpression());
v = 0.5f;
x = new IeeeFloat(v);
Console.WriteLine("{0}, {1}", v, x.toBinaryExpression());
v = 1.0f/3.0f;
x = new IeeeFloat(v);
Console.WriteLine("{0}, {1}", v, x.toBinaryExpression());
v = 0.1f;
x = new IeeeFloat(v);
Console.WriteLine("{0}, {1}", v, x.toBinaryExpression());
v = float.MaxValue;
x = new IeeeFloat(v);
Console.WriteLine("{0}, {1}", v, x.toBinaryExpression());
v = float.MinValue;
x = new IeeeFloat(v);
Console.WriteLine("{0}, {1}", v, x.toBinaryExpression());
v = float.Epsilon;
x = new IeeeFloat(v);
Console.WriteLine("{0}, {1}", v, x.toBinaryExpression());
v = float.Epsilon;
x = new IeeeFloat(v);
x.outputDebugText();
v = 1.0f/3.0f;
x = new IeeeFloat(v);
x.outputDebugText();
v = float.MaxValue;
x = new IeeeFloat(v);
x.outputDebugText();
}
}
Output:
0, b(0.00000000000000000000000)*2**(0) 1, b(1.00000000000000000000000)*2**(0) 2, b(1.00000000000000000000000)*2**(1) 0.5, b(1.00000000000000000000000)*2**(-1) 0.3333333, b(1.01010101010101010101011)*2**(-2) 0.1, b(1.10011001100110011001101)*2**(-4) 3.402823E+38, b(1.11111111111111111111111)*2**(127) -3.402823E+38, -b(1.11111111111111111111111)*2**(127) 1.401298E-45, b(0.00000000000000000000001)*2**(-127) ====== Converting a real number to IEEE Standard Floating Point with Single Precision: Value: 1.401298E-45 Sign: 1 Exponent: -127 Fraction: 0.00000000000000000000001 Position: 01234567890123456789012 ====== Converting a real number to IEEE Standard Floating Point with Single Precision: Value: 0.3333333 Sign: 1 Exponent: -2 Fraction: 1.01010101010101010101011 Position: 01234567890123456789012 ====== Converting a real number to IEEE Standard Floating Point with Single Precision: Value: 3.402823E+38 Sign: 1 Exponent: 127 Fraction: 1.11111111111111111111111 Position: 01234567890123456789012
Exercise: Add a new method in IeeeFloat class to output the value in IEEE 754 storage format, with the following method signature:
public string ToBits()
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