find the magnitude of A - B

Status
Not open for further replies.

drjohnsmith

Well-known member
Well

there might be a simple explanation, but I thought abs( A - B ) would give us the difference, always positive, but it seems not in arduino land.

whats the fastest way for the code to calculate the absolute difference between two uint_32 numbers ?

is there a simple trick I'm missing over and if then else with a ? type

thanks
 
Well

there might be a simple explanation, but I thought abs( A - B ) would give us the difference, always positive, but it seems not in arduino land.

whats the fastest way for the code to calculate the absolute difference between two uint_32 numbers ?

is there a simple trick I'm missing over and if then else with a ? type

thanks

OOPS

code problem.

yes abs works as expected, my mistake
 
Note, there are various flavors of abs, be sure to use the right version or else the compiler will convert the argument to the given type (i.e. if you call abs with a double argument, it will first convert the double to int and then do the absolute value operation:

  • int abs (int);
  • long absl (long);
  • long long absll (long long);
  • intmax_t imaxabs(intmax_t);
  • double fabs (double);
  • float fabsf (float);
  • long double fabsl (long double);
 
never knew about fabs , fabsl etc.
thought abs was overloaded to cover all types,

my original problem was to hasty a post , and finger trouble,
but fabs etc is interesting to know about. not in the arduino ref page.

https://www.arduino.cc/en/Reference/HomePage


mind you, the description of the abs macro on the arduino site is wrong, which confounded my mistake.

https://www.arduino.cc/en/Reference/Abs


how can abs return a negative value !!

by definition abs is the absolute difference, so is always positive.



https://en.wikipedia.org/wiki/Absolute_value
 
It does not :) the documentation is correct :)
"It returns -x when x is < 0"
It returns -(-1), which is +1 for x=-1...
But you're right, it could be explained more clearly.
 
Last edited:
The arduino site says

========
Returns

x: if x is greater than or equal to 0.

-x: if x is less than 0.
========

https://www.arduino.cc/en/Reference/Abs

which I think is wrong, should always return the magnitude of the difference , never signed.

but anyway , the function itself works, even with my incompetence,
'just' the documentation on the italian site wrong me thinks, not much paul can do about that
and I would not hold out for the italians to update their site,
 
Abs can return negative if the input given to it is the most negative number for the integer range of the particular version of abs. This is because under 2's complement arithmetic that is used by every current processor, there is a negative value with no positive counterpart. Some historic mainframes used 1's complement or signed magnitude for encoding integers, but these are relics of the past.

For example, on systems like the Teensy where ints are 32-bits, a signed integer can hold -2147483648 to +2147483647. If you call abs(-2147483648) you will get -2147483648. Yeah mathematically abs should never return negative, but in the real world, you get corner cases.

Note, abs/absl/absll/fabs/fabsl/fabsf are all defined by the C standard (which historically did not have overloaded functions). The C++ standard brings in most of the C standard, including the library. Now, most of the library is not available in embedded environments, but things that don't involve I/O, are generally available.
 
Last edited:
thanks Michael,

As for absl etc,
not in my copy of Kerninghn and Ritchie,

and page 253 defiantly says , abs( X ) returns the absolute value of X .. as expected in mathematics.

anyway, thanks again
 
Interesting, now is abs faster then subtracting a small int from a large int. Along with logic to prevent subtracting larger values from smaller values?
 
Interesting, now is abs faster then subtracting a small int from a large int. Along with logic to prevent subtracting larger values from smaller values?

I dunno, you would have to look at the code. I would imagine ABS on the ARM is something like 2 instructions (compare against 0 and then negate if the comparison was negative -- note with my previous remarks negating the most negative number still gives you the most negative number). On other machines, it may involve an actual branch instruction, or it may involve special machine instructions (such as arithmetic shift left, xor, and subtract that the PowerPC does).

On systems with hardware floating point, fabs/fabsf may be implemented as a single instruction.
 
Status
Not open for further replies.
Back
Top