为什么 System.Timers.Timer.Interval 的数据类型是双精度型? [英] Why is the data type of System.Timers.Timer.Interval a double?

查看:13
本文介绍了为什么 System.Timers.Timer.Interval 的数据类型是双精度型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个学术问题,因为我正在努力思考 Microsoft 使用 double 作为 Interval 属性的数据类型背后的想法!

This is a bit of an academic question as I'm struggling with the thinking behind Microsoft using double as the data type for the Interval property!

首先来自 MDSN Interval 是 Elapsed 事件之间的时间,以毫秒为单位;我会将其解释为一个离散数,那么为什么要使用双精度数呢?肯定 int 或 long 更有意义!?

Firstly from MDSN Interval is the time, in milliseconds, between Elapsed events; I would interpret that to be a discrete number so why the use of a double? surely int or long makes greater sense!?

Interval 可以支持 5.768585 (5.768585 ms) 之类的值吗?尤其是当人们认为 System.Timers.Timer 远没有亚毫秒级精度时...... 最准确的计时器在 .NET 中?

Can Interval support values like 5.768585 (5.768585 ms)? Especially when one considers System.Timers.Timer to have nowhere near sub millisecond accuracy... Most accurate timer in .NET?

对我来说似乎有点愚蠢..也许我遗漏了什么!

Seems a bit daft to me.. Maybe I'm missing something!

推荐答案

反汇编显示间隔是通过调用 (int)Math.Ceiling(this.interval) 消耗的,所以即使你指定一个实数,在使用前会转成int.这发生在名为 UpdateTimer 的方法中.

Disassembling shows that the interval is consumed via a call to (int)Math.Ceiling(this.interval) so even if you were to specify a real number, it would be turned into an int before use. This happens in a method called UpdateTimer.

为什么?不知道,也许规范说在某一时刻需要 double 并且改变了?最终的结果是 double 不是严格要求的,因为它最终被转换为 int 并且不能大于 Int32.MaxValue 根据无论如何都是文档.

Why? No idea, perhaps the spec said that double was required at one point and that changed? The end result is that double is not strictly required, because it is eventually converted to an int and cannot be larger than Int32.MaxValue according to the docs anyway.

是的,计时器可以支持"实数,它只是不会告诉您它悄悄地改变了它们.您可以使用 100.5d 初始化并运行计时器,它将其转换为 101.

Yes, the timer can "support" real numbers, it just doesn't tell you that it silently changed them. You can initialise and run the timer with 100.5d, it turns it into 101.

是的,这有点愚蠢:浪费了 4 个字节、潜在的隐式转换、转换调用、显式转换,如果他们只是使用 int,所有这些都是不必要的.

And yes, it is all a bit daft: 4 wasted bytes, potential implicit casting, conversion calls, explicit casting, all needless if they'd just used int.

这篇关于为什么 System.Timers.Timer.Interval 的数据类型是双精度型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆