abs()

설명

숫자의 절대값 계산.

문법

abs(x)

매개변수

  • x: 숫자

반환값

  • x: x 가 0보다 크면.
  • -x: if x 0보다 작으면.

예제 코드

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

※ 주의 및 경고:

abs() 함수가 구현된 방법 때문에, 괄호 안에 다른 함수를 쓰는 것을 피하세요. 잘못된 결과가 나올 수 있음.

abs(a++); // 이렇게 하지 마세요 - 잘못된 결과 나옴 abs(a); // 대신 이것을 사용하세요 - a++; // 함수 밖에서 다른 수학

더보기

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