Go Down

Topic: Arduino Reference - abs() (Read 9371 times) previous topic - next topic


Oct 22, 2013, 09:08 am Last Edit: Oct 22, 2013, 09:14 am by rockwallaby Reason: 1
Just looking at the reference section for the abs() function and it tells me that abs() does the following;
Computes the absolute value of a number.
x: the number
x: if x is greater than or equal to 0.
-x: if x is less than 0.

Well, blow me down, I'm guessing the return value just returns the original value of x, regardless if it was less than zero. I do not recall abs() would ever return a negative value.
Should this instead read something along the lines of;
Returns the absolute value of x
|x| = abs(x)

abs(12) = 12
abs(-14) = 14

And while I'm at it, the link where it mentions the following;
Corrections, suggestions, and new documentation should be posted to the Forum.
The link on the word 'Forum' is to the 'Arduino Forum :: Development :: Other Software Development', why on Earth there I ask?


Thanks Telecommando you are right, of course, and I should have seen that.
Note to self: spend time away from computer(s), breathe in the fresh air of the forest outside  :smiley-red:


Yes, I made that mistake too a long time ago. But did not post. Which - considering that I have use the ABS function in other languages since mid 1970'ies - is not that I do not know or understand what the function does, but that the explanation is needlessly "mathematical" in nature, escpecially considering that that the documentation is aimed at newcommers in this field.



i have a doubt about the case abs(0) (absolute value of zero); in this piece of code for example:

  step = ((frequency - frequencyOld) / 20);
  step = abs(step);

when both frequencies are the same, then step = 0, the code freezes.

is there any know problem in the implementation of absolute value? abs(0) = 0 as far as wikipedia says, but the reference does not explicits the case for argument = zero.

thanks in advance!


Duh?  abs() is welbehaved and should not have any edge conditions.
Certainly this works
Code: [Select]
int z = 0; float zz = 0.0 ;
void setup() {
  Serial.begin(9600) ;
  zz =abs(z) ;  Serial.println(z) ;
  zz =abs(zz) ;  Serial.println(zz) ;

So start a new thread for discovering why your code hangs if the expression ((frequency - frequencyOld) / 20) returns zero. I guess that whatever code you do something with a step-size of zero has a logical flaw


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

-x: if x is less than 0.

so if x = "-5"

then -x = "--5"

and "--5" is "+5"


note that 0/0 is not allowed it doesnt go wrong on abs() its the line before it.

Go Up