且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

DateTime精度与准确度?

更新时间:2023-02-26 19:01:18

他在该文章中使用术语准确性错误。真实的准确性真的意味着系统的偏见,例如,如果你的电脑的时钟在一分钟之内就会发生什么。 Lippert的文章真正说明的是错误精确度



在换句话说, DateTime 结构非常精确,因为它能够表示只有100纳秒的差异。但是,您从 DateTime.Now 中获得的值只能在16毫秒内精确到达。但是,您所提供的数字仍然具有 DateTime 能够表示的完整精度。这创建了一个getcha,因为很容易假设 DateTime.Now 返回的值包含 DateTime 结构的完整精度而不是花时间来了解 DateTime.Now 获取其值的源所提供的精度。



DateTime ,作为一个简单的数据结构,没有固有的准确性,所以谈论它并不真实。 (一公斤的准确度是多少?)更好地谈谈系统时钟的准确度。 (例如,这个比例是多么准确)但是这可能会随着每台计算机的不同而有所不同,并且与.NET中的数据结构无关。


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 precision so long as everyone knows it. Precision, not accuracy

What does he mean by that?

What does it mean to approach DateTime precision issues, vs. accuracy issues?

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.