英文:
Why does dividing two time.durations in Go result in another time.duration?
问题
我不理解在Go语言中如何对time.Duration
进行除法运算。
例如,这个例子非常可爱:
d, _ := time.ParseDuration("4s")
fmt.Println(d/4)
输出结果是1s
。这很棒,因为(天真地)4秒除以4等于1秒。
然而,当我们发现分母中的4必须是一个时间段时,情况就有点混乱了。所以尽管下面的代码:
d1 := time.Duration(4)
fmt.Println(d/d1)
也输出1s
,我们知道d1
实际上是4ns
,我完全不相信4秒除以4纳秒等于1秒。
我感到困惑的原因是,时间段除以时间段应该是无量纲的(我想是这样的,对吗?),而时间段除以一个无量纲的数应该具有时间单位。
我知道类型不等于单位,但显然我对某些事情存在误解,或者可能是一系列的事情。如果能帮助我澄清这个问题,我将不胜感激!
这是上述示例的Go Playground链接:https://play.golang.org/p/Ny2_ENRlX6。为了提供背景,我正在尝试计算事件之间的平均时间。我可以退而使用浮点数表示秒,但我想保持在time.Duration
的领域内。
英文:
I don't understand what it means to divide a time.Duration
in Go.
For example, this is super lovely:
d,_ := time.ParseDuration("4s")
fmt.Println(d/4)
print 1s
. Which is ace, because (naively) 4 seconds divided by 4 is 1 second.
It gets a little confusing though when we find out that the 4 in the denominator has to be a duration. So although:
d1 := time.Duration(4)
fmt.Println(d/d1)
also prints 1s
, we know that d1
is actually 4ns
and I'm entirely unconvinced that 4 seconds divided by 4 nanoseconds is 1 second.
I'm confused because a duration divided by duration should be dimensionless (I think, right?), whereas a duration divided by a dimensionless number should have units of time.
And I know that type != unit, but I'm clearly misunderstanding something, or quite possibly a set of things. Any help to clear this up would be most appreciated!
Here is a go playground of the above examples. https://play.golang.org/p/Ny2_ENRlX6. And just for context, I'm trying to calculate the average time between events. I can fall back to using floats for seconds, but am trying to stay in time.Duration
land.
答案1
得分: 6
从数学上讲,你是正确的:将两个时间间隔进行除法应该得到一个无量纲的量。但是,Go语言的类型系统不是这样工作的。任何数学运算的结果都是与输入类型相同的值。你需要显式地将除法的结果转换为int64类型,以获得一个"无类型"的量。
英文:
Mathematically, you're correct: dividing two time.Durations should result in a dimensionless quantity. But that's not how go's type system works. Any mathematical operation results in a value of the same type as the inputs. You'll have to explicitly cast the result of the division to an int64 to get an "untyped" quantity.
答案2
得分: 4
这是因为time.Duration
是int64
类型。请参考time包的文档。
你将4000000000(4秒)除以4(4纳秒),得到1000000000(1秒)。在这个操作中,你应该将它们看作整数而不是具有类型的值。Duration
类型使其看起来像是一个物理值,但对于除法操作来说,它只是一个数字。
英文:
It is so because time.Duration
is int64
. See documentation of time package.
You make a division of 4000000000 (4s) by 4 (4ns) and you get 1000000000 (1s). You should look at the operations as they where integers not typed values. Type Duration
make it look like a physical value but for division operation it is just a number.
答案3
得分: 3
time.Duration
没有附加单位。time.Duration
表示一个持续时间的物理概念(以秒为单位),通过提供一个独立的类型,即time.Duration
类型来表示。但从技术上讲,它只是一个uint64
。
如果你试图给类型附加实际的单位,你将进入单位地狱:一个(time.Duration * time.Duration)/acceleration.Radial * mass.MetricTon
会是什么?很可能是未定义的。
英文:
There are no units attached to a time.Duration
. A time.Duration
represents the physical concept of a duration (measured in seconds and having a unit) by providing a distinct type, namely the time.Duration
type. But technically it is just a uint64
.
If you try to attach actual units to types you'll enter unit-hell: What would a (time.Duration * time.Duration)/acceleration.Radial * mass.MetricTon be? Undefined most probably.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论