名称
abs()
説明
数値の絶対値を計算する。
書式
abs(x);
引数
戻り値
xが0以上のときx、xが0より小さいとき-xを返す。
使用例
変数xの絶対値をシリアルモニタに表示する。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
int x = 42;
Serial.print("The absolute value of ");
Serial.print(x);
Serial.print(" is ");
Serial.println(abs(x));
x = -42;
Serial.print("The absolute value of ");
Serial.print(x);
Serial.print(" is ");
Serial.println(abs(x));
}
void loop() {
}
|
注意
abs()はマクロとして実装されているため、引数に他の関数を使わないようにすること。そのような使い方をすると、意図しない結果になることがある。
1
2
3
4
5
|
abs(a++); // avoid this - yields incorrect results
// use this instead:
abs(a);
a++; // keep other math outside the function
|
参照
言語 constrain()
言語 map()
言語 max()
言語 min()
言語 pow()
言語 sq()
言語 sqrt()
オリジナルのページ
https://www.arduino.cc/reference/en/language/functions/math/abs/
Last Revision: 2022/09/07
実装の解析
まだ解析していません。
最終更新日
November 1, 2022