ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/JAVA

[JAVA] Math ํด๋ž˜์Šค

NaNaRin๐Ÿ™ƒ 2021. 1. 17. 11:13

docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/Math.html

 

Math (Java SE 15 & JDK 15)

public final class Math extends Object The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions. Unlike some of the numeric methods of class StrictMath,

docs.oracle.com

java.lang์— ์†ํ•ด์žˆ๋Š” ํด๋ž˜์Šค.

 

์ˆ˜ํ•™์—์„œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ์ ˆ๋Œ€๊ฐ’, ๋ฐ˜์˜ฌ๋ฆผ ๋“ฑ์„ ๋ฉ”์†Œ๋“œ๋กœ ์ œ๊ณตํ•œ๋‹ค.


 

Math : 

 

  • Math.E : public static final double E / ์ž์—ฐ๋กœ๊ทธ e 
  • Math.PI : public static final double PI / ์›์ฃผ์œจ ํŒŒ์ด π

 

  • static double random() : 0 ์ด์ƒ 1 ๋ฏธ๋งŒ์˜ double๊ฐ’ ๋žœ๋ค ๋ฐ˜ํ™˜
  • static int / long / double / float abs(int / long / double / float a) : ์ ˆ๋Œ€๊ฐ’ ๋ฐ˜ํ™˜
  • static double round(double a) : ๋ฐ˜์˜ฌ๋ฆผ
  • static long round(float a) : ๋ฐ˜์˜ฌ๋ฆผ
  • static double floor(double a) : ๋‚ด๋ฆผ
  • static double ceil(double a) : ์˜ฌ๋ฆผ
  • static int / long / double / float max(int / long / double / float a, int / long / double / float b) : ์ตœ๋Œ€๊ฐ’ ๋ฐ˜ํ™˜
  • static int / long / double / float min(int / long / double / float a, int / long / double / float b) : ์ตœ์†Œ๊ฐ’ ๋ฐ˜ํ™˜
  • static double pow(double a, double b) : a์˜ b์ œ๊ณฑ
  • static double sqrt(double a) : a์˜ ์ œ๊ณฑ๊ทผ, ๋ฃจํŠธ a

 

 

Math.random()

Math.abs(a)

Math.round(a)

Math.floor(a)

Math.ceil(a)

Math.max(a, b)

Math.min(ab)

Math.pow(ab)

Math.sqrt(a)

 


์˜ˆ์ œ

public class MathEx {

	public static void main(String[] args) {
		
		System.out.println(Math.E);
		System.out.println(Math.PI);
		System.out.println();
		
		int a = 2;
		int b = 4;
		double c = 2.31;
		double d = 3.78;
		
		System.out.println(Math.random());
		System.out.println();
		
		System.out.println(Math.abs(c) + " / " + Math.abs(d));
		System.out.println();
		
		System.out.println(Math.round(c) + " / " + Math.round(d));
		System.out.println(Math.floor(c) + " / " + Math.floor(d));
		System.out.println(Math.ceil(c) + " / " + Math.ceil(d));
		System.out.println();
		
		System.out.println(Math.max(a, b) + " / " + Math.min(a, b));
		System.out.println();
		
		System.out.println(Math.pow(a, b));
		System.out.println(Math.sqrt(b));

	}
}

 

์ถœ๋ ฅ

2.718281828459045
3.141592653589793

0.936752083337916

2.31 / 3.78

2 / 4
2.0 / 3.0
3.0 / 4.0

4 / 2

16.0
2.0

 

 

์‚ฌ์šฉ ์˜ˆ : nanarin.tistory.com/67