Absolute difference

AllanBertelsen

Senior Member
Is there an easy way to calculate an absolute difference?
For example:
B1 = 2
B2 = 1
B3 = B1-B2 'This equals 1

B1 = 1
B2 = 2
B3 = B1-B2 'I want this to equal 1 to
 

BeanieBots

Moderator
I'm not aware any equivalent to ABS(b0).
The way I do it is to compare first.

psudo code:
If b1 < b2 then
b3=b2-b1
else
b3=b1-b2
end if

There might be a more efficient way of doing it by testing the MSB and doing something with that but I don't know for sure.


Edited by - beaniebots on 11/08/2006 17:31:44
 

AllanBertelsen

Senior Member
Yes - something like:
If B1>B2 then B1Highest
B3=B2-B1
Goto B2Highest
B1Highes:
B3=B1-B2
B2Highest:

It is not that pretty. I was hoping that some of the logical operators or max or min operator could do some magic. I just can't see how. But then – there could be some fancy way.
 

AllanBertelsen

Senior Member
It is only for a limited range of values I need this function. If it was only 0 and 1 it would be XOR.
<code><pre><font size=2 face='Courier'>
A B f(A,B)
0 0 0
0 1 1
0 2 2
1 0 1
1 1 0
1 2 1
2 0 2
2 1 1
2 2 0
</font></pre></code>
I have to make a handful of these calculations. If I can do it without all the ifs and gotos it would be very nice.
 

eclectic

Moderator
Allan.

Might this old post help?
http://www.rev-ed.co.uk/picaxe/forum/Topic.asp?topic_id=3103&amp;forum_id=18&amp;Topic_Title=absolute%2Bvalue%2Bof%2Ba%2Bsubtraction&amp;forum_title=No+new+posts+please%21+12

<A href='http://www.rev-ed.co.uk/picaxe/forum/Topic.asp?topic_id=3103&amp;forum_id=18&amp;Topic_Title=absolute%2Bvalue%2Bof%2Ba%2Bsubtraction&amp;forum_title=No+new+posts+please%21+12' Target=_Blank>External Web Link</a>

I wish I could do that!
e.
 

AllanBertelsen

Senior Member
Thank you eclectic. That is very nice.
bgrabowski I did a forum search. Lot of hits but when I opened them the words was not to be found!?
 

bgrabowski

Senior Member
You need to check the &quot;search for all the words together&quot; box. This narrows down the search significantly.
 
Top