4

Floating-point calculations using floats or doubles are executed on hardware which may give different results on different architectures or optimisation settings, as discussed here.

One thing that was briefly mentionned on that thread but not fully answered is the case of System.Decimal. It was mentionned that System.Decimal might be used as a consistent floating-point type, that it would be very slow, and that the mathematical functions offered by .NET (System.Math) were not implemented for System.Decimal.

The only other bit of information I could find about this type was Marc Gravell saying it is implemented in software, which strongly hints that it is indeed consistent.

My question is thus: can System.Decimal be used to obtain consistent results across all hardware platforms, or is it prone to the same issues as System.Single and System.Double?

If System.Decimal is consistent, is there any free library that implements transcendental functions for it (cos, sin, tan, sqrt, log, etc)?

Community
  • 1
  • 1
Asik
  • 21,506
  • 6
  • 72
  • 131

1 Answers1

2

Consistent in a manner that it will yield the same results on all .NET implementations yes.

Consistent in a manner where it will always represent the correct result of an operation no. Any floating point will have troubles representing some value. System.Decimal can only accurately represent those numbers that be accurately represented as decimal value E.g. 1/3 will be represented inaccurately as 0.33333...... and a lot of the results of cos, sin, tan, sqrt and log will be approximations due to the fact that there for any number system (such as decimal or binary) is a lot of values that can only be represented as the nearest approximation.

The C# specification says: The result of an operation on values of type decimal is that which would result from calculating an exact result (preserving scale, as defined for each operator) and then rounding to fit the representation. Results are rounded to the nearest representable value, and, when a result is equally close to two representable values, to the value that has an even number in the least significant digit position.

Rune FS
  • 21,497
  • 7
  • 62
  • 96
  • Could you provide any logical explanation or authoritative source to support your statement? - about the consistency across platforms, not correctness, of course, the question was about consistency. – Asik Dec 12 '12 at 09:05
  • You wrote “Consistent in a manner that it will yield the same results on all .NET implementations yes” but this page says: “Floating point operations may be performed with higher precision than the result type of the operation”. This is the main cause of inconsistent results in other languages and this is the cause of inconsistent results for .NET implementation. http://www.informit.com/articles/article.aspx?p=1648574 – Pascal Cuoq Dec 12 '12 at 20:15
  • @PascalCuoq that particular sentence is under "Floating-Point Types" which encompasses only Single and Double. It doesn't concern Decimal which has its own section after that. – Asik Dec 12 '12 at 22:18
  • Sorry, I do not know why I thought you were talking about binary floating-point in that sentence. My apologies. – Pascal Cuoq Dec 12 '12 at 22:21