Go Down

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

#### rockwallaby

##### Oct 22, 2013, 09:08 amLast 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;
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.
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

#### rockwallaby

#1
##### Oct 22, 2013, 01:26 pm
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
_____
Paul

#### Msquare

#2
##### Oct 23, 2013, 08:57 am
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.

#### kid_nap

#3
##### Mar 18, 2014, 12:40 am
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.

#### Msquare

#4
##### Mar 18, 2014, 01:00 pm
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

#### JelleWho

#5
##### Sep 29, 2014, 08:50 pm
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"

#### PGT

#6
##### Apr 07, 2015, 01:09 pm
note that 0/0 is not allowed it doesnt go wrong on abs() its the line before it.

#### leonardo50

#7
##### Aug 14, 2016, 06:56 pm
torna0:
if (abs(A[i, K]) > abs(A[L, K])) {
L = i;
}

if (i < N) {
i = i++;    //i=i+1
goto torna0;
}

were is  my  mistache ?  Sed me that  there are  two  element  but for me  A[i,K] or A[L,K]  are  two element  to  work

thank you  Leonardo

#### AWOL

#8
##### Aug 14, 2016, 08:23 pmLast Edit: Aug 14, 2016, 08:23 pm by AWOL
@leonardo50: Check first the correct syntax for an array access.
Then check here
(Also not sure what this has to do with the website or forum)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up

Please enter a valid email to subscribe