We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Originally posted by bunsung92 March 7, 2023 📝 구성
Table of contents generated with markdown-toc
지양
반올림 모드
�float과 double은 부동소수를 사용한다. 금융계산처럼 명확해야 하는 계산에는 지양해야한다.
아래와 같은 코드가 있다.
private static void floatingCalculate() { double funds = 1.00; int itemBought = 0; for (double price = 0.10; funds >= price; price += 0.10) { funds -= price; itemBought++; } System.out.println(itemBought + "개 구입"); System.out.println("잔돈(달러) : " + funds); }
기대한 결과는 사탕 1개 -> 사탕 2개 -> 사탕 3개 -> 사탕 4개 의 구매를 통해 1 + 2 + 3 + 4의 결과로 1달러의 소비를 원했다.
하지만 실제 결과는
좀 더 계산이 명확해야한다.
private static void bigDecimalCalculate() { final BigDecimal TEN_CENTS = new BigDecimal(".10"); int itemBought = 0; BigDecimal funds = new BigDecimal(1.00); for (BigDecimal price = TEN_CENTS; funds.compareTo(price) >= 0; price = price.add(TEN_CENTS)) { funds = funds.subtract(price); itemBought++; } System.out.println(itemBought + "개 구입"); System.out.println("잔돈(달러) : " + funds); }
부동소수점의 사용과 달리 명확한 계산결과가 반환된다. 하지만 BigDecimal은 두가지 단점이 있다.
이를 대안할 수 있는 방법이 있는데...
위의 식을 다시 생각 해보면 다룰 수 있는 값의 크기가 제한할 수있다. (꼭 소수점을 사용하지 않아도 계산이 된다.) 즉 아래와 같은 코드로 작성이 가능하다.
private static void integerCalculate() { int itemBought = 0; int funds = 100; for (int price = 10; funds >= price; price += 10) { funds -= price; itemBought++; } System.out.println(itemBought + "개 구입"); System.out.println("잔돈(센트) : " + funds); }
결과는 아래와 같다.
int는 아홉 자리 십진수 long 열여덟 자리 십진수 열여덟 자리 십진수 이상은 BigDecimal 사용
2023.03.19 일
The text was updated successfully, but these errors were encountered:
Irisation23
No branches or pull requests
Discussed in https://github.com/orgs/Study-2-Effective-Java/discussions/145
Originally posted by bunsung92 March 7, 2023
📝 구성
Table of contents generated with markdown-toc
0. TL;DR 🔨
지양
하라.반올림 모드
는 정확한 연산을 돕는다.1. 금융 계산에 부동소수 타입의 사용
�float과 double은 부동소수를 사용한다.
금융계산처럼 명확해야 하는 계산에는 지양해야한다.
아래와 같은 코드가 있다.
기대한 결과는 사탕 1개 -> 사탕 2개 -> 사탕 3개 -> 사탕 4개 의 구매를 통해 1 + 2 + 3 + 4의 결과로 1달러의 소비를 원했다.
하지만 실제 결과는

좀 더 계산이 명확해야한다.
2. BigDecimal을 사용한 해법
부동소수점의 사용과 달리 명확한 계산결과가 반환된다.
하지만 BigDecimal은 두가지 단점이 있다.
이를 대안할 수 있는 방법이 있는데...
3. 정수 타입을 사용한 해법
위의 식을 다시 생각 해보면 다룰 수 있는 값의 크기가 제한할 수있다. (꼭 소수점을 사용하지 않아도 계산이 된다.)
즉 아래와 같은 코드로 작성이 가능하다.
결과는 아래와 같다.

4. 핵심 요약 📚
5. 회고 🧹
2023.03.19 일
The text was updated successfully, but these errors were encountered: