NaN ๊ณผ Infinity ๋ฐ์ ๊ฒฝ์ฐ
- ๋๋์ ๋๋ ๋๋จธ์ง ์ฐ์ฐ ์ ์ข์ธก ํผ์ฐ์ฐ์๊ฐ ์ ์์ด๊ณ ์ฐ์ธก ํผ์ฐ์ฐ์๊ฐ 0์ผ ๊ฒฝ์ฐ ์์ธ(ArithmeticException)๊ฐ ๋ฐ์ํ๋ค. ๋ฌดํ๋์ ๊ฐ์ ์ ์๋ก ํํํ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค. (ex int result = 5 / 0)
- ์ข์ธก ํผ์ฐ์ฐ์๊ฐ ์ค์ ์ด๊ฑฐ๋ ์ฐ์ธก ํผ์ฐ์ฐ์๊ฐ 0.0๋๋ 0,0f์ด๋ฉด ์์ธ๊ฐ ๋ฐ์ํ์ง ์๊ณ ์ฐ์ฐ์ ๊ฒฐ๊ณผ๋ Infinity ๋๋ NaN(Not a Number)์ด ๋๋ค. ์ด ๊ฒฝ์ฐ ์ด๋ค ์ฐ์ฐ์ ํ๋๋ผ๋ ๊ฒฐ๊ณผ๋ ๊ณ์ ๊ฐ์ ๊ฒ์ด๊ธฐ๋๋ฌธ์ ๋ฐ์ดํฐ๊ฐ ์๋ง์ด ๋ ์์์ผ๋ฏ๋ก ๊ณ์ํด์ ์ฐ์ฐ์ ์ํํด์๋ ์๋๋ค. (ex Infinity + 2 → Infinity, NaN + 2 → NaN)
๋ฏธ๋ฆฌ ๋ฐฉ์งํ๋ ๋ฐฉ๋ฒ
- Double.isInfinite() ์ Double.inNaN() ์ฌ์ฉ
public class InfinityAndNaNCheckExample {
public static void main(String[] args) {
int x = 5;
double y = 0.0;
double z = x / y;
//double z = x % y;
//์๋ชป๋ ์ฝ๋
System.out.println(z + 2);
//์๋ง์ ์ฝ๋
if(Double.isInfinite(z) || Double.isNaN(z)) {
System.out.println("๊ฐ ์ฐ์ถ ๋ถ๊ฐ");
} else {
System.out.println(z + 2);
}
}
}
์ถ์ฒ:์ด๊ฒ์ด ์๋ฐ๋ค(๊ต์ก ํ์ฅ์์ ๊ฐ์ฅ ๋ง์ด ์ฐ์ด๋ JAVA ํ๋ก๊ทธ๋๋ฐ์ ๊ธฐ๋ณธ์)
https://search.shopping.naver.com/book/catalog/34238594620?cat_id=50010920&frm=PBOKMOD&query=%EC%9D%B4%EA%B2%83%EC%9D%B4%EC%9E%90%EB%B0%94%EB%8B%A4&NaPm=ct%3Dlocm18y8%7Cci%3D0e64b25f86693278b2d96906cbe4b981fe74c6d5%7Ctr%3Dboknx%7Csn%3D95694%7Chk%3D95e00da716b01b77d749d200eae34c788eb63950
'๐ Today I Learned(๊ฐ๋ฐ์ธ์ดํ์ต) > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA] ๋ฐ์ดํฐ ํ์ ๋ถ๋ฅ (0) | 2023.11.04 |
---|---|
[JAVA] Switch Expressions (Java 12์ดํ) (0) | 2023.11.03 |
[JAVA] ๋ฌธ์์ด ๋ฐ๋ณต ( String.repeat()ํ์ฉ ) (0) | 2023.10.31 |
[JAVA] ๋ฌธ์ ๋ฆฌ์คํธ๋ฅผ ๋ฌธ์์ด๋ก ๋ณํํ๊ธฐ (0) | 2023.10.30 |
[JAVA] ๋ฌธ์์ด ์๋ฅด๊ธฐ(substring() ํ์ฉ) (0) | 2023.10.27 |