0

I'm using BinaryReader to read some floats off of a file and put them in a DataGrid. I saved the file back using the data that I got in the DataGrid without making any changes, and when doing a binary compare of the original and the saved files, I noticed that any negative zeros (0x00000080 in little-endian) were being transformed into positive zeros (0x00000000). I know it probably won't hurt, but maybe there's a reason negative zeros are saved like that, so I'm asking whether there's a way to handle negative zeros as they are.

Do I have to override ReadSingle and ReadDouble and do my own conversions? Is there a way to make sure a float remains a negative zero even after being put into a DataGridColumn with typeof(float), and then saved correctly back in the resulting file using a BinaryWriter?

  • 1
    Are there really negative zero and positive zero character codes reserved??? From mathematical point of view, it's nonsense... – george.zakaryan Oct 05 '12 at 12:21
  • Looking at BinaryReader and BinaryWriter in ILSpy, it doesn't look like they do this on purpose. Have you checked whether the DataGrid is failing to preserve it? – Rawling Oct 05 '12 at 12:25
  • Are you sure that the conversion happens in Binary(Reader|Writer)? Reflector shows that they treat the float value as binary exactly. They just copy the bits using unsafe code. – usr Oct 05 '12 at 12:28
  • I was looking at the resulting float of ReadSingle, and it shows up on intellisense as 0.0. If it's just IntelliSense's fault, and it is actually saved as negative 0 in the float, how would I go about verifying that? And how would I preserve the negative 0 in a DataGrid? What if the user wanted to enter a negative zero in a cell that didn't have one in order to be saved as 0x00000080? How would I go about doing that? – Lefteris Aslanoglou Oct 05 '12 at 12:28
  • @usr Just did a quick test and `Binary____er` does indeed roundtrip `-0f` as `-0f`. – Rawling Oct 05 '12 at 12:33
  • Good to know. I'll update my question. – Lefteris Aslanoglou Oct 05 '12 at 12:34
  • As sizeof(float) == sizeof(int) you could just save data as integer( if use union) (I don't know C#). more about signed zeros http://en.wikipedia.org/wiki/Signed_zero – kanna Nov 30 '12 at 17:11

0 Answers0