Herong's Tutorial Notes On C# - Part B
Dr. Herong Yang, Version 2.02

Binary Representation of 'float' and 'double' Values

Part:   1  2  3  

(Continued from previous part...)

   public void outputDebugText() {
      int i;
   
      Console.WriteLine("======");
      Console.WriteLine("Converting a real number to IEEE Stardard 754");
      Console.WriteLine("Binary 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;
      
      // try 0.0 first 
      v = 0.0f;
      x = new IeeeFloat(v);
      Console.WriteLine("{0}, {1}", v, x.toBinaryExpression());
      
      // try 1.0
      v = 1.0f;
      x = new IeeeFloat(v);
      Console.WriteLine("{0}, {1}", v, x.toBinaryExpression());

      // try 2.0
      v = 2.0f;
      x = new IeeeFloat(v);
      Console.WriteLine("{0}, {1}", v, x.toBinaryExpression());

      // try 0.5
      v = 0.5f;
      x = new IeeeFloat(v);
      Console.WriteLine("{0}, {1}", v, x.toBinaryExpression());

      // try 1/3
      v = 1.0f/3.0f;
      x = new IeeeFloat(v);
      Console.WriteLine("{0}, {1}", v, x.toBinaryExpression());

      // try 0.1
      v = 0.1f;
      x = new IeeeFloat(v);
      Console.WriteLine("{0}, {1}", v, x.toBinaryExpression());

      // try MaxValue
      v = float.MaxValue;
      x = new IeeeFloat(v);
      Console.WriteLine("{0}, {1}", v, x.toBinaryExpression());

      // try MinValue
      v = float.MinValue;
      x = new IeeeFloat(v);
      Console.WriteLine("{0}, {1}", v, x.toBinaryExpression());

      // try Epsilon
      v = float.Epsilon;
      x = new IeeeFloat(v);
      Console.WriteLine("{0}, {1}", v, x.toBinaryExpression());

      // try Epsilon
      v = float.Epsilon;
      x = new IeeeFloat(v);
      x.outputDebugText();

      // try 1/3
      v = 1.0f/3.0f;
      x = new IeeeFloat(v);
      x.outputDebugText();

      // try MaxValue
      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 Stardard 754
Binary Floating Point with Single Precision:

 Value:    1.401298E-45
 Sign:     1
 Exponent: -127
 Fraction: 0.00000000000000000000001
 Position:   01234567890123456789012

======
Converting a real number to IEEE Stardard 754
Binary Floating Point with Single Precision:

 Value:    0.3333333
 Sign:     1
 Exponent: -2
 Fraction: 1.01010101010101010101011
 Position:   01234567890123456789012

======
Converting a real number to IEEE Stardard 754
Binary 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()

Part:   1  2  3  

Dr. Herong Yang, updated in 2002
Herong's Tutorial Notes On C# - Part B - Binary Representation of 'float' and 'double' Values