C# Decimal.Epsilon

Roland Pihlakas picture Roland Pihlakas · Aug 2, 2012 · Viewed 7.5k times · Source

Why doesn't Decimal data type have Epsilon field?

From the manual, the range of decimal values is ±1.0 × 10e−28 to ±7.9 × 10e28.

The description of Double.Epsilon:

Represents the smallest positive Double value greater than zero

So it seems, Decimal has such a (non-trivial) value too. But why isn't it easily accessible?

I do understand that +1.0 × 10e−28 is exactly the smallest positive Decimal value greater than zero:

decimal Decimal_Epsilon = new decimal(1, 0, 0, false, 28); //1e-28m;

By the way, there are a couple of questions that give information about Decimal data type's internal representation:

Here's an example where Epsilon would be useful.

Lets say I have a weighted sum of values from some sampling set and sum of weights (or count) of samples taken. Now I want to compute the weighted mean value. But I know that the sum of weights (or count) may be still zero. To prevent division by zero I could do if... else... and check for the zero. Or I could write like this:

T weighted_mean = weighted_sum / (weighted_count + T.Epsilon)

This code is shorter in my eye. Or, alternatively I can skip the + T.Epsilon and instead initialize with:

T weighted_count = T.Epsilon;

I can do this when I know that the values of real weights are never close to Epsilon.

And for some data types and use cases this is maybe even faster since it does not involve branches. As I understand, the processors are not able to take both branches for computation, even when the branches are short. And I may know that the zeros occur randomly at 50% rate :=) For Decimal, the speed aspect is likely not important or even positively useful in the first case though.

My code may be generic (for example, generated) and I do not want to write separate code for decimals. Therefore one would like to see that Decimal have similar interface as other real-valued types.

Answer

Tanzelax picture Tanzelax · Aug 2, 2012

Contrary to that definition, epsilon is actually a concept used to eliminate the ambiguity of conversion between binary and decimal representations of values. For example, 0.1 in decimal doesn't have a simple binary representation, so when you declare a double as 0.1, it is actually setting that value to an approximate representation in binary. If you add that binary representation number to itself 10 times (mathematically), you get a number that is approximately 1.0, but not exactly. An epsilon will let you fudge the math, and say that the approximate representation of 0.1 added to itself can be considered equivalent to the approximate representation of 0.2.

This approximation that is caused by the nature of the representations is not needed for the decimal value type, which is already a decimal representation. This is why any time you need to deal with actual numbers and numbers which are themselves not approximations (i.e. money as opposed to mass), the correct floating point type to use is decimal and not double.