views:

61

answers:

3

Is it guaranteed that the binary representation of singles, doubles, and floats will be identical to the original after serializing and deserializing?

+1  A: 

I doubt it very much.

Try it using the DataContractSerializer instead. The XmlSerializer isn't being actively maintained.

John Saunders
+1  A: 

No you can't assume that. Eventually, something will get rounded during serialization and your number will be off. Accurately representing a floating point number in a binary system is difficult at best, and at times is impossible.

http://usenix.org/publications/login/2005-02/pdfs/mccluskey.pdf

Kevin
+1  A: 

You'll need to read this thread to get a feel for what accuracy means when you work with floating point numbers. Whatever happens to them when they get serialized to XML really doesn't matter much. It will be accurate enough. Use the decimal type if you are uncomfortable about this kind of fuzziness. You shouldn't be, that's how they work.

Hans Passant