C# supports infinity math for binary floating point types (Double
and Single
) (see: Express mathematical infinity in C#), but not for Decimal.
I like the Decimal
type for the precision it offers, but I need support for infinity math (details below) as well.
Is there a 'lazy' way to create an InfinityDecimal
struct that adds support for -Inf/+Inf/NaN, much like the Nullable
generic struct adds support for Nullable
types? Perhaps there's some interesting use of attributes that might apply? An existing math library that includes such a beast?
Otherwise I should create a class or struct with a Decimal
value
property, and explicitly implement each of the public methods of the struct Decimal
type, returning the results of the equivalent Decimal
operation unless a operand is, or a result should be, +/-Inf or NaN.
Right?
+INF + N = +INF (where –INF < N < INF)
-INF + N = -INF (where –INF < N < INF)
+INF + +INF = +INF
-INF - +INF = -INF
+INF - +INF = NAN
+INF * +INF = +INF
+INF * -INF = -INF
-INF * -INF = +INF
+INF / +INF = NAN
-INF / -INF = NAN
-INF / +INF = NAN
+INF / -INF = NAN
+INF / 0 = +INF
-INF / 0 = -INF
+INF / N = +INF (where 0 < N < INF)