libzahl

big integer library
git clone git://git.suckless.org/libzahl
Log | Files | Refs | README | LICENSE

commit a70f79dfb22e8ea00231f5739f89ecc8d552643f
parent 3a3ea3e654fa7131c0812977f92dc923d4daf9ce
Author: Mattias Andrée <maandree@kth.se>
Date:   Wed, 11 May 2016 22:26:27 +0200

On sign manipulation

Signed-off-by: Mattias Andrée <maandree@kth.se>

Diffstat:
doc/arithmetic.tex | 65++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 60 insertions(+), 5 deletions(-)

diff --git a/doc/arithmetic.tex b/doc/arithmetic.tex @@ -91,10 +91,10 @@ be detrimental to libzahl's simplicity. in-place operation: \begin{alltt} - zadd(a, a, b); - zadd(b, a, b); \textcolor{c}{/* \textrm{should be avoided} */} - zadd_unsigned(a, a, b); - zadd_unsigned(b, a, b); \textcolor{c}{/* \textrm{should be avoided} */} + zadd(a, a, b); + zadd(b, a, b); \textcolor{c}{/* \textrm{should be avoided} */} + zadd_unsigned(a, a, b); + zadd_unsigned(b, a, b); \textcolor{c}{/* \textrm{should be avoided} */} \end{alltt} \noindent @@ -137,4 +137,59 @@ TODO % zpow zpowu zmodpow zmodpowu \section{Sign manipulation} \label{sec:Sign manipulation} -TODO % zabs zneg +libzahl provides two functions for manipulating +the sign of integers: + +\begin{alltt} + void zabs(z_t r, z_t a); + void zneg(z_t r, z_t a); +\end{alltt} + +{\tt zabs} stores the absolute value of {\tt a} +in {\tt r}, that is, it creates a copy of +{\tt a} to {\tt r}, unless {\tt a} and {\tt r} +are the same reference, and then removes its sign; +if the value is negative, it becomes positive. + +\vspace{1em} +\( + r \gets \lvert a \rvert = + \left \lbrace \begin{array}{rl} + -a & \quad \textrm{if}~a \le 0 \\ + +a & \quad \textrm{if}~a \ge 0 \\ + \end{array} \right . +\) +\vspace{1em} + +{\tt zneg} stores the negated of {\tt a} +in {\tt r}, that is, it creates a copy of +{\tt a} to {\tt r}, unless {\tt a} and {\tt r} +are the same reference, and then flips sign; +if the value is negative, it becomes positive, +if the value is positive, it becomes negative. + +\vspace{1em} +\( + r \gets -a +\) +\vspace{1em} + +Note that there is no function for + +\vspace{1em} +\( + r \gets -\lvert a \rvert = + \left \lbrace \begin{array}{rl} + a & \quad \textrm{if}~a \le 0 \\ + -a & \quad \textrm{if}~a \ge 0 \\ + \end{array} \right . +\) +\vspace{1em} + +\noindent +calling {\tt zabs} followed by {\tt zneg} +should be sufficient for most users: + +\begin{alltt} + #define my_negabs(r, a) (zabs(r, a), zneg(r, r)) +\end{alltt}