JavaScript の数学オブジェクトをマスターする: 組み込みの数学関数とプロパティの包括的なガイド

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, exponent): 底の指数乗を返します。
  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 オブジェクトにはコンストラクター機能がありません。すべてのプロパティとメソッドは静的です。

Math オブジェクトのメソッドとプロパティ


1. Math.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. Math.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 ビット バイナリ表現の先頭のゼロの数を返します。

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 の数学オブジェクトをマスターする: 組み込みの数学関数とプロパティの包括的なガイドの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:dev.to
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!