C# FAQ: Why does .NET make arithmetic errors

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:FAQs
edit

Why does .NET make arithmetic errors?

If you are new to computer arithmetic, you may have a few surprises coming. Computer arithmetic is necessarily done in binary (base 2); so, many floating point numbers can only be approximated in the binary. All runtime systems face these limitations, not just the .NET Common Language Runtime (CLR).

The following is a simple example:

double d = 0.1;

The value 0.1 must be stored in a variable of type double. But, that decimal value has no precise equivalent in binary. So, a value is stored which is as close to 0.1 as can be represented in a (binary) double type; but, the value is not exactly 0.1.

This potential problem exists for both double and float types where decimal fractions must be represented in binary.


See also



Personal tools