# Do "time points" and "durations" have an algebraic structure?

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 time
• duration is, well, a duration

Mathematical operations can be made mixing these two types, namely:

• duration + duration -> duration
• time_pointtime_point -> duration
• time_point + duration -> time_point
• time_point + time_point is not defined

I 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:

• $$0 + 0 = 0$$
• $$1 – 1 = 0$$
• $$1 + 0 = 1$$

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:

• Vectors behave exactly as they did in $$V$$, just with a zero attached as an extra coordinate
• Adding a vector to a point gives an element of $$V oplus mathbb R$$ with the last coordinate equal to $$1$$, i.e. a point
• Subtracting two points gives an element of $$V oplus mathbb R$$ with the last coordinate equal to $$0$$, i.e. a vector
• Adding two points gives an element of $$V oplus mathbb R$$ with the last coordinate equal to $$2$$, which is neither a point nor a vector (although one can speculate on the meaning of such elements)
• In an affine space, any linear combination of points with the sum of coeffieients equal to $$1$$ is meaningful and produces a point, while a combination with the sum of coeffieients equal to $$0$$ is meaningful as well and produces a vector. Likewise, if one takes a linear combination of points embedded in $$V oplus mathbb R$$, the last coordinate will be just the sum of coefficients of the combination, giving $$1$$ (a point) or $$0$$ (a vector) for the aforementioned cases.
• This embedding allows affine transformations to be represented as linear operators and thus as matrices, which is extensively used in 3D graphics.

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

## Related Questions

### $limlimits_{Rto0^+}intlimits_{x^2+y^2le R^2}e^{-x^2}cos(y)dxdy=?$

1  Asked on December 8, 2021

### What is the difference between ${3 choose 2}$ and ${3 choose 1}{2 choose 1}$?

3  Asked on December 8, 2021

### Computing sparse eigenvectors of psd matrices

1  Asked on December 8, 2021

### Express the accumulated amount in the bank as an explicit formula

0  Asked on December 8, 2021 by tay-ivan

### Which of the following sets of boolean functions is functionally complete?

1  Asked on December 8, 2021 by play4u

### Find the probability using the formula for the number of permutations with repetitions

2  Asked on December 8, 2021

### How to decompose a number sequence into convergent subsequences?

1  Asked on December 8, 2021 by user779130

### How to approximate the peak of an epidemic (depending on epidemiological parameters)?

1  Asked on December 8, 2021

### Two conditional expectations equal almost everywhere

1  Asked on December 8, 2021 by yy66yy

### Evaluate $cos a cos 2 a cos 3 a cdots cos 999 a$ where $a=frac{2 pi}{1999}$

1  Asked on December 8, 2021

### Uniformly continuous and differentiable

0  Asked on December 8, 2021

### Is there a thing as a “Negative Dimensional Space”?

0  Asked on December 8, 2021

### Are the rationals minus a point homeomorphic to the rationals?

3  Asked on December 8, 2021 by cheerful-parsnip

### Edited: Let $I=int_{0}^{pi/2}(sin 2x)^{1/3}sin x dx$ $J=int_{0}^{pi/2}(cos 2x)^{1/3}cos x dx$. Find $I/J$

3  Asked on December 8, 2021 by aspirant

### Abelian finite groups and their subgroups

2  Asked on December 8, 2021 by markvs

### Painting the edges of an $n$-gonal prism with 3 colors all the edges of each vertex have different colors if $n= 2018$ and $n= 2019$?

2  Asked on December 8, 2021

### A question for a measurable function $g$ on a finite measure space such that $fgin L^p$ for all $fin L^p$

1  Asked on December 8, 2021 by blancket

### Integral of a logarithmic derivative of a complex polynomial over the real line

1  Asked on December 8, 2021 by user302934

### Relation between spectral radius when the norms are equivalent

1  Asked on December 8, 2021 by math-lover

### Financial Mathematics : Annuity loan with a different first period?

2  Asked on December 8, 2021

### Ask a Question

Get help from others!