# Math Whiz? Formula for Absolute Value

**URL:** <https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129>\
**Category:** Factual Questions\
**Created:** [August 27, 2007, 6:10pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129 "2007-08-27T18:10:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiggs](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@jiggs](https://boards.straightdope.com/u/jiggs)\
**Post date:** [August 27, 2007, 6:10pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/1 "2007-08-27T18:10:09Z")

</div>

For an oddball problem at work I have a need to calculate the absolute value of a number. I am dealing with a very primitive calculator that has +, -, \*, / and a power/exponent function that will only accept positive integer exponents. The calculator can be programmed with a formula using these operators and accept a single input number.

For reasons far too unglamorous and boring to go into here, we need this primitive calculator to take as input a positive or negative number and return the absolute value as the result.

Try as we might, we can’t seem to come up with a formula for getting this done. Any math whiz out there who can tell us how, perhaps with some optional belittling of our puny little math-impaired brains?

Thanks!!

---

<div class="post-metadata">

**Author:** ![beowulff](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/beowulff/32/542_2.png) [@beowulff](https://boards.straightdope.com/u/beowulff)\
**Post date:** [August 27, 2007, 6:16pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/2 "2007-08-27T18:16:08Z")

</div>

I don’t think it can be done with the conditions you have described.

---

<div class="post-metadata">

**Author:** ![Billdo](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/billdo/32/315_2.png) [@Billdo](https://boards.straightdope.com/u/Billdo)\
**Post date:** [August 27, 2007, 6:16pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/3 "2007-08-27T18:16:13Z")

</div>

Does the calculator have a square root function. You can if so, you can use the function:

square root ((input number)^2).

---

<div class="post-metadata">

**Author:** ![TheManTwo](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@TheManTwo](https://boards.straightdope.com/u/TheManTwo)\
**Post date:** [August 27, 2007, 6:18pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/4 "2007-08-27T18:18:46Z")

</div>

Do you have a square root function or can you use X^.5? If so you can just take the original number, square it, then take the square root of it. That should always return the absolute value.

---

<div class="post-metadata">

**Author:** ![jiggs](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@jiggs](https://boards.straightdope.com/u/jiggs)\
**Post date:** [August 27, 2007, 6:19pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/5 "2007-08-27T18:19:06Z")

</div>

Sorry, no square root. Only positive integer powers.

---

<div class="post-metadata">

**Author:** ![Arnold\_Winkelried](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@Arnold\_Winkelried](https://boards.straightdope.com/u/Arnold_Winkelried)\
**Post date:** [August 27, 2007, 6:31pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/6 "2007-08-27T18:31:20Z")

</div>

let x= input number  
y = x[sup]2[/sup]

z[sub]0[/sub] = 2  
z[sub]1[/sub] = (z[sub]0[/sub] + (y/z[sub]0[/sub])) / 2  
z[sub]2[/sub] = (z[sub]1[/sub] + (y/z[sub]1[/sub])) / 2  
etc.

determine how many steps you want to go through in the above iteration. let’s say you want to stop at z[sub]10[/sub]. It is trivial (but tedious) to write the formula for z[sub]10[/sub], e.g.

```auto

z[sub]2[/sub] = (((2 + (y/2)) / 2)+ (y/((2 + (y/2)) / 2))) / 2

```

---

<div class="post-metadata">

**Author:** ![Chronos](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/chronos/32/134_2.png) [@Chronos](https://boards.straightdope.com/u/Chronos)\
**Post date:** [August 27, 2007, 6:45pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/7 "2007-08-27T18:45:39Z")

</div>

These more complicated formulae that you can write… Can they include conditionals? Because the best way to write an absolute value function is to just say something like “if x \< 0: x = -x”

Alternately, is there some restriction on the values which can be input to this absolute value function? If, for instance, it only needs to handle integers in the range [-10,10], then you could write a 20th-order polynomial which will return the correct value for every input. I really wouldn’t recommend this, though, both because a 20th-order polynomial is pretty clunky, and because for any other value than those it’s specifically designed for, it’ll go absolutely haywire.

The real answer is, if your calculator can’t do conditionals, and doesn’t have a built-in absolute value function, then you’re using the wrong tool for the job. Go yell at whoever you need to to get a different calculator.

---

<div class="post-metadata">

**Author:** ![pulykamell](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/pulykamell/32/3166_2.png) [@pulykamell](https://boards.straightdope.com/u/pulykamell)\
**Post date:** [August 27, 2007, 7:54pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/8 "2007-08-27T19:54:17Z")

</div>

[QUOTE=Chronos]

The real answer is, if your calculator can’t do conditionals, and doesn’t have a built-in absolute value function, then you’re using the wrong tool for the job. Go yell at whoever you need to to get a different calculator.  
[/QUOTE]

There’s also a method to do this using a bit shift, an addition, and an exclusive or (XOR), but I assume that functionality will not be available on the calculator. Otherwise, for those curious, here’s the formula (assuming a 32-bit word. Otherwise, replace 31 with whatever your word length is minus one).

a = (a\>\>31)^(a + (a\>\>31));

---

<div class="post-metadata">

**Author:** ![jiggs](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@jiggs](https://boards.straightdope.com/u/jiggs)\
**Post date:** [August 27, 2007, 8:42pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/9 "2007-08-27T20:42:19Z")

</div>

[QUOTE=Chronos]  
These more complicated formulae that you can write… Can they include conditionals?  
[/QUOTE]

No, the problem would indeed be trivial in that case.

[QUOTE=Chronos]  
Alternately, is there some restriction on the values which can be input to this absolute value function?  
[/QUOTE]

Decimal numbers in the range of about positive to negative 99,999,999.99.

[QUOTE=Chronos]  
The real answer is, if your calculator can’t do conditionals, and doesn’t have a built-in absolute value function, then you’re using the wrong tool for the job. Go yell at whoever you need to to get a different calculator.  
[/QUOTE]

It’s actually an old program written for a piece of hardware. We don’t have the source, or I’d have skinned this cat pdq.

---

<div class="post-metadata">

**Author:** ![jiggs](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@jiggs](https://boards.straightdope.com/u/jiggs)\
**Post date:** [August 27, 2007, 8:59pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/10 "2007-08-27T20:59:59Z")

</div>

[QUOTE=Arnold Winkelried]  
let x= input number  
y = x[sup]2[/sup]

z[sub]0[/sub] = 2  
z[sub]1[/sub] = (z[sub]0[/sub] + (y/z[sub]0[/sub])) / 2  
z[sub]2[/sub] = (z[sub]1[/sub] + (y/z[sub]1[/sub])) / 2  
etc.

determine how many steps you want to go through in the above iteration. let’s say you want to stop at z[sub]10[/sub]. It is trivial (but tedious) to write the formula for z[sub]10[/sub], e.g.

```auto

z[sub]2[/sub] = (((2 + (y/2)) / 2)+ (y/((2 + (y/2)) / 2))) / 2

```

[/QUOTE]

I worked through the z[sub]2[/sub] formula for an 8 input and came up with a result of 2.833. Not exactly close, so I gather I’m doing something wrong, or do I have to get out to z[sub]50[/sub] or so in order to get anywhere close? Or am I missing something entirely about the formula?

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [August 27, 2007, 9:18pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/11 "2007-08-27T21:18:35Z")

</div>

With the operators +, -, \* and /, you can generate the set of rational functions (i.e., functions of the form P(x)/Q(x), where P and Q are both polynomial functions). Rational functions are differentiable at every point where they’re continuous. |x| is continuous at 0 and not differentiable there, so it’s not a rational function. You’re SOL.

---

<div class="post-metadata">

**Author:** ![Joey\_P](https://avatars.discourse-cdn.com/v4/letter/j/919ad9/32.png) [@Joey\_P](https://boards.straightdope.com/u/Joey_P)\
**Post date:** [August 27, 2007, 9:39pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/12 "2007-08-27T21:39:52Z")

</div>

x[sup]2[/sup] - (x + x[sup]2[/sup])  
I thought this would do it, but it seems to return the opposite value.

---

<div class="post-metadata">

**Author:** ![Arnold\_Winkelried](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@Arnold\_Winkelried](https://boards.straightdope.com/u/Arnold_Winkelried)\
**Post date:** [August 27, 2007, 9:50pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/13 "2007-08-27T21:50:47Z")

</div>

[QUOTE=jiggs]  
I worked through the z[sub]2[/sub] formula for an 8 input and came up with a result of 2.833. Not exactly close, so I gather I’m doing something wrong  
[/QUOTE]  
Yes you did something wrong, I think you missed the “y = x[sup]2[/sup]” step in my instructions.

```auto

x = 8
y = 64
z0 = 2
z1 = (2 + 64 / 2) / 2 = 17
z2 = (17 + 64 / 17) / 2 = 10.38235294
z3 = (10.38235294 + 64 / 10.38235294) / 2 = 8.273329445
z4 = (8.273329445 + 64 / 8.273329445) / 2 = 8.00451505

```

---

<div class="post-metadata">

**Author:** ![Arnold\_Winkelried](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@Arnold\_Winkelried](https://boards.straightdope.com/u/Arnold_Winkelried)\
**Post date:** [August 27, 2007, 9:57pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/14 "2007-08-27T21:57:17Z")

</div>

[QUOTE=Joey P]  
x[sup]2[/sup] - (x + x[sup]2[/sup])  
I thought this would do it, but it seems to return the opposite value.  
[/QUOTE]  
x[sup]2[/sup] - (x + x[sup]2[/sup]) = x[sup]2[/sup] - x - x[sup]2[/sup] = -x

Not what the OP needs.

---

<div class="post-metadata">

**Author:** ![Arnold\_Winkelried](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@Arnold\_Winkelried](https://boards.straightdope.com/u/Arnold_Winkelried)\
**Post date:** [August 27, 2007, 10:12pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/15 "2007-08-27T22:12:01Z")

</div>

Here’s an expanded version of what I posted. Not a good way of doing it, but I can’t think of another way to solve your problem with the given constraints.

find absolute value of input x: square x and then use an approximation method to find the square root of the square of x

for z[sub]0[/sub], choose a value as close to x as possible to converge quickly. In my case, I just make a crappy guess, z[sub]0[/sub] = 2

z[sub]0[/sub] = 2

z[sub]n+1[/sub] = ((z[sub]n[/sub] + (x[sup]2[/sup] / z[sub]n[/sub])) / 2)

so, expanding:

z[sub]1[/sub] = ((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2)

z[sub]2[/sub] = ((((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2) + (x[sup]2[/sup] / ((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2))) / 2)

z[sub]3[/sub] = ((((((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2) + (x[sup]2[/sup] / ((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2))) / 2) + (x[sup]2[/sup] / ((((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2) + (x[sup]2[/sup] / ((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2))) / 2))) / 2)

z[sub]4[/sub] = ((((((((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2) + (x[sup]2[/sup] / ((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2))) / 2) + (x[sup]2[/sup] / ((((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2) + (x[sup]2[/sup] / ((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2))) / 2))) / 2) + (x[sup]2[/sup] / ((((((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2) + (x[sup]2[/sup] / ((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2))) / 2) + (x[sup]2[/sup] / ((((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2) + (x[sup]2[/sup] / ((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2))) / 2))) / 2))) / 2)

z[sub]5[/sub] = ((((((((((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2) + (x[sup]2[/sup] / ((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2))) / 2) + (x[sup]2[/sup] / ((((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2) + (x[sup]2[/sup] / ((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2))) / 2))) / 2) + (x[sup]2[/sup] / ((((((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2) + (x[sup]2[/sup] / ((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2))) / 2) + (x[sup]2[/sup] / ((((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2) + (x[sup]2[/sup] / ((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2))) / 2))) / 2))) / 2) + (x[sup]2[/sup] / ((((((((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2) + (x[sup]2[/sup] / ((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2))) / 2) + (x[sup]2[/sup] / ((((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2) + (x[sup]2[/sup] / ((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2))) / 2))) / 2) + (x[sup]2[/sup] / ((((((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2) + (x[sup]2[/sup] / ((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2))) / 2) + (x[sup]2[/sup] / ((((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2) + (x[sup]2[/sup] / ((z[sub]0[/sub] + (x[sup]2[/sup] / z[sub]0[/sub])) / 2))) / 2))) / 2))) / 2))) / 2)

etc.

---

<div class="post-metadata">

**Author:** ![The\_Hamster\_King](https://avatars.discourse-cdn.com/v4/letter/t/8edcca/32.png) [@The\_Hamster\_King](https://boards.straightdope.com/u/The_Hamster_King)\
**Post date:** [August 27, 2007, 10:57pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/16 "2007-08-27T22:57:09Z")

</div>

Depending on how the calculator handles overflow conditions you might be able to exploit that to get absolute value.

For example, say the maximum number the calculator can hold is MAX\_INT. And any operations that calculate a result higher than MAX\_INT return MAX\_INT instead. And operations that calculate a result lower than -MAX\_INT return -MAX\_INT instead.

Then you can get absolute value with the following formula:

abs(x) = x \* ((x \* MAX\_INT) / MAX\_INT)

Essentially the overflow condition can be used as a ceiling function, and once you have a ceiling function you can get absolute value. But all of this is highly dependent upon how your particular hardware handles overflow.

---

<div class="post-metadata">

**Author:** ![John\_Mace](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/john_mace/32/185_2.png) [@John\_Mace](https://boards.straightdope.com/u/John_Mace)\
**Post date:** [August 27, 2007, 11:06pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/17 "2007-08-27T23:06:00Z")

</div>

[QUOTE=Pochacco]  
Depending on how the calculator handles overflow conditions you might be able to exploit that to get absolute value.

For example, say the maximum number the calculator can hold is MAX\_INT. And any operations that calculate a result higher than MAX\_INT return MAX\_INT instead. And operations that calculate a result lower than -MAX\_INT return -MAX\_INT instead.

Then you can get absolute value with the following formula:

abs(x) = x \* ((x \* MAX\_INT) / MAX\_INT)  
[/QUOTE]

What if x is between -1 and 1?

---

<div class="post-metadata">

**Author:** ![brazil84](https://avatars.discourse-cdn.com/v4/letter/b/8491ac/32.png) [@brazil84](https://boards.straightdope.com/u/brazil84)\
**Post date:** [August 27, 2007, 11:25pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/18 "2007-08-27T23:25:38Z")

</div>

[QUOTE=jiggs]  
For an oddball problem at work I have a need to calculate the absolute value of a number. I am dealing with a very primitive calculator that has +, -, \*, / and a power/exponent function that will only accept positive integer exponents. The calculator can be programmed with a formula using these operators and accept a single input number.  
[/quote]

I don’t think this is a math question so much as a hacking question.

From the math perspective, any function will be a polynomial on x. You need a function with a first derivative that is always 1 when x is positive and always -1 when X is negative. It is hard to see how you get get this with a polynomial, even if you allow for negative (but integral) exponenents.

What I would suggest is this: Many calculators treat the input 0 - - x the same as 0 - x. Or 0 - - - x the same as 0 - - x.

So I would play with it to see if you can get the calculator to disregard a leading minus sign before the input.

---

<div class="post-metadata">

**Author:** ![Harmonious\_Discord](https://avatars.discourse-cdn.com/v4/letter/h/74df32/32.png) [@Harmonious\_Discord](https://boards.straightdope.com/u/Harmonious_Discord)\
**Post date:** [August 28, 2007, 12:03am UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/19 "2007-08-28T00:03:14Z")

</div>

N= number input  
L = variable  
R= result

L= N\*N  
R= L^.5

R=(N\*N)^.5

This works for what I take it to be you wish to do.

Well I missed the integer part.

---

<div class="post-metadata">

**Author:** ![Harmonious\_Discord](https://avatars.discourse-cdn.com/v4/letter/h/74df32/32.png) [@Harmonious\_Discord](https://boards.straightdope.com/u/Harmonious_Discord)\
**Post date:** [August 28, 2007, 12:21am UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/20 "2007-08-28T00:21:54Z")

</div>

N= Number input  
R=Result  
Eraser

I’m done for a while.

[Next page](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129.md?page=2)
