1

I understand that due to the way floats are stored, there are some values that cannot be represented exactly. (See Why Are Floating Point Numbers Inaccurate?)

That being said, in .NET / C#, what options or guarantees does one have in terms of precision? https://msdn.microsoft.com/en-us/library/b1e65aza.aspx?f=255&MSPPError=-2147217396 refers to "approximate" range. Why is an approximation necessary in this context?

Ultimately, I'm looking to store a value like:

x.xxxxxxxx

where the value is always positive. This doesn't seem to be a hugely precise number, yet I'd like to know for sure that all possible numeric combinations above are "durable."

Edit:

To clarify about the exact level of precision I want, I'd like to be able to have eight digits following the decimal point, and a single digit to the left of it.

Community
  • 1
  • 1
BenjiFB
  • 4,545
  • 10
  • 46
  • 53
  • 1
    Well what level of precision and range *do* you want? It looks like you could easily use `decimal` from your example, but if you want more digits than that, you'll need to be more specific. – Jon Skeet Dec 13 '16 at 20:49
  • I've updated the text to be more specific. Thanks... – BenjiFB Dec 13 '16 at 20:55
  • 2
    Right, well `decimal` is fine for that. `double` would be fine too, given than it's got 15-16 digits of precision, but `decimal` would be more simply predictable, and I'd argue that it's more representative if you care a lot about the exact digits. – Jon Skeet Dec 13 '16 at 20:56

2 Answers2

4

It sounds like you're looking for decimal.

From the link:

Precision 28-29 significant digits

Approximate Range (pasting doesn't give the formatting of the exponent etc. so just check out the link.)

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • Cool. Do you know why https://msdn.microsoft.com/en-us/library/364x0z75.aspx?f=255&MSPPError=-2147217396 again uses the term "approximate range" still? Is this type subject to the possible approximation that applies to float? – BenjiFB Dec 13 '16 at 21:00
  • 1
    @BenjiFB I'm not sure. But _maybe_ that's because we're dealing with base 10 digits, so different numbers will be a little different depending on how they align with base 2. (Just a guess, though.) – ispiro Dec 13 '16 at 21:05
  • 1
    @BenjiFB They likely give an approximate range because very few people are interested in the *exact* limits. This is like stating that normalized IEEE-754 single precision (`float`) values can express magnitude in the approximate range [3.2e-38, 3.4e+38], rather than stating the precise limits [1.17549435e-38, 3.40282347e+38] – njuffa Dec 13 '16 at 21:20
1

what options or guarantees does one have in terms of precision

base on the doc an Wikipedia float has a precision of 7 significant decimal digits precision

something like 1234567 or 1.234567 or 0.0000001234567

Why is an approximation necessary in this context

because it is not exact. IEEE 754 floating-point value is (2 − 2−23) × 2127 ≈ 3.402823 × 10^38

though you might be able to get a number like 3.4028239999 × 10^38 and the last 5 digits (99999) cannot be trusted

Steve
  • 11,696
  • 7
  • 43
  • 81
  • Got it. But ultimately, the first 7 digits can be "trusted" with a float? It's only the digits following which cannot be trusted? – BenjiFB Dec 13 '16 at 21:01
  • 1
    @BenjiFB yes. Though you want to be careful when doing massive calculation with float. you will be losing some precision each time and eventually you might end up with 0 precision – Steve Dec 13 '16 at 21:07
  • 1
    @BenjiFB Actually [only 6 digits can be trusted in the worst case](http://www.exploringbinary.com/decimal-precision-of-binary-floating-point-numbers/). (For example, 9.86e-4 to 7 digits is 9.860001e-4 .) – Rick Regan Dec 14 '16 at 14:45
  • 1
    @RickRegan what I mean is if you do for (int i = 0; i < int.Max; i ++) floatVar = floatVar * floatVar; then you might end up with 0 – Steve Dec 14 '16 at 15:19