# 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:** 2

<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:32am UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/21 "2007-08-28T00:32:40Z")

</div>

I had it right if you didn’t mean no negative base numbers for the ^ function.  
In which case I will leave you going “Dooh!” Homer Simpson style.

R=N\*(N^0)

---

<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 28, 2007, 12:33am UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/22 "2007-08-28T00:33:57Z")

</div>

[QUOTE=Harmonious Discord]  
I had it right if you didn’t mean no negative base numbers for the ^ function.  
In which case I will leave you going “Dooh!” Homer Simpson style.

R=N\*(N^0)  
[/QUOTE]

N^0 = 1, so R = N. As I explained in post #11, this is impossible.

---

<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 28, 2007, 12:59am UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/23 "2007-08-28T00:59:06Z")

</div>

[QUOTE=Arnold Winkelried]  
Yes you did something wrong, I think you missed the “y = x[sup]2[/sup]” step in my instructions.  
[/QUOTE]

Yup, sure did. Thought you had just done a superscript when you meant to do a subscript.

Your solution is impressive (not to mention the quantity of typing - wow!), but I see it takes at least 5 or more iterations to get to a reasonably close absolute value (we mostly deal with 2 decimals, but 6 is the program’s limit and we’d have to be good to there).

Unfortunately I didn’t mention one additional limitation of our crappy program because I didn’t think it would come up. We can’t feed it a formula of more than 128 characters, and it looks like getting a good approximation with this method is going to blow that limit out of the water pretty quick.

So you definitely won the battle, but the war may be lost.

My greatest appreciation for everyone who has taken the time to cogitate on this li’l conundrum. Another poster who seems to know his/her math seems to have proof (unintelligible to my pea brain, unfortunately) that given the parameters of the problem there can be no viable solution. If nothing else, I can print out this thread and tell my boss that if the SD cognoscenti can’t solve 'er, she ain’t solvable!

---

<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, 1:25am UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/24 "2007-08-28T01:25:14Z")

</div>

Two decimals of accuracy.

R= ((N\*100)^1)/100  
Try that.

---

<div class="post-metadata">

**Author:** ![Stealth\_Potato](https://avatars.discourse-cdn.com/v4/letter/s/d78d45/32.png) [@Stealth\_Potato](https://boards.straightdope.com/u/Stealth_Potato)\
**Post date:** [August 28, 2007, 6:26am UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/25 "2007-08-28T06:26:59Z")

</div>

[QUOTE=Harmonious Discord]  
Two decimals of accuracy.

R= ((N\*100)^1)/100  
Try that.  
[/QUOTE]

R = ((N \* 100)[sup]1[/sup])/100 = (N\*100)/100 = N

That doesn’t work either… Were you trying for something else, maybe?

---

<div class="post-metadata">

**Author:** ![Indistinguishable](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@Indistinguishable](https://boards.straightdope.com/u/Indistinguishable)\
**Post date:** [August 28, 2007, 7:08am UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/26 "2007-08-28T07:08:54Z")

</div>

**ultrafilter** ’s post #11 is (or should be) the last word on this.

The one thing he left out is your ability to use the ^ operator, so I’ll fill in that detail; suppose you have an expression made from numeric constants, +, -, \*, /, and ^. We can break this up into a bunch of pieces which don’t use ^, and then combine those with ^. As noted by **ultrafilter** , the pieces which don’t use ^ are rational functions. Thus, we have an expression put together out of rational functions and ^. We shall prove by induction that any such expression which is well-defined on all inputs, under the restriction that ^ only takes positive integer exponents, defines a rational function of its input (and thus cannot define the absolute value function): the base case is trivial, where the expression is given directly as a rational function. In the inductive case, we are looking at e1^e2 where e1 and e2 are, by the induction hypothesis, rational functions of their input. For the total expression to be well-defined on all inputs, we need e2 to give only values which are positive integers. The only rational functions which do so are those which are constant. Thus, the expression e1^e2 will be equivalent to e1^n for some constant positive integer n, which will be a rational function (since e1 is). This completes the proof.

(If, incidentally, the ^ operator on your calculator does something other than crash and burn when given non-positive-integer exponents [like, maybe, for some crazy reason, return 0 to indicate failure], the proof fails (to be relevant), and you might be able to get by)

---

<div class="post-metadata">

**Author:** ![Stealth\_Potato](https://avatars.discourse-cdn.com/v4/letter/s/d78d45/32.png) [@Stealth\_Potato](https://boards.straightdope.com/u/Stealth_Potato)\
**Post date:** [August 28, 2007, 7:21am UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/27 "2007-08-28T07:21:30Z")

</div>

[QUOTE=Stealth Potato]  
R = ((N \* 100)[sup]1[/sup])/100 = (N\*100)/100 = N

That doesn’t work either… Were you trying for something else, maybe?  
[/QUOTE]

Oops! Actually, I retract this. I just realized that you meant XOR, not exponentiation. I should have assumed that first, actually, since after years of writing C code, I almost always read “^” as “XOR”. The one time I make an exception, I make a fool of myself. :smack:

---

<div class="post-metadata">

**Author:** ![Indistinguishable](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@Indistinguishable](https://boards.straightdope.com/u/Indistinguishable)\
**Post date:** [August 28, 2007, 7:49am UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/28 "2007-08-28T07:49:25Z")

</div>

Huh? How would XOR make things work out?

---

<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, 8:58am UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/29 "2007-08-28T08:58:44Z")

</div>

Ok I see what my mistake is. A negative raised to a even number is positive. Raise a negative to an odd exponent get a negative result. Mark it down as confusion taken too long to clear.

---

<div class="post-metadata">

**Author:** ![Stealth\_Potato](https://avatars.discourse-cdn.com/v4/letter/s/d78d45/32.png) [@Stealth\_Potato](https://boards.straightdope.com/u/Stealth_Potato)\
**Post date:** [August 28, 2007, 9:25am UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/30 "2007-08-28T09:25:12Z")

</div>

[QUOTE=Indistinguishable]  
Huh? How would XOR make things work out?  
[/QUOTE]

Ah, well, it doesn’t. :smack:

I had just punched it into Windows calculator for a few sample numbers without thinking, and it seemed to work - if I’d been paying attention, I would’ve noticed that Windows calculator conveniently always gives a result from XOR with the sign of the second argument in decimal mode, for some reason presumably known only to the calculator gods.

In actual two’s complement with 32-bit words, if we try, for example, -5, we get:

|-5| ~= ((-5\*100)^1)/100 = (-500^1)/100 = (0xFFFFFE0C ^ 1)/100 = 0xFFFFFE0D / 100 = -499/100 = -4

Which obviously doesn’t work.

In short, nothing to see here! 😛 Obviously if you can use XOR, you’d just want to use |a| = (a\>\>31)^(a + (a\>\>31)), as **pulykamell** suggested.

---

<div class="post-metadata">

**Author:** ![Sage\_Rat](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/sage_rat/32/399_2.png) [@Sage\_Rat](https://boards.straightdope.com/u/Sage_Rat)\
**Post date:** [August 28, 2007, 9:35am UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/31 "2007-08-28T09:35:37Z")

</div>

(x \* x \* -x) / (x \* -x)

---

<div class="post-metadata">

**Author:** ![Sage\_Rat](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/sage_rat/32/399_2.png) [@Sage\_Rat](https://boards.straightdope.com/u/Sage_Rat)\
**Post date:** [August 28, 2007, 9:39am UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/32 "2007-08-28T09:39:57Z")

</div>

Bleh, nevermind.

---

<div class="post-metadata">

**Author:** ![SiouxChief](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@SiouxChief](https://boards.straightdope.com/u/SiouxChief)\
**Post date:** [August 28, 2007, 3:16pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/33 "2007-08-28T15:16:08Z")

</div>

[QUOTE=jiggs]  
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.  
[/QUOTE]

Knowing that it’s a program, can we go the way of Captain Kirk in the [Kobayashi Maru](http://en.wikipedia.org/wiki/Kobayashi_Maru) and “change the scenario”?

Have you tried to disassemble the program?  
Is it possible that we can write other programs to manipulate the input or output of your calculator program?  
What OS + hardware platform are we talking about?

(Or, maybe I should just let it go…)

---

<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 28, 2007, 4:33pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/34 "2007-08-28T16:33:24Z")

</div>

> [@](#):
>
> (If, incidentally, the ^ operator on your calculator does something other than crash and burn when given non-positive-integer exponents [like, maybe, for some crazy reason, return 0 to indicate failure], the proof fails (to be relevant), and you might be able to get by)

This is a good point. What does this calculator do, in the event of an illegal input (such as an exponent other than a positive integer, or a number that’s too large)? If it’s anything other than crash-and-burn, as **Indistinguishable** says, then it’d be possible to kludge together the equivalent of a conditional. How exactly to do this, though, would depend on what exactly the calculator does.

A general note, if you do go this route: A kludge of this order will almost certainly not be portable, even to other machines which are supposed to be 100% compatible with what you’re using now. If you do write such a monstrosity of an absolute value function, be absolutely certain to leave a comment in the code saying that your implementation is badly broken, and that it should be replaced with a real abs() at the first opportunity. Or if your coding environment is so pathological that it doesn’t even allow comments (which at this point wouldn’t surprise me), then leave the comment on a post-it note stuck to the machine, or something. But don’t leave such an abomination to pounce without warning on the next generation who will have to use this machine (or even on yourself, next year when you’ve forgotten what you did now).

---

<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 28, 2007, 6:08pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/35 "2007-08-28T18:08:50Z")

</div>

[QUOTE=SiouxChief]  
Knowing that it’s a program, can we go the way of Captain Kirk in the [Kobayashi Maru](http://en.wikipedia.org/wiki/Kobayashi_Maru) and “change the scenario”?

Have you tried to disassemble the program?  
[/QUOTE]

Before it would come to that I’d just write a new program. We’ve got all the needed tools, just thought it would be nice, not to mention delightfully sneaky, to bend the existing program to our collective will.

Besides, I just figured we were missing some perfectly obvious method to do it. But looks like we weren’t quite as thick as we assumed we were.

---

<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 28, 2007, 6:15pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/36 "2007-08-28T18:15:49Z")

</div>

[QUOTE=Chronos]  
This is a good point. What does this calculator do, in the event of an illegal input (such as an exponent other than a positive integer, or a number that’s too large)?  
[/QUOTE]

That’s the really annoying part. It does a very thorough job of parsing input for illegal conditions. I thought for sure I would beat it with:

(X ^ 2) ^ (1 / 2)

But it looked into my blackhearted attempt and spit it right back. I think I heard giggling coming from inside the machine.

---

<div class="post-metadata">

**Author:** ![Mathochist](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@Mathochist](https://boards.straightdope.com/u/Mathochist)\
**Post date:** [August 28, 2007, 9:53pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/37 "2007-08-28T21:53:23Z")

</div>

1. Look at your calculator. Does the display start with a little horizontal line?
2. If so, multiply by -1.
3. If not, don’t touch it.

---

<div class="post-metadata">

**Author:** ![Quartz](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/quartz/32/267_2.png) [@Quartz](https://boards.straightdope.com/u/Quartz)\
**Post date:** [August 28, 2007, 11:00pm UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/38 "2007-08-28T23:00:19Z")

</div>

Got the formula for calculating a logarithm? We could take the square of the input, get the logarithm, then halve it and re-calculate the exponent.

---

<div class="post-metadata">

**Author:** ![CJJ](https://avatars.discourse-cdn.com/v4/letter/c/ecc23a/32.png) [@CJJ](https://boards.straightdope.com/u/CJJ)\
**Post date:** [August 29, 2007, 12:33am UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/39 "2007-08-29T00:33:35Z")

</div>

I know the OP is long gone, and **ultrafilter** and **Chronos** have given far more practical answers, but I’ve been toying with this problem on and off for the past few hours while taking breaks at work, and think i have a semi-answer.

If i is the input, you can get a pretty close approximation to |i| under the assumed conditions by using the following formula:

```auto

          2*2^(100i)   
|i| ~ { -------------- - 1 } * i
         2^(100i) + 1  

```

100 is arbitrary, just to guarantee the exponent is arbitrarily large. If i is positive, then 2^100i is a very large number N, so the fraction becomes 2_N/(N+1), which is just a shade under 2. If i is negative, then 2^100i is a very tiny positive number n, so the fraction becomes 2_n/(n+1), or a positive number just a shade over zero. Subtract 1 from either result, and you get numbers very close to -1 and +1, depending on the sign of i. Multiply this by i to get |i|.

This obviously fails if i is fairly small (that can be corrected by increasing the 100 in the exponent), and if i is large enough the calculator may not calculate the exponential, so it’s a rather impractical solution. I offer it only to give myself closure to a problem that has been monopolizing my thoughts this afernoon 🙂

---

<div class="post-metadata">

**Author:** ![Indistinguishable](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@Indistinguishable](https://boards.straightdope.com/u/Indistinguishable)\
**Post date:** [August 29, 2007, 12:47am UTC](https://boards.straightdope.com/t/math-whiz-formula-for-absolute-value/417129/40 "2007-08-29T00:47:03Z")

</div>

Simplifying your expression a tiny bit, we have |x| approximated as [1 - 2/(B^x+1)] \* x, where B is very, very large (the level of largeness required being determined by the size of x and how accurate a result you want).

Of course, cute as that is, it depends crucially on the ability to use ^ with exponents which may be negative (not to mention non-integral), which the OP lacks.

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

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