Analysis of carries in signed digit expansions

The number of positive and negative carries in the addition of two independent random signed digit expansions of given length is analyzed asymptotically for the (q, d)-system and the symmetric signed digit expansion. The results include expectation, variance, covariance between the positive and negative carries and a central limit theorem. Dependencies between the digits require determining suitable transition probabilities to obtain equidistribution on all expansions of given length. A general procedure is described to obtain such transition probabilities for arbitrary regular languages. The number of iterations in von Neumann’s parallel addition method for the symmetric signed digit expansion is also analyzed, again including expectation, variance and convergence to a double exponential limiting distribution. This analysis is carried out in a general framework for sequences of generating functions.


Introduction
Addition is an essential arithmetic operation in many algorithms. As the efficiency of addition is influenced by the number of occurring carries, we asymptotically analyze this number, which depends on the base and the digit set of the digit expansion.
We consider two different types of digit expansions: On the one hand, we investigate (q, d)-expansions, that are extensions of the standard q-ary digit expansion with digit set {d, . . . , q + d − 1}. With d = 0, this includes the case of the standard q-ary expansion. Consecutive digits are independent in this case. On the other hand, the symmetric signed digit expansion [6] has an even base q and the redundant digit set {−q/2, . . . , q/2}. To remove the redundancy, there is a syntactical rule to decide which of the digits −q/2 and q/2 is used. This rule introduces dependencies between consecutive digits.
Two different addition algorithms are investigated. The first one is the standard addition: We add two digits starting at the least significant position. If the result is not in the given digit set or does not fulfill the syntactical conditions, then a non-zero carry is produced. This carry is added to the sum of the two digits at the next position. An example for this standard addition of two decimal expansions is given in Table 1.
In the case of positive and negative digits, positive and negative carries occur. The parameter of interest is their number for an independent pair of random summands of given length.
In contrast to standard addition, von Neumann's addition is a parallel algorithm with several iterations. The idea is to add the digits at each position in parallel (the interim result). If this result is not admissible in the given digit system, then a non-zero carry is produced and the interim result is corrected correspondingly at this position. However, this carry is not added immediately: The interim result and the carries are the input for the next iteration. When the carry sequence only contains zeros, then the algorithm terminates. An example for von Neumann's addition is shown in Table 2 for the addition of two decimal expansions.
The number of iterations of von Neumann's addition is of interest as it corresponds to the running time. Table 1 Example for standard addition in the decimal system 2 1 4 6 1 0 2 1 5 1 5 0 3 4 0 1 The subscripts in the second row are the carries Diaconis and Fulman [1] and Nakano and Sadahiro [9] consider the carries of the standard addition as a Markov chain. This is only valid if the digits of the digit expansion are independent. In their analysis, they obtain a stationary distribution. In this article, we determine the expectation, variance and central limit theorem for the number of positive and negative carries as well as the covariance between the positive and negative carries in the (q, d)-system and the symmetric signed digit system. The authors of [1] concentrate on an odd basis q and the symmetric digit set {−(q − 1)/2, . . . , (q − 1)/2}. The symmetric signed digit expansion (defined later) is the natural way to define a unique representation with a symmetric set of digits and an even base q. Thus, a part of the present paper can be seen as a complement of [1].
The expected number of iterations of von Neumann's addition was analyzed in [8] and [7] for standard q-ary expansions and (q, d)-expansions, respectively. It turns out that the expected number of iterations is logarithmic in the length of the expansions. In [7], symmetric signed digit expansions are analyzed, too, but with a simplified probabilistic model since a precise probabilistic model exceeded computing resources available at that time. This simplification has a significant influence on the main term. In this paper, we combine advances in soft-and hardware with sophisticated use of the finite state machine package of SageMath [13] to tackle the precise model in roughly 10 minutes of CPU time. The results include expectation, variance and convergence to a double exponential distribution.
The outline of the paper is as follows. In Sect. 2, we define (q, d)-expansions and symmetric signed digit expansions. We first analyze the standard addition in Sects. [3][4][5]. The algorithms and the corresponding transducers for the standard addition of (q, d)-expansions and symmetric signed digit expansions are presented in Sect. 3. Our probabilistic model is to choose both summands of length independently such that each expansion of length is equally likely. In the case of the symmetric signed digit expansions, the dependencies between the digits require approximating the equidistribution with an error that does not influence the final result. The corresponding probabilities are defined in Lemma 4.1 in Sect. 4 for general regular languages, see also [10,14]. In Sect. 5, we combine this approximate equidistribution with the transducers from Sect. 3.2 to obtain an asymptotic analysis including the expectation, the variance and asymptotic normality in the main Theorems 1 and 2 for the (q, d)-system and the symmetric signed digit system, respectively.
Then, we analyze von Neumann's addition. We start in Sect. 6 with the algorithms and the automaton. Theorem 3 provides a general framework for the analysis of sequences occurring in this context. Then we again use the approximate equidistribution from Sect. 4 to asymptotically analyze the number of iterations of von Neumann's addition in Theorem 4 in Sect. 7. This analysis extends the results in [7,8] to the symmetric signed digit expansions and to include not only the expected value but also the variance and a convergence in distribution.
Obtaining the values of the constants occuring in the asymptotic analysis of standard and von Neumann's addition requires computations involving finite state machines and determinants of matrices in several variables. These computations are performed using the mathematical software system SageMath [13]. Notebooks containing all the computations can be found at [4]. However, the existence of these constants follows from the theoretical results.

Digit expansions
In this section, we define the digit expansions which will be used in later sections. We also recall their properties.
The (q, d)-expansion exists for all integers if d = 0 and d = −q + 1. For d = 0 (this is the standard q-ary expansion), only the non-negative integers have a (q, d)expansion. Conversely, for d = −q + 1, only the non-positive integers have a (q, d)expansion. If the (q, d)-expansion of an integer exists, then it is unique up to leading zeros.
If q is odd and d = −q+1 2 , then the (q, d)-expansion minimizes the sum of absolute values of the digits among all q-ary expansions with arbitrary digits (see [6]).

Symmetric signed digit expansion
We recall the definition of the symmetric signed digit expansion (SSDE) as defined in [6] and further analyzed in [7].

Definition 2.3
Let q ≥ 2 be an even integer. The symmetric signed digit expansion (SSDE) of an integer is the q-ary digit expansion (x . . .
such that the syntactical rule In [6], it is shown that each integer n has a unique SSDE (up to leading zeros). It minimizes the sum of absolute values of the digits among all q-ary expansions of n with arbitrary digits (cf. [6]). For q = 2, we obtain the digit set {0, ±1} and the syntactical rule that at least one of any two adjacent digits is zero. This digit expansion is also called non-adjacent form (cf. [12]).

Standard addition
We write bold face letters for sequences which are padded with zeros on the left. Let x = . . . x 1 x 0 and y = . . . y 1 y 0 be the two summands given as q-ary expansions with digit set D (possibly satisfying some syntactical rules). Then standard addition can be written in the form . . z 1 z 0 satisfying the syntactical rules of the digit system under consideration. We asymptotically analyze the sequence of carries c = . . . c 2 c 1 . From a different point of view, the standard addition with digit set D is a conversion between different digit sets: We have a q-ary digit expansion with digits in D + D and we want to transform this digit expansion into a digit expansion with digit set D satisfying all syntactical rules. This can be written in the form We call the sequence s the digitwise sum of x and y and write s = x + y.
We will mostly use this point of view. Most of the algorithms and transducers require the input of s. If there are syntactical rules for x and y, then the sequence s can not be arbitrary.
Remark 3.1 From this point of view, it is clear that interchanging two digits x i and y i of the two summands does not influence the result, but only both summands. The carries, the digitwise sum and the steps taken by the algorithms and the transducers stay the same as they depend only on the digitwise sum. Table 3 Example for standard addition for (5, −1)-expansions 1 2 31 1 1 2 0 111 0 31 3 3 The subscripts in the second row are the carries

Standard addition for (q, d)-expansions
The digit set is D = {d, . . . , q + d − 1}. Algorithm 1 transforms a q-ary expansion with digit set D + D into a (q, d)-expansion. As there are no syntactical rules, all digits are independent. Thus, we do not have to look ahead when choosing the carry.
An example of standard addition for (5, −1)-expansions using this algorithm is given in Table 3.

Standard addition for SSDEs
Let q ≥ 2 be even. Algorithm 2 transforms a q-ary expansion with digit set {−q, . . . , q} into a SSDE. As the choice between the redundant digits q 2 and − q 2 depends on the next digit, we have to look ahead at the next digit in these cases. This algorithm is an extension of the one in [7] taking into account that we start with a larger digit set. An example of standard addition for SSDEs with q = 4 using this algorithm is given in Table 4.

Transducers
In this section, we present the transducers for the algorithms presented in the last section.
We are not interested in the output of the addition, but only in the carries. Thus we only use the carries as the output of the transducer. But, if required, the output digits can easily be reconstructed.
In our setting, a transducer consists of a finite set of states S, a finite input alphabet D + D, an output alphabet, a set of transitions E ⊆ S 2 × (D + D) with input labels in D + D, output labels in the output alphabet for each transition, and an initial state. All states are final.
The input of the transducer is a digit expansion with digits in D + D. The output of the transducer is the sequence of labels of a path starting in the initial state with the given input as the input label. In our cases, always exists such a path and it is unique (i.e., the transducer is complete and deterministic).
The labels of the states encode the current carry (except for the situations when we have to look ahead). The number of states is independent of q. The number of transitions between two states depends on the base q. Table 4 Example for standard addition for SSDEs for q = 4 11 0 2 1 011 1 1 2 0 212 0 The subscripts in the second row are the carries for ε = ±1 and a set M. This definition is motivated by the following interpretation: Whenever a set M = { j, . . . , u} occurs, it is actually meant to be the interval [ j, ∞) intersected with the extended digit set. Subtracting 1 leads to [ j − 1, ∞), again intersected with the extended digit set. This corresponds to M − 1 as defined above.

Standard addition for (q, d)-expansions
The transducer in Fig. 1  The transitions are constructed by using Algorithm 1 for the current input and carry.

Standard addition for SSDEs
The transducer in Fig. 2 computes the carries as in Algorithm 2. We use the sets L = {0, . . . , q 2 − 1}, H = { q 2 + 1, . . . , q} and H q = { q 2 , . . . , q − 1}. The transitions are constructed by using Algorithm 2 for the current input and carry. The labels of the states −1, 0 and 1 encode the current carry. In the states with labels ± q 2 , we do not know yet whether the digit of the sum should be q 2 or − q 2 and thus, which carry is produced. To decide this, we have to look at the next digit. Thus, the transitions leading to a state ± q 2 have no output (−) and the transitions starting at a state ± q 2 have two output digits.

Approximate equidistribution
As a probabilistic input model, we want to use an equidistribution on all digit expansions satisfying certain syntactical rules. This is easy in the case of (q, d)-expansions (see Sect. 4.1) because there are no syntactical rules. But in the case of a general regular language, like the SSDE, we can only approximate an equidistribution by Lemma 4.1. However, this approximation does not influence the main terms of the results. A regular language is recognized by an automaton. An automaton is defined to consist of states, transitions between these states with labels, an initial state and final states. So to say, it is a transducer without output. The automaton recognizes a word from a language, if there exists a path starting at the initial state, leading to a final state with this word as the label.
We call an automaton aperiodic if its underlying directed graph is aperiodic, i.e., the greatest common divisor of all lengths of directed cycles of the graph is 1. If the underlying directed graph is strongly connected, then the automaton is so, too. If an automaton is strongly connected and aperiodic, then the adjacency matrix of the underlying graph is primitive.
Given an automaton A for a regular language, we automatically construct transition probabilities between the states to obtain an approximate equidistribution on all words of given length . The weight of the word is the product of the transition probabilities multiplied with an exit weight [the factor in front of the product in (2) below]. This corresponds to an approximate equidistribution on all paths of length of the underlying graph of the automaton starting in the initial state. Without the exit weights, these transition probabilities are the same as defined by Shannon in [14] and Parry in [10]. We implemented the computations of this lemma as part of SageMath [13] as Automaton.shannon_parry_markov_chain. The dominant eigenvalue of A is denoted by λ, all other eigenvalues of A are assumed to be of modulus less than or equal to ξλ for some 0 < ξ < 1. If there are eigenvalues of modulus ξλ, then each of them must be semisimple, i.e., its algebraic and geometric multiplicities coincide.
Let w > 0 and u > 0 be right and left eigenvectors of A for the eigenvalue λ, respectively, such that w 1 = 1 and u, w = 1.
For a transition t from some state i to some state j, we set For ≥ 0, the set of words of L of length is denoted by L . For a word x ∈ L , we denote the states and transitions used when A reads x by 1 = s 0 , …, s and t 1 , …, t , respectively. The weight W (x) of x is then defined to be where e F is the indicator vector of the set F of final states.
Then t leaves i holds for all states i and holds uniformly for ≥ 0 and x ∈ L . Furthermore, consider the time-homogeneous Markov chain M on the state space {1, . . . , n} where the transition probability from state i to state j is t p t where the sum runs over all transitions in A from i to j. Then this Markov chain has the stationary distribution For large and a transition t from some state i to some state j, p t can be thought as the probability of using t under the condition that the automaton is currently in state i. Note that the sum in (3) runs over all transitions leaving i such that multiple transitions between i and j are counted separately although their individual weights p t only depend on i and j. It turns out that the exit weights do not influence the main term of our asymptotic expressions.
Proof of Lemma 4. 1 We first note that the cardinality |L | is given by For x ∈ L with associated sequence of states (s 0 , . . . , s ), we have As w s 0 = w 1 = 1, we get (4). Next, we prove (3) by rewriting the sum as Finally, the transition matrix of the Markov chain M is by definition of the Markov chain and (1). Thus

Fig. 3 Automaton recognizing (q, d)-expansions
As is a left eigenvector of P to the eigenvalue 1. By definition of u and w, n i=1 u i w i = 1. As M is aperiodic and irreducible, (u 1 w 1 , . . . , u n w n ) is the unique left eigenvector with this property and therefore the stationary distribution.
The weight W induces a measure on the words of length . The total measure of all words of length is 1 up to an exponentially small error, thus it is a probability measure up to an exponentially small error. Each word has exactly the same weight. If we see the transition probabilities as a part of the automaton, we obtain a probabilistic automaton: holds for all states s. We call p t the weight or the probability of the transition t.

Weights for (q, d)-expansions
We can use Lemma 4.1 in this case, too, but the digits of a (q, d)-expansion are independent of each other because there are no syntactical rules involving more than one digit. Therefore we can directly obtain equidistribution, not only approximating it. We first describe the direct way and later, in Remark 4.3, we consider using Lemma 4.1.
For any digit x 0 ∈ D, we use the weights W (x 0 ) = 1 q . The exit weight is 1. By independence, we have the weight for a digit expansion x of length . With this weight, we have an equidistribution of all (q, d)-expansions of length .

Remark 4.3
The same weights can be obtained by Lemma 4.1. The transition probabilities are p 0→0 = q −1 . As the automaton recognizing (q, d)-expansions has only one state (see Fig. 3), there is no error term in (4).

Weights for SSDEs
The automaton in Fig. 4 recognizes SSDEs. The adjacency matrix of this automaton is where the states are ordered by their labels.
The matrix A has the eigenvalues q, −1 and 0. The vectors ( 1 q+1 , q q+1 , 1 q+1 ) and ( 1 2 , 1, 1 2 ) are the left and right eigenvector corresponding to the eigenvalue q, respectively. The transition probabilities are The constant in the error term is ξ = 1 q . The exit weights are (2, 1, 2) · q+1 q+2 . With these transition probabilities, the asymptotic frequencies of the digits (cf. [6]) can be computed as by using the stationary distribution given in (5).

Asymptotic analysis of the standard addition
In this section, we use the probabilistic model defined in Sect. 4 for the input sequence of the transducers in Sect. 3.2. Then we will use Lemma 5.1 to obtain expectation, variance and asymptotic normality of the number of carries. In Sects. 5.1 and 5.2, we will construct probabilistic automata whose transition labels are the carries and where each transition has a weight corresponding to the weight constructed in Sect. 4.
Let m and n be two functions mapping the output of a transition into the real numbers; for brevity we write m(t) and n(t) without mentioning the output label of the transition t. In our setting m will count the number of carries 1, and n the number of carries −1 of the output of a transition. We consider the two random variables M and N which are the sum of the values of m and n, respectively, over a path of length with probability the product of the weights of this path multiplied with the exit weight.
We will use multivariate generating functions in three variables x, y and z. The variables x and y mark the number of carries 1 and −1, respectively, and the variable z marks the length of the expansion.
The transition matrix A(x, y) of a probabilistic automaton with K states and two functions m and n is a K × K matrix whose (i, j)-th entry is where p t is the weight of the transition t.
The next lemma is a slight modification of [5, Theorem 3.9] taking into account the non-uniform distribution of the input alphabet. e n = f y f z (1,1,1) .
Furthermore, if v m and v n are non-zero, then M and N are asymptotically normally distributed, respectively. If the variance-covariance matrix is non-singular, then M and N are asymptotically jointly normally distributed.
Proof The moment generating function is where e 1 is a unit vector with a 1 at the position of the initial state and the entries of w F are the exit weights of the states. Since the automaton is probabilistic and aperiodic, the unique dominant eigenvalue of A(1, 1) is 1. Thus the same arguments apply as in [5] after replacing "complete" by "probabilistic". We obtain the same formulas for the constants of the expectation, the variance and the covariance. Also the central limit theorem follows.

Standard addition for (q, d)-expansions
To construct the probabilistic automaton, we start with the transducer in Fig. 1, and use the weights from Sect. 4.1.
All steps in this section, including the computation of the constants in Theorem 1, can be done in the mathematical software system SageMath [13] by using the included finite state machine package described in [3]. The corresponding SageMath file is available at [4].
The construction in this section is more general than needed for the case of independent digits as in (q, d)-expansions. But discussing it here in full generality allows reusing the same ideas for the case of dependent digits as in SSDEs later on. We will use the same construction for SSDEs in Sects. 5.2 and 7.
In this section, let A be the automaton in Fig. 3, equipped with the weight 1 q for every transition and the exit weight 1 for every state (by Sect. 4.1). Construct A 2 as the additive Cartesian product 1 of A with itself by the following rules: • The states of A 2 are pairs of states of A.
• There is a transition from (a, b) to (c, d) with label x + y in A 2 if there are transitions from a to c with label x and b to d with label y in A. • The weight of a transition in A 2 is the product of the weights of the two transitions in A. • The exit weight of a state in A 2 is the product of the exit weights of the two states in A. The probabilistic automaton A 2 recognizes all possible sequences s of digitwise sums with the correct weights for the equidistribution on the independent (q, d)-expansions x and y.
In this section, let B be the transducer in Fig. 1 performing the standard addition of two (q, d)-expansions. Next, we construct S (q,d) as the composition B • A 2 by the following rules: • The states of S (q,d) are pairs of states of B and A 2 .
• For each pair of transitions from a to c with input label s and output label k in B and from b to d with weight w and label s in A 2 , there is a transition from (a, b) to (c, d) with weight w and label k in S (q,d) . • The exit weight of a state in S (q,d) is the exit weight of the corresponding state in A 2 .
The probabilistic automaton S (q,d) recognizes the sequence of carries c with the correct weights for the equidistribution on the independent (q, d)-expansions x and y. The probabilistic automaton S (q,d) has three states.
To determine the transition matrix of S (q,d) , we use the following lemma to compute the number of transitions between two states. The lemma is proved by an inclusionexclusion argument.
Then we have with N (0, ∞, 0, ∞, 0, s max ) = 0 if s max is negative and otherwise. This gives the transition matrix in Table 6 in the appendix where x marks carries 1 and y marks carries −1. For example, the entry in the first row and column is because this entry corresponds to the transitions from −1 to −1 with input label L + 1 and output label1 in B and from (0, 0) to (0, 0) in A 2 .
With the transition matrix, the next theorem follows directly from Lemma 5.1. N ) is (e 1 , e −1 ) + O(1) with constants

Theorem 1 Let M and N be the number of carries 1 and −1, respectively, when adding two independent random (q, d)-expansions of length . The expected value of (M ,
Furthermore, the number of carries 1 and −1 is asymptotically jointly normally distributed for d = 0, −q + 1. For d = 0, M is asymptotically normally distributed and N = 0 because the carry −1 does not occur. For d = −q + 1, the same holds with M and N exchanged. In Fig. 5, variances and covariance for (10, d)-expansions are shown.

Remark 5.3
The expected value for carries in the addition of (q, d)-expansions corresponds to the result in [9]. There, the authors find the stationary distribution for the states (−1, 0, 1) of the carry process. For d = −q+1 2 , this stationary distribution can also be found in [1].

Standard addition for SSDEs
To cope with the dependencies between the digits, we have to combine the conditional probabilities of the automaton in Fig. 4 with the carries computed by the automaton in Fig. 2. This is done in the same way as in Sect. 5.1.
All steps in this section, including the computation of the constants in Theorem 2, can be done in the mathematical software system SageMath [13] by using its included finite state machine package described in [3]. The corresponding SageMath file is available at [4].
In this section, let A be the automaton in Fig. 4 equipped with the weights in (6) and let B be the transducer in Fig. 2 performing the standard addition of two SSDEs.
We first construct the additive Cartesian product A 2 , recognizing all possible sequences s of digitwise sums with the correct weights approximating the equidistribution on two independent SSDEs x and y. This probabilistic automaton has 9 states.
Next, we construct S SSDE as the composition B • A 2 . This probabilistic automaton recognizes the sequence of carries c with the correct weights approximating the equidistribution on two independent SSDEs x and y. This gives a transducer with 45 states.
Because of symmetries (cf. Remark 3.1), we can simplify S SSDE such that it has only 14 states 2 :

Lemma 5.4 A probabilistic automaton can be simplified by applying the following rules:
• If between two states, there are two transitions with the same label, then these two transitions can be combined. The weights are summed up in this process.

partition of the states of the automaton with the following property: If a, b ∈ C j are two states, then there is a bijection between the transitions leaving a and the ones leaving b which preserves the label, the weight of the transition and into which set of the partition the transitions lead. These bijections define an equivalence relation on the transitions leaving a set of the partition. Then each set of the partition can be contracted to a new state. For each equivalence class of transitions, there is one transition in the simplified transducer.
Thus, we obtain a 14×14 transition matrix of S SSDE given in Table 7 in the appendix (using Lemma 5.2).

Theorem 2
The expected value of the number of carries equal to 1 when adding two SSDEs of length is q 2 + 2q + 4 8(q + 1) 2 + O (1) and the variance is The same result holds for carries equal to −1. The covariance between carries 1 and −1 is The number of carries 1 and −1 is asymptotically jointly normally distributed.
In Fig. 6, variance and covariance for SSDEs are shown. Proof We can compute the determinant f (x, y, z) = det(I − z A(x, y)) of the transition matrix A(x, y) in the appendix of the simplified automaton S SSDE with 14 states. Thus, Lemma 5.1 implies the expected value, the variance and the central limit theorem where the input sequence is the sum of two independent SSDEs of length with the approximate equidistribution W . As the (exact) equidistribution P satisfies P = (1 + O(ξ ))W , these results also hold for the (exact) equidistribution.

Von Neumann's addition
In this section, we analyze von Neumann's addition algorithm for SSDEs, a parallel algorithm using several iterations. This algorithm was analyzed by Knuth in [8] for standard q-ary expansions. In [7], this analysis was extended to (q, d)-expansions and SSDEs. However, for q ≥ 4, the hardware and software available at that time made the use of the probabilistic model of Sect. 4.2 computationally infeasible. The approximate model described in Remark 5.5 was used instead. As Remark 5.5 demonstrates, this approximation may lead to different main terms in the expectation and the variance. As before, we choose an approximate equidistribution for all independent pairs of SSDEs of length as our probabilistic input model. In contrast to the result in [7], we obtain more natural constants occurring in the main term of the expectation and the variance.
For von Neumann's addition of two standard q-ary digit expansions, the number of iterations depends on the longest subsequence (q − 1) . . . (q − 1) j with j ≥ q of the digitwise sum s, see [8]. Such sequences can be found by an automaton with two classes of transitions (see Fig. 7 and [7, Figure 1]). One class corresponds to the digit (q − 1) of a carry generating sequence and is depicted by solid lines. The other class corresponds to all other digits (including the digit j of a carry generating sequence) and is depicted by dotted lines. The longest consecutive run of solid edges in the automaton in Fig. 7 corresponds to the number of iterations of von Neumann's addition minus 2. The asymptotic analysis of these longest runs can be performed using the probabilistic version of the automaton in Fig. 7. We will extend this approach to SSDEs with arbitrary even base using a larger probabilistic automaton in Sect. 7. Fig. 7 Automaton to find the longest carry generating sequence for von Neumann's addition of two standard q-ary expansions

Algorithm
Let x and y be two SSDEs. The idea of the algorithm is to construct the sequence of digitwise sums s = x + y and correct each position if the number at this position is not in the digit set or at the border of the digit set where we have to take into account the syntactical rule. In Table 5, an example for von Neumann's addition for SSDEs with q = 4 is given.
As in [7], we define (z, c) = add(s) with s = x + y by otherwise, Here, the choice of the carry c j+1 corresponds to the one in Algorithm 2. By iterating this step we obtain (z (k+1) , c (k+1) ) = add(z (k) + c (k) ) with z (0) = x and c (0) = y. If c (k) = 0, then z (k) is the SSDE of the sum x + y and the algorithm stops. Note that during this process, z (k) and c (k) are not necessarily SSDEs. In [7], the correctness and the termination of this algorithm were proved. We denote the number of iterations of von Neumann's addition algorithm by t (x, y) = min{k ≥ 0 : c (k) = 0}.

Automaton
A description of all SSDEs x and y with t (x, y) = k is given in [7]. This description is in terms of an automaton and leads to the automaton in [7, Figure 5] reproduced here as   after reading s J , an additional solid edge will be traversed. Thus, we have to specially treat the states 2 and 7.

Asymptotic analysis of von Neumann's addition
For the asymptotic analysis, we combine the automaton in Fig. 8 with the probabilistic model for SSDEs from Sect. 4.2 in the same way as in Sect. 5.2. All steps in this section, including the computation of the constants in Theorem 4, can be done in the mathematical software system SageMath [13] by using the included finite state machine package described in [3]. The corresponding SageMath file is available at [4].
We again use the automata A and A 2 described in Sect. 5.2, recognizing SSDEs and the digitwise sum of two SSDEs, respectively. As before, the next step is to construct the Cartesian product N SSDE of the automaton B in Fig. 8 and A 2 .
In this case, the simplification is done in the same way as in Lemma 5.4, but also taking into account the class (dotted or solid) of a transition. The partition of the set of states was constructed by the symmetries between the two sequences x and y described in Remark 3.1, for example {(1, (−1, 1)), (1, (1, −1))}, and the additional vertical symmetry of the automaton in Fig. 8, for example {(4, 1, 1), (9, −1, −1)}. The state (1, (0, 0)) is initial and all states are final. The next theorem is an extension of Lemma 2.5 in [7] additionally including the variance and convergence in distribution.
where r 0 , s 0 , and s 1 are real polynomials in z (not depending on k). Furthermore, r 1 and s 2 are real polynomials in z, (z/a 1 ) k , …, (z/a m ) k for some m ≥ 2 and some real numbers 1 < a := a 1 < |a 2 | ≤ |a 3 | ≤ · · · ≤ |a m | such that each of the summands in r 1 is divisible by one of the terms (z/a 1 ) k , …, (z/a m ) k and each of the summands in s 2 is divisible by one of the terms (z/a 1 ) 2k , (z/a 2 ) k , …, (z/a m ) k . Define Assume furthermore that r 0 (1) = 0, that s 0 does not have any zero in |z| ≤ 1 and that δ > 0.
Let (X ) ≥ 0 be the sequence of random variables with support N 0 defined by Then the asymptotic formula holds as → ∞ for k = log a +O(1). Hence the shifted random variable X −log a converges weakly to a limiting distribution if runs through a subset of the positive integers such that the fractional part {log a } of log a converges. The expected value of X is the variance is VX = π 2 6 log 2 a + 1 12 where γ is the Euler-Mascheroni constant, and 0 (x) and 1 (x) are periodic functions (with period 1 and mean value 0), given by the Fourier expansions Proof Parts of the proof of this theorem follow along the same lines as the proof of Lemma 2.5 in [7]. However, we include all steps of the proof for the sake of readability. Without loss of generality, we can assume r 0 (1)/s 0 (1) = 1, as otherwise w k and w are multiplied by a constant. Let 0 ≤ k 1 ≤ k 2 ≤ k 3 denote suitable constants.
For some C > 0 such that there is no root of s 0 inside {z : |z| ≤ 1 + 2C} and such that (1 + C)/a < 1, we have for |z| = 1 + C and k ≥ k 1 . By Rouché's Theorem, we conclude that for k ≥ k 1 , S k (z) has exactly one simple root in the disk {z : |z| ≤ 1 + C}.
Since G k and G are rational functions, G k and G can be continued analytically beyond their dominant singularities ζ k and 1, respectively. We have lim k→∞ Res z=ζ k G k (z) = Res z=1 G(z) = 1. Thus [11,Theorem 1] implies (12) and the limiting distribution.
The coefficients w k and w of z in G k and G, respectively, coincide for k ≥ . Thus the support of X is finite. Furthermore, the condition on s 0 implies that w = 1+O(κ ) for a constant 0 ≤ κ < 1 by singularity analysis. Thus, the expectation is Using the residue theorem and the assumption r 0 (1) = s 0 (1), we get Replacing w k with exp(− δ/a k ) yields the error terms Thus, (13) follows from (17), (19) and the well known fact (see e.g. [2]) that with the periodic function 0 (x) given in (15). The second moment is As k=0 (1 − w k ) has already been computed for the expectation, we are left with k=0 k(1 − w k ). We use (18) to obtain The Mellin transform (see [2]) of the harmonic sum for −1 < s < 0. The singular expansion of this Mellin transform at s = 0 is for χ n = 2πin log a . Thus, − log a x log a n =0 (−χ n ) exp(2πin log a x) . (20), (17), (13) and (21) give the variance as stated in (14).

Theorem 4 Let q ≥ 2 be even. Then the expected number of iterations when adding two SSDE of length with von Neumann's algorithm is
where δ = (q − 1)(4q 10 + 10q 9 + 18q 8 − 4q 7 − 10q 6 + 7q 5 + 44q 4 − 29q 3 − 8q 2 − 20q + 16) is a 1-periodic function with mean 0 given by the Fourier expansion The variance of the number of iterations is π 2 6 log 2 q + 1 12 (24) where 1 is a 1-periodic function with mean 0 given by the Fourier expansion The asymptotic formula holds as → ∞ for k = log q + O (1). The random variable t (X, Y ) − log q converges weakly to a double exponential random variable if runs through a subset of the positive integers such that the fractional part {log q } converges. Remark 7.1 A similar result for q ≥ 4 was obtained in [7] using the same probabilistic model as in Remark 5.5. This changes the main term of the expected value. In [7], the logarithm of the main term was taken to the base α −1 with α = q −1 − q −4 + O(q −5 ).
In contrast, we here obtain the logarithm of the main term in (22) to the base q, which is a more natural constant appearing in this context. For q = 2, this result is contained in [7].
Proof Let P be the (exact) equidistribution of all SSDE of length . For k > + 2, we know that P (t (X, Y ) ≤ k) = 1 because an input sequence of length traverses at most solid edges in the automaton in Fig. 8. If we use the approximate equidistribution W = (1 + O(ξ ))P of all SSDE of length , an exponentially small error term is introduced. Because of the finite support, this error term does not change the main term of the expectation, the variance and the distribution function. Thus, also the limiting distribution remains the same.
We will use Theorem 3 with the generating function To construct this generating function, we use the same techniques as in [7]. The generating function G k (z) counts the weighted number of paths in the automaton N SSDE of the pattern . . . B + R {1,k} B + R {1,k} . . . where B + is an arbitrary non-empty sequence of dotted transitions and R {1,k} is a non-empty sequence of solid transitions of length at most k. The first transition can be a dotted or a solid transition. We stop with either arbitrarily many dotted transitions or at most k solid transitions, where we have to take into account the special situation in states 2 or 7 in the automaton in Fig. 8 (see also Remark 6.1): Because of the solid transition starting in 2 and 7 with label 0, we are not allowed to stop with k solid transitions in state 2 or 7 but only with at most k − 1 ones.
To find the generating functions for B + and R {1,k} , we use the transition matrices for the dotted and the solid parts of the automaton N SSDE .
Let q ≥ 6. The transition matrix R for the solid transitions of automaton N SSDE is a 12 × 12 matrix given in Table 9 in the appendix (using Lemma 5.2). The transition matrix B for the dotted transitions of automaton N SSDE is given in Table 10 (using Lemma 5.2). The order of the states is given in (11) and also in Table 8 in the appendix.
The (matrix) generating function for arbitrary non-empty dotted paths B + is The entry (i, j) of this matrix is the generating function of non-empty dotted paths of arbitrary length starting in state i and leading to state j. For arbitrary non-empty solid paths, the (matrix) generating function is To obtain the (matrix) generating function R {1,k} for non-empty solid paths R {1,k} of length at most k, we have to restrict each entry of R + corresponding to an infinite geometric series to a finite geometric series. 3 We will illustrate this procedure on the entry at position (5, 1) of R + . The partial fraction decomposition of (26) with respect to z is By truncating the infinite geometric sum (1 − z) −1 after k + 1 summands, i.e., by be the block matrix of total size 24 × 24. Then, the (matrix) generating function of non-empty paths . . .
By Theorem 3, we obtain the expectation, the variance, the distribution function and the limiting distribution of the non-negative truncation of t (x, y) − 2. From (18) and the monotonicity of w k , we know that w −2 = w −1 = O( −2 ). Therefore, the results transfer to the random variable t (x, y) as stated in the theorem.  The order of the states is Table 8 Exit weights of As discussed in (11), the states of N SSDE are equivalence classes of states. For brevity, we list one representative for each state of N SSDE to give the order of the states Table 9 Transition matrix R for the solid transitions in The order of the states is the same as in Table 8  Table 10 Transition matrix B for the dotted transitions in The order of the states is the same as in Table 8