abs()

Description

Calcul la valeur absolue d'un nombre.

Syntaxe

abs(x);

Paramètres

  • x: le nombre

Valeurs Renvoyées

  • x: si x est supérieur ou égal à 0.
  • -x: si x est inférieur à 0.

Exemple

void setup() { Serial.begin(9600); int x = -4; int y = abs(x); Serial.print("The absolute value of of "); Serial.print(x); Serial.print(" is "); Serial.println(y); } void loop() {}

The result in Serial Monitor:

Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
The absolute value of of -4 is 4
Ln 11, Col 1
Arduino Uno on COM15
2

※ Remarque:

En raison de la façon dont la fonction abs() est programmée, éviter d'utiliser d'autres fonctions à l'intérieur des parenthèses car cela pourrait aboutir à des résultats incorrects.

abs(a++); // éviter cela - renvoie un résultat incorrect a++; // utiliser plutôt cette forme - abs(a); // en gardant les autres fonctions mathématiques en dehors de la fonction

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables .

※ OUR MESSAGES