DateTime precision vs accuracy?

前端 未结 2 1936
野性不改
野性不改 2021-01-15 03:13

Eric Lippert wrote an article about DateTime. Jon Skeet commented on his article with

I don\'t have a problem with DateTime having too much p

相关标签:
2条回答
  • 2021-01-15 03:44

    The problem that arises with having that much precision is of course that it is very easy to assume that a given value is as accurate as it is precise. But that’s not warranted at all! I can represent my height in a double-precision floating point number as 1.799992352094 metres; though precise to a trillionth of a metre, it’s only accurate to about a hundredth of a metre because I do not have a device which can actually measure my height to a trillionth of a meter, or even a thousandth of a metre. There is way more precision than accuracy here.

    Source

    0 讨论(0)
  • 2021-01-15 03:52

    He's using the term accuracy wrong in that article. Poor accuracy really implies a systematic bias, such as what would happen if your computer's clock were off by one minute. What Lippert's article is really illustrating is false precision.

    In other words, the DateTime structure is very precise in that it is capable of representing a difference of only 100 nanoseconds. However, the values you'll get from DateTime.Now are only precise to within 16 milliseconds. But the numbers you're given will still carry the full precision that DateTime is capable of representing. That creates a gotcha, since it's easy to assume that DateTime.Now is returning values that carry the DateTime structure's full precision instead of taking the time to find out what precision is provided by the source from which DateTime.Now gets its values.

    DateTime, being a simple data structure, has no inherent accuracy, so it doesn't really make sense to talk about it. (What's the accuracy of a kilogram?) Better to talk about how accurate the system clock is. (e.g., how accurate is that scale?) But that could vary with every computer, and doesn't really have anything to do with data structures in .NET.

    0 讨论(0)
提交回复
热议问题