Advertisement
Guest User

Proving a point about saving floats to a text/binary file.

a guest
Dec 20th, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.10 KB | None | 0 0
  1. #define Human_Readable
  2. // Comment-out the line above to do binary serialization instead. Both work.
  3.  
  4. using System;
  5.  
  6. using System.IO;
  7. using System.Runtime.Serialization;
  8. using System.Runtime.Serialization.Formatters.Binary;
  9. using System.Xml.Serialization;
  10.  
  11. namespace FloatSerializationTest
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             Random random = new Random();
  18.  
  19.             int sampleCount = 10000;
  20.  
  21.             // Generate a big array of random floats between 0 and 1:
  22.             float[] originalValues = new float[sampleCount];
  23.             for (int i = 0; i < sampleCount; i++)
  24.                 originalValues[i] = (float)random.NextDouble();
  25.  
  26.             // Save the floats to a file.
  27.             SaveFloatArray(originalValues);
  28.  
  29.             // Load the floats back from the file.
  30.             float[] loadedValues = LoadFloatArray();
  31.  
  32.             for(int i = 0; i < sampleCount; i++)
  33.             {
  34.                 double error = (double)originalValues[i] - (double)loadedValues[i];
  35.                 Console.WriteLine(originalValues[i] + ": " + error);
  36.                 if(error != 0.0)
  37.                 {
  38.                     Console.WriteLine("Dearest Krythic, you were correct, please accept my apology.");
  39.                     // Note: sadly, the preceding line never runs. ;)
  40.                     Console.ReadLine();
  41.                     return;                    
  42.                 }
  43.             }
  44.  
  45.             // This line runs instead.
  46.             Console.WriteLine("All values are exactly equal after saving to a file & reading them back.");
  47.             Console.ReadLine();
  48.         }
  49.  
  50.         static void SaveFloatArray(float[] data)
  51.         {
  52.             FileStream fs = new FileStream("savedFloats.dat", FileMode.Create);
  53.  
  54.             // Choose binary or human-readable text - both work just fine!
  55. #if Human_Readable
  56.             var formatter = new XmlSerializer(typeof(float[]));            
  57. #else
  58.             var formatter = new BinaryFormatter();
  59. #endif
  60.             try
  61.             {
  62.                 formatter.Serialize(fs, data);
  63.             }
  64.             catch (SerializationException e)
  65.             {
  66.                 Console.WriteLine("Failed to serialize. Reason: " + e.Message);
  67.                 throw;
  68.             }
  69.             finally
  70.             {
  71.                 fs.Close();
  72.             }
  73.         }
  74.  
  75.         static float[] LoadFloatArray()
  76.         {
  77.             float[] data;
  78.             FileStream fs = new FileStream("savedFloats.dat", FileMode.Open);
  79.  
  80. #if Human_Readable
  81.             var formatter = new XmlSerializer(typeof(float[]));            
  82. #else
  83.             var formatter = new BinaryFormatter();
  84. #endif
  85.             try
  86.             {
  87.                 data = (float[])formatter.Deserialize(fs);
  88.             }
  89.             catch (SerializationException e)
  90.             {
  91.                 Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
  92.                 throw;
  93.             }
  94.             finally
  95.             {
  96.                 fs.Close();
  97.             }
  98.  
  99.             return data;
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement