Mathematics Asked on October 20, 2020
The C++ programming language has two types that are used to represent time: time_point
and duration
. As the name suggests:
time_point
represents a specific point in timeduration
is, well, a durationMathematical operations can be made mixing these two types, namely:
duration
+ duration
-> duration
time_point
– time_point
-> duration
time_point
+ duration
-> time_point
time_point
+ time_point
is not definedI noticed that $({texttt{duration},texttt{time_point}},+)$ looks like it is almost isomorphic to the group $({0,1},+)$. If we replace duration
with 0 and time_point
with 1 in the above equations we get:
The isomorphism is not perfect, in that $1+1$ is well defined in $({0,1},+)$, but time_point
+ time_point
is not.
My question is: can it be shown, perhaps using category theory, that the types duration
and time_point
form some kind of known algebraic structure?
As already been said in the comments, the relevant algebraic structure is a one-dimensional affine space. This concept originates in geometry, where it is sometimes convenient to distinguish points from vectors. You can add/subtract vectors, you can add a vector to a point, you can subtract points to get a vector, but you cannot add points meaningfully. With this correspondence, durations are vectors and time points are points. Note that these are still two separate sets, connected via common operations.
The analogy with $(0,1)$ that you've discovered is commonly used to put an affine space $A$ and the associated vector space $V$ to a common ground. Take the vector space $V oplus mathbb R$. The vectors from $V$ form a subspace of $V oplus mathbb R$ of the form ${(v,0) | v in V}$. Now, pick any point $p_0 in A$ and define an embedding of $A$ into $V oplus mathbb R$ by mapping $p in A$ to $(p - p_0, 1)$ (note that $p-p_0 in V$). This embedding has several useful features:
However, note that none of the standard data types of C++ actually form a field (and only a few of them form a ring), due to various reasons (signed integer overflow is UB, floating-points are not even associative, etc), and durations & time points are subject to all those problems as well. What it means is that while this correspondence between durations & time points and affine spaces is a mentally fruitful one, care must be taken since one cannot in general use this as a strict mathematical model and/or prove anything about these C++ types.
Answered by lisyarus on October 20, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP