-bottom

mmpp.lang
Class MathFP

java.lang.Object
  |
  +--mmpp.lang.MathFP

public final class MathFP
extends java.lang.Object

fixed point 연산을 하기 위한 클래스 KVM에서 floating point 연산을 지원하지 않기 때문에, 소수점 이하의 값을 갖는 값들을 계산하기 위하여 차선책으로 integer 변수를 활용한 fixed point 연산을 지원한다.

즉 integer변수(32bit)를 2부분으로 나누어서, 상위 20bit을 정수 부분, 하위 12bit을 소수 부분을 표현하는 데 사용한다. fixed point는 floating point와 달리 소수점의 위치가 이동하지 않기 때문에 가질 수 있는 값의 범위가 매우 한정되어 있다. fixed point로 표현할 수 있는 범위는 최대 524287(2^19)에서 최소 -524288(-2^19)이다. 그리고 정확도는 0.000244140625(2^-12)이다.

현재 구현되어 있는 함수는 fixed point로 변환과 역변환 관련( parseFP(), toInt(), toString()) 산술 연산 관련(add(), sub(), multiply(), divide(), abs(), round(), pow(), sqrt(), log(), exp(), max(), min()), 삼각함수 관련(sin(), cos(), tan() asin(), acos(), atan())이 있다.

기본적인 사용법은 기존의 값들을 FP로 변경한 뒤, 해당하는 연산을 수행하고, 이를 다시 기존의 형식으로 돌린다.

 예1)
 {
     int fp, result;
     fp = MathFP.parseFP("12.563");
     result = MathFP.log(fp);
     System.out.println( MathFP.toString(result) );
 }
 

예2) { int fp, result; fp = MathFP.PI/4; result = MathFP.tan(fp); System.out.println( MathFP.toString(result) ); }

Since:
1.0

Field Summary
static int E
          e에 가장 가까운 fixed point 값 자연 로그 계산 등에 사용된다.
static int MAX_VALUE
          fixed point값으로 표현할 수 있는 최대값(fixed point 형식)
static int MAX_VALUE_INT
          fixed point값으로 표현할 수 있는 최대값(integer 형식)
static int MIN_VALUE
          fixed point값으로 표현할 수 있는 최소값(fixed point 형식)
static int MIN_VALUE_INT
          fixed point값으로 표현할 수 있는 최대값(integer point 형식)
static int PI
          pi에 가장 가까운 fixed point 값 원주율으로 삼각 함수에 사용된다.
 
Method Summary
static int abs(int i)
          첫번째 인자(fixed point)의 절대값을 리턴한다.
static int acos(int r)
          주어진 값에 대한 arc cosine값을 구한다.
static int add(int i, int j)
          첫번째 인수(fixed point)와 두번째 인수(fixed point)를 더한 값을 리턴(fixed point)한다.
static int asin(int r)
          주어진 값에 대한 arc sine값을 구한다.
static int atan(int r)
          주어진 값에 대한 arc tangent값을 구한다.
static int cos(int r)
          라디안 값으로 주어진 각도에 대하여 삼각함수 cosine값을 리턴(fixed point)한다.
static int divide(int i, int j)
          첫번째 인수(fixed point)와 두번째 인수(fixed point)를 곱한 값을 리턴(fixed point)한다.
static int exp(int f)
          e를 첫번째 인수만큼 제곱하여 리턴(fixed point)한다.
static int log(int f)
          첫번째 인자(fixed point)의 자연 로그값을 리턴(fixed point)한다.
static int max(int a, int b)
          첫번째 인수(fixed point)와 두번째 인수(fixed point)를 비교하여 큰 값을 리턴(fixed point)한다.
static int min(int a, int b)
          첫번째 인수(fixed point)와 두번째 인수(fixed point)를 비교하여 작은 값을 리턴(fixed point)한다.
static int multiply(int i, int j)
          첫번째 인수(fixed point)와 두번째 인수(fixed point)를 곱한 값을 리턴(fixed point)한다.
static int parseFP(int f)
          일반 integer 값을 fixed point값으로 변경시킨다. fixed point 값의 범위가 한정되어 있기 때문에, MAX_VALUE_INTMIN_VALUE_INT 사이의 범위를 벗어나는 경우 NumberFormatException을 발생시킨다.
static int parseFP(java.lang.String s)
          String으로 표현된 변수값을 fixed point값으로 변경시킨다. fixed point 값의 범위가 한정되어 있기 때문에, MAX_VALUE_INTMIN_VALUE_INT 사이의 범위를 벗어나는 경우 NumberFormatException을 발생시킨다.
static int pow(int b, int e)
          첫번째 인자(fixed point)를 두번째 인자(fixed point)만큼 제곱하여 리턴(fixed point)한다.
static int round(int i)
          첫번째 인자(fixed point)의 가장 가까운 정수를 리턴(fixed point)한다.
static int sin(int r)
          라디안 값으로 주어진 각도에 대하여 삼각함수 sine값을 리턴(fixed point)한다.
static int sqrt(int i)
          첫번째 인자(fixed point)의 제곱근을 리턴(fixed point)한다.
static int sub(int i, int j)
          첫번째 인수(fixed point)와 두번째 인수(fixed point)를 뺀 값을 리턴(fixed point)한다.
static int tan(int r)
          라디안 값으로 주어진 각도에 대하여 삼각함수 tangent값을 리턴(fixed point)한다.
static int toInt(int i)
          fixed point 변수의 값을 int 값으로 변경한다.
static java.lang.String toString(int f)
          fixed point의 String 표현을 돌려준다.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

E

public static final int E
e에 가장 가까운 fixed point 값 자연 로그 계산 등에 사용된다.

PI

public static final int PI
pi에 가장 가까운 fixed point 값 원주율으로 삼각 함수에 사용된다.

MAX_VALUE

public static final int MAX_VALUE
fixed point값으로 표현할 수 있는 최대값(fixed point 형식)

MIN_VALUE

public static final int MIN_VALUE
fixed point값으로 표현할 수 있는 최소값(fixed point 형식)

MAX_VALUE_INT

public static final int MAX_VALUE_INT
fixed point값으로 표현할 수 있는 최대값(integer 형식)

MIN_VALUE_INT

public static final int MIN_VALUE_INT
fixed point값으로 표현할 수 있는 최대값(integer point 형식)
Method Detail

abs

public static int abs(int i)
첫번째 인자(fixed point)의 절대값을 리턴한다.
Parameters:
i - fixed point integer
Returns:
i의 절대값을 갖는 fixed point integer

round

public static int round(int i)
첫번째 인자(fixed point)의 가장 가까운 정수를 리턴(fixed point)한다.
Parameters:
i - fixed point integer
Returns:
i의 가장 가까운 정수값을 갖는 fixed point integer

pow

public static int pow(int b,
                      int e)
               throws java.lang.IllegalArgumentException,
                      java.lang.ArithmeticException
첫번째 인자(fixed point)를 두번째 인자(fixed point)만큼 제곱하여 리턴(fixed point)한다.

지수의 값(두번째 인자)으로 소수부분을 갖는 fixed point의 값도 올 수 있다. 단, 밑이 음수일 경우, 지수가 소수부분을 가지면( (-2)^(1.2) ), 부호의 정의가 애매하므로 IllegalArgumentException을 발생시킨다.

결과값이 fixed point로 표현할 수 있는 범위를 넘어나도 Arithmetic Exception을 발생시킨다.

Parameters:
b - fixed point integer
e - fixed point integer
Returns:
b^e (fixed point integer 형식)
Throws:
java.lang.IllegalArgumentException - 밑이 음수인 경우
java.lang.ArithmeticException - 결과 값이 fixed point로 표현할 수 있는 범위를 벗어날 경우

sqrt

public static int sqrt(int i)
                throws java.lang.ArithmeticException
첫번째 인자(fixed point)의 제곱근을 리턴(fixed point)한다.

인자의 값이 음수일 경우 IllegalArgumentException을 발생시키고, 오버 플로우가 발생하면 ArithmeticException을 발생시킨다.

Parameters:
i - fixed point integer
Returns:
i의 제곱근 값을 갖는 fixed point
Throws:
java.lang.IllegalArgumentException - i가 음수인 경우
java.lang.ArithmeticException - 오버플로우 발생할 경우

log

public static int log(int f)
               throws java.lang.ArithmeticException
첫번째 인자(fixed point)의 자연 로그값을 리턴(fixed point)한다.

첫번째 인자의 값이 음수일 경우 IllegalArgumentException을 발생시키고, 값의 범위에 벗어나도 ArithmeticException을 발생시킨다.

Parameters:
f - fixed point integer
Returns:
f의 자연 로그값을 갖는 fixed point
Throws:
java.lang.IllegalArgumentException - f가 음수인 경우
java.lang.ArithmeticException - 계산 결과가 fixed point범위를 벗어나는 경우

exp

public static int exp(int f)
               throws java.lang.ArithmeticException
e를 첫번째 인수만큼 제곱하여 리턴(fixed point)한다.

결과 값이 표현할 수 있는 범위를 벗어나면 ArithmeticException을 발생시킨다.

Parameters:
f - fixed point integer
Returns:
e^f (fixed point 형식)
Throws:
java.lang.ArithmeticException - 오버플로우 발생 시

max

public static int max(int a,
                      int b)
첫번째 인수(fixed point)와 두번째 인수(fixed point)를 비교하여 큰 값을 리턴(fixed point)한다.
Parameters:
a - fixed point integer
b - fixed point integer
Returns:
a,b 중에서 더 큰 값

min

public static int min(int a,
                      int b)
첫번째 인수(fixed point)와 두번째 인수(fixed point)를 비교하여 작은 값을 리턴(fixed point)한다.
Parameters:
a - fixed point integer
b - fixed point integer
Returns:
a,b 중에서 더 작은 값

add

public static int add(int i,
                      int j)
첫번째 인수(fixed point)와 두번째 인수(fixed point)를 더한 값을 리턴(fixed point)한다.
Parameters:
i - fixed point integer
j - fixed point integer
Returns:
i+j (fixed point 형식)

sub

public static int sub(int i,
                      int j)
첫번째 인수(fixed point)와 두번째 인수(fixed point)를 뺀 값을 리턴(fixed point)한다.
Parameters:
i - fixed point integer
j - fixed point integer
Returns:
i-j (fixed point 형식)

multiply

public static int multiply(int i,
                           int j)
                    throws java.lang.ArithmeticException
첫번째 인수(fixed point)와 두번째 인수(fixed point)를 곱한 값을 리턴(fixed point)한다.

fixed point로 표현할 수 있는 최대값을 넘길 경우, Arithmetic Exception을 발생시킨다.

Parameters:
i - fixed point integer
j - fixed point integer
Returns:
i*j (fixed point 형식)
Throws:
java.lang.ArithmeticException - 오버플로우 발생 시

divide

public static int divide(int i,
                         int j)
                  throws java.lang.ArithmeticException
첫번째 인수(fixed point)와 두번째 인수(fixed point)를 곱한 값을 리턴(fixed point)한다.
Parameters:
i - fixed point integer
j - fixed point integer
Returns:
i/j (fixed point 형식)
Throws:
java.lang.ArithmeticException - j가 0인 경우

parseFP

public static int parseFP(int f)
                   throws java.lang.NumberFormatException
일반 integer 값을 fixed point값으로 변경시킨다. fixed point 값의 범위가 한정되어 있기 때문에, MAX_VALUE_INTMIN_VALUE_INT 사이의 범위를 벗어나는 경우 NumberFormatException을 발생시킨다.
Parameters:
f - fixed point로 변경될 integer 값
Returns:
변경된 fixed point integer
Throws:
java.lang.NumberFormatException - 변환한 값이 MAX_VALUE_INTMIN_VALUE_INT를 벗어나는 경우

parseFP

public static int parseFP(java.lang.String s)
                   throws java.lang.NumberFormatException
String으로 표현된 변수값을 fixed point값으로 변경시킨다. fixed point 값의 범위가 한정되어 있기 때문에, MAX_VALUE_INTMIN_VALUE_INT 사이의 범위를 벗어나는 경우 NumberFormatException을 발생시킨다.

 예)
 {
     int fp = MathFP.parseFP("-1.234");
 }
 
Parameters:
s - fixed point값으로 변경될 String값
Returns:
변경된 fixed point integer
Throws:
java.lang.NumberFormatException - 변화한 값이 MAX_VALUE_INTMIN_VALUE_INT사이의 범위를 벗어나는 경우

toInt

public static int toInt(int i)
fixed point 변수의 값을 int 값으로 변경한다.

반올림을 통해서 integer값으로 변경된다.

Returns:
변경된 int 값

toString

public static java.lang.String toString(int f)
fixed point의 String 표현을 돌려준다.
Returns:
a new String object representing the specified fixed point integer.

cos

public static int cos(int r)
라디안 값으로 주어진 각도에 대하여 삼각함수 cosine값을 리턴(fixed point)한다.
Parameters:
r - radian 각도를 표현하는 fixed point integer
Returns:
r에 대한 cos값

sin

public static int sin(int r)
라디안 값으로 주어진 각도에 대하여 삼각함수 sine값을 리턴(fixed point)한다.
Parameters:
r - radian 각도를 표현하는 fixed point integer
Returns:
r에 대한 sin값

tan

public static int tan(int r)
라디안 값으로 주어진 각도에 대하여 삼각함수 tangent값을 리턴(fixed point)한다.
Parameters:
r - radian 각도를 표현하는 fixed point integer
Returns:
r에 대한 tan값

acos

public static int acos(int r)
주어진 값에 대한 arc cosine값을 구한다.

-1과 1사이를 벗어나는 잘못된 입력에 대해서는 IllegalArgumentException을 발생시킨다.

Parameters:
r - cosine의 값을 나타낼 fixed point 값
Returns:
r을 cosine 값으로 갖는 라디안 각도(fixed point)
Throws:
java.lang.IllegalArgumentException - r이 -1과 1사이를 벗어나는 경우

asin

public static int asin(int r)
주어진 값에 대한 arc sine값을 구한다.

-1과 1사이를 벗어나는 잘못된 입력에 대해서는 IllegalArgumentException을 발생시킨다.

Parameters:
r - sine의 값을 나타낼 fixed point 값
Returns:
r을 sine 값으로 갖는 라디안 각도(fixed point)
Throws:
java.lang.IllegalArgumentException - r이 -1과 1사이를 벗어나는 경우

atan

public static int atan(int r)
주어진 값에 대한 arc tangent값을 구한다.
Parameters:
r - tangent의 값을 나타낼 fixed point 값
Returns:
r을 tangent 값으로 갖는 라디안 각도(fixed point)