* 양의 정수를 이진법으로 표현한 결과 - 00000101
* 1의 보수 (0은 1로, 1은 0으로 바꾸는 과정) 만들기 - 11111010
* 1을 더한다 - 11111011
* 5+(-5)=0
* 음의 정수와 절댓값이 같은 양의 정수를 더하면 0이 된다
public class VariableDec1 {
public static void main(String[] args) {
double num1, num2;
double result;
num1 = 1.0000001;
num2 = 2.0000001;
result = num1 + num2;
System.out.println(result);
}
}
*num4와 num5에서 실수의 범위 내의 가장 근사한 값으로
표현 되기 때문에 출력값에 오류가 생김
고정소수점 : 소수점이 고정
예) 1.0123, 0.0004586 ...
부동소수점 : 소수점이 떠있다는 뜻
예) 3.4e3 -> 3.4 x10^3 (부동소수점) = 3400.0
3.4xe-3 -> 3.4x10^-3 (부동소수점) = 0.0034
public class OperatePromotion {
public static void main(String[] args) {
short num1 = 11;
short num2 = 22;
short result = num1 + num2;
System.out.println(result);
}
}
출력값
오류의 원인 : cpu에서 메모리로 (정수의) 결과값을 보낼 때 32bit인 JVM과 2byte(16bit)인 short 자료형 사이에 오류가 생긴다.
해결방법 : int 이상의 자료형으로 코드를 짜야한다.
1.
public class OperatePromotion {
public static void main(String[] args) {
short num1 = 11;
short num2 = 22;
int result = num1 + num2;
System.out.println(result);
}
}
*result를 int형으로 바꾸기
2.
public class OperatePromotion {
public static void main(String[] args) {
int num1 = 11;
int num2 = 22;
int result = num1 + num2;
System.out.println(result);
}
}
*전체를 int형으로 바꾸기