Java Math class provides several methods to work on math calculations like min(), max(), avg(), sin(), cos(), tan(), round(), ceil(), floor(), abs() etc.
Unlike some of the StrictMath class numeric methods, all implementations of the equivalent function of Math class can't define to return the bit-for-bit same results. This relaxation permits implementation with better-performance where strict reproducibility is not required.
If the size is int or long and the results overflow the range of value, the methods addExact(), subtractExact(), multiplyExact(), and toIntExact() throw an ArithmeticException.
For other arithmetic operations like increment, decrement, divide, absolute value, and negation overflow occur only with a specific minimum or maximum value. It should be checked against the maximum and minimum value as appropriate.
public class UseMath {
public static void main(String[] args) {
System.out.println(Math.PI);
System.out.println(Math.sqrt(2)); // 제곱근
System.out.println(Math.toDegrees(Math.PI)); // 각도
System.out.println(Math.toDegrees(Math.PI * 2.0));
double radian45 = Math.toRadians(45); // 라디안
System.out.println(Math.sin(radian45));
System.out.println(Math.cos(radian45));
System.out.println(Math.tan(radian45));
System.out.println(Math.log(25));
System.out.println(Math.pow(2, 4)); // 제곱
}
}
Java Random class is used to generate a stream of pseudorandom numbers. The algorithms implemented by Random class use a protected utility method than can supply up to 32 pseudorandomly generated bits on each invocation.
public class UseRandom {
public static void main(String[] args) {
Random rand = new Random();
for(int i=0;i<10;i++)
System.out.print(rand.nextInt(100) + ", ");// 0 ~ 99
System.out.println();
//Math.random();// 0.0이상 ~ 1.0미만 실수
for(int i=0;i<10;i++)
System.out.print((int)(Math.random()*100) + ", ");// 0 ~ 99
System.out.println();
}
}
'java' 카테고리의 다른 글
Java/ 자료구조 - Array List (0) | 2020.02.28 |
---|---|
Java/ 자료구조 - Linked List (0) | 2020.02.28 |
Java/ Interface의 개념 (0) | 2020.02.10 |
Java/ MVC패턴과 추상클래스를 이용한 사원 관리 프로그램 (0) | 2020.02.10 |
Java/ MVC패턴과 추상클래스를 이용한 연락처 관리 프로그램 (0) | 2020.02.10 |