3 ms·
This looks to me like the semantics are good but the implementation details are broken. 1 * time.Second * time.Second semantically reads to me as 1 second. If t
by eyelidlessness 5y ago
This looks to me like the semantics are good but the implementation details are broken. 1 * time.Second * time.Second semantically reads to me as 1 second. If time.Second is some numeric value, that’s obviously wrong everwhere unless the type system reflects and enforces the unit conversion.
- linkdd 5y ago> 1 * time.Second * time.Second semantically reads to me as 1 second. Which is wrong, 1s * 1s = 1s². For example, the force of gravity is expressed in m/s² and describe an acceleration (m/s / s, aka a change of velocity per time units, where velocity is a change of distance per time units).
- deleted 5y ago[deleted]
- eyelidlessness 5y agoOkay so do I need to consult Relativity to program 1sec + 2min?
- linkdd 5y agoSince 1min could be 61 seconds[1], yes? But assuming your comment is not a joke. You probably want to convert minutes to seconds in order to work with the same units, then add the scalar parts together. That's how you deal with different quantities: convert to same unit, add values. This is analog to fractions: 1/2 + 1/4 = 2/4 + 1/4 = (2+1)/4 = 3/4. [1] - https://en.wikipedia.org/wiki/Leap_second
- eyelidlessness 5y agoIn basic middle school math it’s common to multiply different units as a basic conversion mechanism. Multiplying by the same unit is semantically equivalent to “x times 1 is identity(x)”, and other cross-unit arithmetic implies conversion to ensure like units before processing. A typed unit numeric system would imply that to me. It would not imply I’m multiplying the units, but rather the scalar value of the unit.
- linkdd 5y ago> In basic middle school math it’s common to multiply different units as a basic conversion mechanism EDIT: Yes, you multiply the units `2m * 2s` : you first multiply the units to get: `m.s`. This is what I say: you convert everything to the same units before doing the calculations. > Multiplying by the same unit is semantically equivalent to “x times 1 is identity(x)” This is wrong. 1kg * 1kg = 1kg² period. What you're saying is `2kg * 1 = 2kg`, which is right, because `1` is a scalar while `2kg` is a quantity. This is completely different than multiplying 2 quantities. > It would not imply I’m multiplying the units, but rather the scalar value of the unit. That's where you're wrong. When doing arithmetic on quantities, you have 2 equations: x = 2kg * 4s unit(x) = kg * s = kg.s scalar(x) = 2 * 4 = 8 x = 8 kg.s Or x = 5m / 2s x = (5/2) m/s x = 2.5 m/s There is a meaning to units and the operation you do with them. `5m / 2s` is 5 meters in 2 seconds, which is the speed `2.5 m/s`. `2m + 1s` has no meaning, therefore you can't do anything with the scalar values, and the result remains `2m + 1s`, not `3 (m+s)`.
- int_19h 5y agoWhat is the "scalar value of the unit"? Units can be expressed in terms of other units, and you can arbitrarily pick one unit as a base and then express the rest in it. But the key word here is "arbitrarily". If multiplying by the same unit yield the same unit, then how did you compute area or volume in school?
- gpderetta 5y agoWait, would you really expect 1m * 1m to be anything other than 1m²? When does it ever happens that you want to multiply to non-unitless[1] measurements and not multiply the units??? [1] would that be unitful?
- eyelidlessness 5y agoI expect 1 * m = 1m, and 1 * m * m = 1m because applying a unit doesn’t inherently have a value of that unit associated with it. (1 m) (1 m) obviously equals 1m^2, but ((1 m) m) is not the same expression.
- mminer237 5y agoYou can just do `1 * time.Second + 2 * time.Minute` to do that. Adding times works intuitively. It's multiplying durations that gives you accelerations.