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 サイトの他の関連記事を参照してください。

ホットAIツール

Undress AI Tool
脱衣画像を無料で

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

JavaScriptの範囲は、グローバル、機能、およびブロックレベルの範囲に分割される変数のアクセシビリティ範囲を決定します。コンテキストは、この方向を決定し、関数呼び出し方式に依存します。 1.スコープには、グローバルスコープ(どこでもアクセス可能)、関数スコープ(関数内でのみ有効)、およびブロックレベルのスコープ(letとconstは{}内で有効です)が含まれます。 2。実行コンテキストには、変数オブジェクト、スコープチェーン、およびこの値が含まれます。これは、通常の関数におけるグローバルまたは未定義を指します。メソッドコールはコールオブジェクトを指し、コンストラクターは新しいオブジェクトをポイントし、call/apply/bindで明示的に指定することもできます。 3。閉鎖とは、外部スコープ変数へのアクセスと記憶の関数を指します。それらはしばしばカプセル化とキャッシュに使用されますが、引き起こす可能性があります

タイプキャストとは、JavaScriptの1つのタイプの値を別のタイプに自動的に変換する動作です。一般的なシナリオには次のものが含まれます。1。オペレーターを使用する場合、一方が文字列である場合、反対側も「5」などの文字列に変換されます。結果は「55」です。 2。ブールのコンテキストでは、非ブールの値は、空の文字列、0、ヌル、未定義などのブール型に暗黙的に変換されます。 3. Nullは数値操作に参加し、0に変換され、未定義はNANに変換されます。 4.暗黙の変換によって引き起こされる問題は、number()、string()、boolean()などの明示的な変換関数によって回避できます。これらのルールをマスターすることは役立ちます

VUE3のCompositapiは、複雑なロジックとタイプの導出により適しており、OptionsAPIはシンプルなシナリオや初心者に適しています。 1。OptionsAPIは、データやメソッドなどのオプションに従ってコードを整理し、明確な構造を持っていますが、複雑なコンポーネントは断片化されています。 2。CompusitionAPIは、セットアップを使用して関連ロジックを集中させます。これは、メンテナンスと再利用を助長します。 3。CompusitionAPIは、混合性機能を介して競合のないパラメーター化可能な論理再利用を実現します。これは、混合物よりも優れています。 4。CoputionAPIは、TypeScriptとより正確なタイプの派生をより適切にサポートしています。 5。2つのパフォーマンスとパッケージングのボリュームに大きな違いはありません。 6。

JavaScriptのWebworkersとJavathreadsの同時処理には本質的な違いがあります。 1。JavaScriptは、単一スレッドモデルを採用しています。 Webworkersは、ブラウザによって提供される独立したスレッドです。これは、UIをブロックしないがDOMを操作できない時間のかかるタスクを実行するのに適しています。 2。Javaは、複雑な同時ロジックとサーバー側の処理に適した、スレッドクラスを通じて作成された言語レベルからの実際のマルチスレッドをサポートしています。 3。ウェブワーカーは、PostMessage()を使用してメインスレッドと通信します。これは非常に安全で孤立しています。 Javaスレッドはメモリを共有できるため、同期の問題に注意する必要があります。 4。ウェブワーカーは、画像処理などのフロントエンドの並列コンピューティングにより適しています。

プロジェクトを初期化し、package.jsonを作成します。 2。シバンを使用してエントリスクリプトindex.jsを作成します。 3。Package.jsonのBin Fieldsを介してコマンドを登録します。 4. Yargsおよびその他のライブラリを使用して、コマンドラインパラメーターを解析します。 5。NPMLINKローカルテストを使用します。 6.エクスペリエンスを強化するためのヘルプ、バージョン、オプションを追加します。 7.オプションでnpmpublishを介して公開します。 8.オプションでYargsを使用して自動完成を達成します。最後に、合理的な構造とユーザーエクスペリエンスの設計を通じて実用的なCLIツールを作成し、自動化タスクを完了し、ウィジェットを配布し、完全な文で終了します。

document.createelement()を使用して、新しい要素を作成します。 2。TextContent、クラスリスト、SetAttribute、およびその他の方法を使用して要素をカスタマイズします。 3。AppendChild()またはより柔軟なappend()メソッドを使用して、DOMに要素を追加します。 4.オプションで、挿入位置を制御するために、insertbefore()、およびその他のメソッドを使用します。完全なプロセスは、→カスタマイズ→追加を作成することであり、ページコンテンツを動的に更新できます。

TypeScriptの高度な条件タイプは、TextEndsu?X:Y Syntaxを介してタイプ間の論理的判断を実装します。そのコア機能は、分散条件タイプ、推測タイプの推論、および複雑なタイプのツールの構築に反映されます。 1.条件付きタイプは、裸の型パラメーターに分散され、string [] | number []を取得するためにtoArrayなどのジョイントタイプを自動的に分割できます。 2.分布を使用してフィルタリングおよび抽出ツールを構築します。除外textendsuを除く除外:t、抽出抽出抽出extract textendsu?t:never、およびnullable filters null/undefined。 3

MicrofRontendsSolvessCallingChallengesimSimSimSimsByEnablingEndependDevelymentAndDeployment.1)chooseanintegrations trategy:usemodulefederationinwebpack5forruntimeloadingindingindrueindopendence、build-time-integrationforsimplestups、oriframes/webcomponents
