How quick is the abs() function, for int variable vs. float variable?

I'm an old FORTRAN jockey, and that ancient language had two different functions for absolute value. IABS(I) for int arguments, and ABS(X) for Float arguments (aka REAL). IABS was always quicker than ABS, for obvious reasons (floating-point number always took more manipulations, point shifting etc.).

In the Arduino's language, I see there is only one function for absolute value, abs(x). Apparently it takes either int data types and returns an int, or a float datatype and returns a float. (Is this correct?)

I have a timing-critical program I'm working on, and I use integer math whenever I can.

My question is, is the abs of an integer variable, usually quicker than the abs of a float variable?

Anybody know?

Thanx all!

Probably depends on which "Arduino" board you're talking about. But, regardless, if it's not fast enough for your liking, you can implement your own version.

Even on a float, doing an abs() should be very quick - it requires nothing more than doing a 2's complement on the mantissa.

Of course, it would only take a few seconds to write and run the few lines of code required to measure it....

The bigger question is: Why on earth do you care? Surely this is picking the fly poop out of the pepper....

Regards,
Ray L.

lilabner045:
In the Arduino's language, I see there is only one function for absolute value, abs(x). Apparently it takes either int data types and returns an int, or a float datatype and returns a float. (Is this correct?)

No, there are two* functions, but overloaded on parameter type.
The compiler decides which to call depending on supplied arguments.

It’s been an absolute age since I did any FORTRAN, but I’m willing to bet that being a language of a certain age it didn’t initially support overloading (not until FORTRAN90 according to Google).
So, in order to have only one function would have required either explicit or implicit casting (however you do that in FORTRAN??) to and back from a float. Now that would have been inefficient, and also potentially inaccurate for integer types (depending on float precision).

  • edit: Very possibly more than two. It may also be overloaded on float, double, byte, short, int, long, etc.
    e.g.
int abs(int value)
{
  return value < 0 ? -value : value;
}

float abs(float value)
{
  return value < 0 ? -value : value;
}

//etc...

In Arduino land, abs() like many other things does not do things the standard/normal way.
abs() is macro defined in Arduino.h that will override any builtin abs() processing by the compiler or any libC function.
Here is the definition from Arduino.h

#define abs(x) ((x)>0?(x):-(x))

So data type does not matter.

This type of macro can cause issues do the multiple references of the argument.

This kind of thing was common 30+ years ago, before the days of gcc and good optimization as it would generate better / faster code than calling the C library function.
The time for this kind of stuff is long past and now it often does not produce as good of code as the inlined version provided by the compiler.

--- bill

Yeah, I just grab another processor to offload stuff.

-jim lee