1

I do not understand why decimals can represent numbers like 0.1 and floating points cannot. I have read so many articles and questions on this e.g. this one: Difference between decimal, float and double in .NET?

The answerer in the link above states that floating points are base 2 and decimals are base 10. I believe this has something to do with it. However, I have the same confusion as @BKSpurgeon (comment under the answer). Everything is base 2 in the end?

Community
  • 1
  • 1
w0051977
  • 15,099
  • 32
  • 152
  • 329
  • They're not immune to rounding errors - they can just represent base10 numbers precisely. See if http://csharpindepth.com/Articles/General/FloatingPoint.aspx and http://csharpindepth.com/Articles/General/Decimal.aspx help... but if not, it would be worth editing the question to be clearer about what you're looking for... or just add a comment to the answer on the other question. – Jon Skeet Sep 05 '16 at 08:58
  • @Jon Skeet, thanks. I read your article at the weekend and it helped. However, I cannot get my head around the base 2 v base 10 argument. Everything is base 2 in the end isn't it? I plan to buy your book (C# in depth) in the next few days as I will have to use C# more in future. – w0051977 Sep 05 '16 at 10:02
  • Well, there are certainly "bits"... but for both binary floating point and decimal floating point, you can think of the number as being an integer and a scale... and in decimal, that "integer" is treated as being base 10, so an integer of 123 and a scale of -2 would end up as 1.23. – Jon Skeet Sep 05 '16 at 10:07
  • @Jon Skeet, could you provide an example of a number that has rounding errors when represented as floating point, but does not have rounding errors as a decimal? – w0051977 Sep 05 '16 at 12:09
  • Sure: 0.1. That can't be exactly represented in binary, but is represented as "1, scaled by shifting one digit to the right" in decimal. (Note that decimal is still "floating point" - it's just that it's *decimal* floating point rather than *binary* floating point.) – Jon Skeet Sep 05 '16 at 12:10
  • @Jon Skeet, Ah, it is the arithmetic that is base 10 (in the case of a decimal) instead of the storage (which is always base 2). Is that correct? – w0051977 Sep 05 '16 at 12:50
  • Well, sort of. I don't think I'd express it that way, but it's the base in which you interpret the number when shifting... – Jon Skeet Sep 05 '16 at 12:51
  • @Jon Skeet, how would you express it? I realise this question is quite old but thought I would ask. I have never got my head around this properly. I now have a requirement, which may need a Floating Point number (never used them before). – w0051977 May 02 '17 at 08:24
  • I'd say that the storage is as an integer, scaled by some power of ten... so you could write it as a *decimal* integer and then shift the decimal point... that would be an accurate representation of what's stored. – Jon Skeet May 02 '17 at 09:00

1 Answers1

1

Decimal numbers do indeed appear to be error free from rounding so long as you write them out with a radix of 10. They most certainly are not "base 2 in the end".

IEEE754 floating point numbers are also error free from rounding if you write them out with a radix of 2.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483