JavaScript&#s 수학 객체 익히기: 내장 수학 함수 및 속성에 대한 종합 가이드

WBOY
풀어 주다: 2024-09-08 20:34:08
원래의
885명이 탐색했습니다.

Mastering JavaScript

JavaScript 수학 객체: 개요

JavaScript Math 개체는 수학 함수 및 상수 모음을 제공하는 내장 개체입니다. 생성자가 아니므로 인스턴스를 만들 수 없습니다. 대신 정적 메서드와 속성을 통해 직접 사용됩니다.

1. 상수

Math 개체에는 수학적 계산에 유용한 여러 상수가 포함되어 있습니다.

  • Math.E: 자연 로그의 밑, 대략 2.718과 같습니다.
  • Math.LN2: 2의 자연 로그로, 대략 0.693과 같습니다.
  • Math.LN10: 10의 자연 로그로, 대략 2.303과 같습니다.
  • Math.LOG2E: E의 밑이 2인 로그로, 대략 1.442와 같습니다.
  • Math.LOG10E: E의 밑이 10인 로그로, 대략 0.434와 같습니다.
  • Math.PI: 원주와 지름의 비율은 대략 3.14159와 같습니다.
  • Math.SQRT1_2: 1/2의 제곱근, 대략 0.707과 같습니다.
  • Math.SQRT2: 2의 제곱근, 대략 1.414와 같습니다.

2. 방법

Math 개체는 수학 연산을 수행하기 위한 여러 메서드를 제공합니다.

  • Math.abs(x): x의 절대값을 반환합니다.
  Math.abs(-5); // 5
로그인 후 복사
  • Math.ceil(x): x를 가장 가까운 정수로 반올림합니다.
  Math.ceil(4.2); // 5
로그인 후 복사
  • Math.floor(x): x를 가장 가까운 정수로 내림합니다.
  Math.floor(4.7); // 4
로그인 후 복사
  • Math.round(x): x를 가장 가까운 정수로 반올림합니다.
  Math.round(4.5); // 5
로그인 후 복사
  • Math.max(...values): 0개 이상의 숫자 중 가장 큰 숫자를 반환합니다.
  Math.max(1, 5, 3); // 5
로그인 후 복사
  • Math.min(...values): 0개 이상의 숫자 중 가장 작은 숫자를 반환합니다.
  Math.min(1, 5, 3); // 1
로그인 후 복사
  • Math.random(): 0(포함)과 1(제외) 사이의 의사 난수를 반환합니다.
  Math.random(); // e.g., 0.237
로그인 후 복사
  • Math.pow(base, exComponent): 지수 거듭제곱으로 거듭제곱된 밑수를 반환합니다.
  Math.pow(2, 3); // 8
로그인 후 복사
  • Math.sqrt(x): x의 제곱근을 반환합니다.
  Math.sqrt(9); // 3
로그인 후 복사
  • Math.trunc(x): 소수점 이하 자릿수를 제거하고 x의 정수 부분을 반환합니다.
  Math.trunc(4.9); // 4
로그인 후 복사

3. 사용예

Math 개체를 사용하는 방법에 대한 몇 가지 실제 예는 다음과 같습니다.

  • 임의의 정수 생성
  function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }
  console.log(getRandomInt(1, 10)); // e.g., 7
로그인 후 복사
  • 사변 계산
  function calculateHypotenuse(a, b) {
    return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
  }
  console.log(calculateHypotenuse(3, 4)); // 5
로그인 후 복사

4. 제한사항 및 참고사항

  • 정밀도 문제: 부동 소수점 산술은 정밀도 문제로 이어질 수 있습니다. 예를 들어 Math.sqrt(2) * Math.sqrt(2)는 반올림 오류로 인해 정확히 2가 아닐 수도 있습니다.
  • 생성자가 아님: Math 객체에는 생성자 기능이 없습니다. 모든 속성과 메서드는 정적입니다.

수학 객체 메서드 및 속성


1. 수학.abs(x)

x의 절대값을 반환합니다.

console.log(Math.abs(-10)); // 10
console.log(Math.abs(5.5)); // 5.5
로그인 후 복사

2. Math.acos(x)

x의 아크코사인(역코사인)을 라디안 단위로 반환합니다.

console.log(Math.acos(1)); // 0
console.log(Math.acos(0)); // 1.5707963267948966 (π/2)
로그인 후 복사

3. Math.acosh(x)

x의 쌍곡선 아크코사인을 반환합니다.

console.log(Math.acosh(1)); // 0
console.log(Math.acosh(2)); // 1.3169578969248166
로그인 후 복사

4. Math.asin(x)

x의 아크사인(역사인)을 라디안 단위로 반환합니다.

console.log(Math.asin(0)); // 0
console.log(Math.asin(1)); // 1.5707963267948966 (π/2)
로그인 후 복사

5. Math.asinh(x)

x의 쌍곡선 아크사인을 반환합니다.

console.log(Math.asinh(0)); // 0
console.log(Math.asinh(1)); // 0.881373587019543
로그인 후 복사

6. Math.atan(x)

x의 아크탄젠트(역탄젠트)를 라디안 단위로 반환합니다.

console.log(Math.atan(1)); // 0.7853981633974483 (π/4)
console.log(Math.atan(0)); // 0
로그인 후 복사

7. Math.atan2(y, x)

인수 몫의 아크탄젠트를 라디안 단위로 반환합니다.

console.log(Math.atan2(1, 1)); // 0.7853981633974483 (π/4)
console.log(Math.atan2(-1, -1)); // -2.356194490192345 (-3π/4)
로그인 후 복사

8. Math.atanh(x)

x의 쌍곡선탄젠트를 반환합니다.

console.log(Math.atanh(0)); // 0
console.log(Math.atanh(0.5)); // 0.5493061443340549
로그인 후 복사

9. 수학.cbrt(x)

x의 세제곱근을 반환합니다.

console.log(Math.cbrt(27)); // 3
console.log(Math.cbrt(-8)); // -2
로그인 후 복사

10. Math.ceil(x)

x를 가장 가까운 정수로 반올림합니다.

console.log(Math.ceil(4.2)); // 5
console.log(Math.ceil(-4.7)); // -4
로그인 후 복사

11. Math.clz32(x)

x의 32비트 이진 표현에서 앞에 오는 0의 개수를 반환합니다.

console.log(Math.clz32(1)); // 31
console.log(Math.clz32(0x80000000)); // 0
로그인 후 복사

12. Math.cos(x)

x의 코사인을 반환합니다(여기서 x는 라디안임).

console.log(Math.cos(0)); // 1
console.log(Math.cos(Math.PI)); // -1
로그인 후 복사

13. Math.cosh(x)

Returns the hyperbolic cosine of x.

console.log(Math.cosh(0)); // 1
console.log(Math.cosh(1)); // 1.5430806348152437
로그인 후 복사

14. Math.E

Returns Euler's number, approximately 2.718.

console.log(Math.E); // 2.718281828459045
로그인 후 복사

15. Math.exp(x)

Returns the value of e raised to the power of x.

console.log(Math.exp(1)); // 2.718281828459045
console.log(Math.exp(0)); // 1
로그인 후 복사

16. Math.expm1(x)

Returns the value of e raised to the power of x, minus 1.

console.log(Math.expm1(1)); // 1.718281828459045
console.log(Math.expm1(0)); // 0
로그인 후 복사

17. Math.floor(x)

Rounds x downwards to the nearest integer.

console.log(Math.floor(4.7)); // 4
console.log(Math.floor(-4.2)); // -5
로그인 후 복사

18. Math.fround(x)

Returns the nearest (32-bit single precision) float representation of x.

console.log(Math.fround(1.337)); // 1.336914
console.log(Math.fround(1.5)); // 1.5
로그인 후 복사

19. Math.LN2

Returns the natural logarithm of 2, approximately 0.693.

console.log(Math.LN2); // 0.6931471805599453
로그인 후 복사

20. Math.LN10

Returns the natural logarithm of 10, approximately 2.302.

console.log(Math.LN10); // 2.302585092994046
로그인 후 복사

21. Math.log(x)

Returns the natural logarithm (base e) of x.

console.log(Math.log(Math.E)); // 1
console.log(Math.log(10)); // 2.302585092994046
로그인 후 복사

22. Math.log10(x)

Returns the base-10 logarithm of x.

console.log(Math.log10(10)); // 1
console.log(Math.log10(100)); // 2
로그인 후 복사

23. Math.LOG10E

Returns the base-10 logarithm of e, approximately 0.434.

console.log(Math.LOG10E); // 0.4342944819032518
로그인 후 복사

24. Math.log1p(x)

Returns the natural logarithm of 1 + x.

console.log(Math.log1p(1)); // 0.6931471805599453
console.log(Math.log1p(0)); // 0
로그인 후 복사

25. Math.log2(x)

Returns the base-2 logarithm of x.

console.log(Math.log2(2)); // 1
console.log(Math.log2(8)); // 3
로그인 후 복사

26. Math.LOG2E

Returns the base-2 logarithm of e, approximately 1.442.

console.log(Math.LOG2E); // 1.4426950408889634
로그인 후 복사

27. Math.max(...values)

Returns the largest of zero or more numbers.

console.log(Math.max(1, 5, 3)); // 5
console.log(Math.max(-1, -5, -3)); // -1
로그인 후 복사

28. Math.min(...values)

Returns the smallest of zero or more numbers.

console.log(Math.min(1, 5, 3)); // 1
console.log(Math.min(-1, -5, -3)); // -5
로그인 후 복사

29. Math.PI

Returns the value of π, approximately 3.14159.

console.log(Math.PI); // 3.141592653589793
로그인 후 복사

30. Math.pow(base, exponent)

Returns the value of base raised to the power of exponent.

console.log(Math.pow(2, 3)); // 8
console.log(Math.pow(5, 0)); // 1
로그인 후 복사

31. Math.random()

Returns a pseudo-random number between 0 (inclusive) and 1 (exclusive).

console.log(Math.random()); // e.g., 0.237
로그인 후 복사

32. Math.round(x)

Rounds x to the nearest integer.

console.log(Math.round(4.5)); // 5
console.log(Math.round(4.4)); // 4
로그인 후 복사

33. Math.sign(x)

Returns the sign of a number, indicating whether the number is positive, negative, or zero.

console.log(Math.sign(-5)); // -1
console.log(Math.sign(0)); // 0
console.log(Math.sign(5)); // 1
로그인 후 복사

34. Math.sin(x)

Returns the sine of x (where x is in radians).

console.log(Math.sin(0)); // 0
console.log(Math.sin(Math.PI / 2)); // 1
로그인 후 복사

35. Math.sinh(x)

Returns the hyperbolic sine of x.

console.log(Math.sinh(0)); // 0
console.log(Math.sinh(1)); // 1.1752011936438014
로그인 후 복사

36. Math.sqrt(x)

Returns the square root of x.

console.log(Math.sqrt(9)); // 3
console.log(Math.sqrt(16));

 // 4
로그인 후 복사

37. Math.SQRT1_2

Returns the square root of 1/2, approximately 0.707.

console.log(Math.SQRT1_2); // 0.7071067811865476
로그인 후 복사

38. Math.SQRT2

Returns the square root of 2, approximately 1.414.

console.log(Math.SQRT2); // 1.4142135623730951
로그인 후 복사

39. Math.tan(x)

Returns the tangent of x (where x is in radians).

console.log(Math.tan(0)); // 0
console.log(Math.tan(Math.PI / 4)); // 1
로그인 후 복사

40. Math.tanh(x)

Returns the hyperbolic tangent of x.

console.log(Math.tanh(0)); // 0
console.log(Math.tanh(1)); // 0.7615941559557649
로그인 후 복사

41. Math.trunc(x)

Returns the integer part of a number by removing any fractional digits.

console.log(Math.trunc(4.9)); // 4
console.log(Math.trunc(-4.9)); // -4
로그인 후 복사

위 내용은 JavaScript&#s 수학 객체 익히기: 내장 수학 함수 및 속성에 대한 종합 가이드의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:dev.to
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!