Pages: [1]   Go Down
Author Topic: Arduino Reference - abs()  (Read 4410 times)
0 Members and 1 Guest are viewing this topic.
Tasmania - Australia
Offline Offline
Sr. Member
****
Karma: 16
Posts: 310
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Just looking at the reference section for the abs() function and it tells me that abs() does the following;
Quote
Description
Computes the absolute value of a number.
Parameters
x: the number
Returns
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;
Quote
Returns the absolute value of x
|x| = abs(x)

example:
abs(12) = 12
abs(-14) = 14

And while I'm at it, the link where it mentions the following;
Quote
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?
_____
Paul
« Last Edit: October 22, 2013, 02:14:39 am by rockwallaby » Logged


Tasmania - Australia
Offline Offline
Sr. Member
****
Karma: 16
Posts: 310
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
_____
Paul
Logged


Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 33
Posts: 1225
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello!

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!

Logged

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 33
Posts: 1225
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Duh?  abs() is welbehaved and should not have any edge conditions.
Certainly this works
Code:
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
Logged

Pages: [1]   Go Up
Jump to: