Absolute value

The notion of absolute value is quite important.

Using signed numbers in assembly language asks for two’s complement form. It is not always convenient to use it. Sometimes, it’s better to rewrite a formula in a form that doesn’t need signed numbers. That’s what I have done, for instance, in my early line drawing routine.

There, we had the formula:

y = m * x - m * x1 + y1

Both x and y are known to be positive, since on the C64 bitmap screen, coordinates are always positive or 0. So, only the slope m can be either positive or negative.

If we don’t want to use signed numbers, we must split the above formula somehow. We already did that on the line drawing program, but let’s look at the way things can be set up formally.

The function absolute value associates to the real number a another number |a|. This number, called absolute value, is defined as follows:

|a| = a,  if a >= 0
|a| = -a, if a <  0

If we want to express the above formula without using signed numbers, we then have to use the notation:

y = |m| * x - |m|* x1 + y1

Using the above definition of absolute value, this leads to the formulas:

y = m * x - m * x1 + y1,    if m >= 0

y = -m * x - (-m)*x1 + y1,  if m <  0

Which can be rewritten as follows:

(1)   y = y1 + m * x - m * x1,    if m >= 0

(2)   y = y1 + m*x1 - m * x,      if m <  0

It’s important to note that on these formulas, we must only use the unsigned m, as we have already dealt with the sign when building the formulas themselves. So, if we have m = -5, we must use the formula (2) by letting m = 5 (not -5). We just have to replace the letter m with the digit 5, leaving all signs in the formulas untouched.

With (1) and (2), we don’t have any partial negative result, so we can use unsigned numbers. These are the same formulas used in the lines drawing program mentioned above. But now, we have obtained them using the formal definition of absolute value.

Note: in computer science, unsigned numbers actually means positive numbers.

Please note that if we used signed numbers (with the two’s complement form), we would have had the following disadvantages:

  • one bit is used for sign, so we have smaller numbers taking the same amount of memory;
  • we need to code signed multiply routines.

 

Insert math as
Block
Inline
Additional settings
Formula color
Text color
#333333
Type math using LaTeX
Preview
\({}\)
Nothing to preview
Insert