달력

11

« 2024/11 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

'자바퍼즐러'에 해당되는 글 2

  1. 2010.12.08 [자바퍼즐러 3] 긴 나눗셈
  2. 2010.11.09 [자바퍼즐러] 홀수판별
2010. 12. 8. 11:17

[자바퍼즐러 3] 긴 나눗셈 Study/Java2010. 12. 8. 11:17

핵심 : 큰 수를 다룰 때는 오버플로우에 주의.

..생략..
final long Micro = 24 * 60 * 60 * 1000 * 1000;
final long Millis = 24 * 60 * 60 * 1000;
System.out.println(Micro/Millis);

연산은 오로지 int로만 이루어지고 끝난후에 long 타입으로 확장된다.

int - > long 타입으로의 확장은 기본 타입 확장 변환 이다. (JLS 5.1.2)

<JLS 발췌>
5.1.2 Widening Primitive Conversion
The following 19 specific conversions on primitive types are called the widening
primitive conversions:
CONVERSIONS AND PROMOTIONS Widening Primitive Conversion 5.1.2

• byte to short, int, long, float, or double
• short to int, long, float, or double
• char to int, long, float, or double
• int to long, float, or double
• long to float or double
• float to double
Widening primitive conversions do not lose information about the overall
magnitude of a numeric value. Indeed, conversions widening from an integral type
to another integral type do not lose any information at all; the numeric value is
preserved exactly. Conversions widening from float to double in strictfp
expressions also preserve the numeric value exactly; however, such conversions
that are not strictfp may lose information about the overall magnitude of the
converted value.
Conversion of an int or a long value to float, or of a long value to double,
may result in loss of precision—that is, the result may lose some of the least significant
bits of the value. In this case, the resulting floating-point value will be a
correctly rounded version of the integer value, using IEEE 754 round-to-nearest
mode (§4.2.4).
A widening conversion of a signed integer value to an integral type T simply
sign-extends the two’s-complement representation of the integer value to fill the
wider format. A widening conversion of a char to an integral type T zero-extends
the representation of the char value to fill the wider format.
Despite the fact that loss of precision may occur, widening conversions
among primitive types never result in a run-time exception (§11).
Here is an example of a widening conversion that loses precision:
class Test {
public static void main(String[] args) {
int big = 1234567890;
float approx = big;
System.out.println(big - (int)approx);
}
}
which prints:
-46
thus indicating that information was lost during the conversion from type int to
type float because values of type float are not precise to nine significant digits.


수정은 간단하다. long 타입 상수를 이용하면 된다.

final long Micro = 24L * 60 * 60 * 1000 * 1000;
final long Mills = 24L * 60 * 60 * 1000;

뒷장에서 나오지만 Long 타입 리터럴 을 사용할 경우는 소문자가 아닌 대문자 L을 이용한다.

(1과 l 은 햇갈릴 수 있기 때문..)
:
Posted by 유쾌한순례자
2010. 11. 9. 11:46

[자바퍼즐러] 홀수판별 Study/Java2010. 11. 9. 11:46

일반적으로 홀수 판별할때 2로 나누어서 나머지로 판단을 한다.

만약 판별할 수가 음수로 들어간다면?

한번도 생각해보지 않고 매번 i % 2 == 1 이렇게 했는데

음수일 경우도 생각해봐야겠다.

(즉 파라미터 각각에 음,양,0 을 넘겼을 때 제대로 작동하는지 확인)


:
Posted by 유쾌한순례자