JavaScript/Math/cosh
表示
< JavaScript | Math
Math.cosh(x)
は、引数 x
の双曲線余弦(ハイパボリックコサイン)を返します。これは、x
の値に対応する双曲線余弦を返します[1]。
- 引数
x
がNaN
の場合、NaN
を返します。 - 引数
x
が+0
または-0
の場合、1
を返します。 - 引数
x
がInfinity
または-Infinity
の場合、Infinity
を返します。
例
[編集]双曲線余弦を計算するプログラム
[編集]以下のプログラムは、ユーザーが入力した値の双曲線余弦を計算します。
constf=p=>{for(;;){a=prompt(`${p}は何ですか?`);if(!isNaN(a))returna;alert(`${p}に、入力ミスがあります。 "${a}"`);}}for(;;){constx=f("値");constcosh=Math.cosh(x);if(!isNaN(cosh)){alert(`${x} の双曲線余弦は ${cosh.toFixed(3)} です。`);break;}alert("入力が大きすぎます。");}
このプログラムでは、Math.cosh
を使用して値の双曲線余弦を計算しています。ユーザーが入力した値が NaN
や Infinity
の場合、適切に処理されます。
双曲線余弦関数のグラフを描画するプログラム
[編集]以下のプログラムは、双曲線余弦関数のグラフを描画します。
constcanvas=document.createElement('canvas');document.body.appendChild(canvas);constctx=canvas.getContext('2d');canvas.width=800;canvas.height=400;constxScale=canvas.width/10;constyScale=canvas.height/100;ctx.beginPath();ctx.moveTo(0,canvas.height/2);for(letx=-5;x<=5;x+=0.1){consty=Math.cosh(x);constcanvasX=(x+5)*xScale;constcanvasY=canvas.height/2-y*yScale;ctx.lineTo(canvasX,canvasY);}ctx.strokeStyle='blue';ctx.lineWidth=2;ctx.stroke();
このプログラムでは、Math.cosh
を使用して双曲線余弦関数のグラフを描画しています。xScale
と yScale
は、グラフのスケーリングを調整するための変数です。
注意点
[編集]- 戻り値の範囲:
Math.cosh
の戻り値は、1
からInfinity
の範囲です。 - 精度: 浮動小数点演算の特性上、
Math.cosh
の結果には微小な誤差が含まれることがあります。
脚註
[編集]- ^これは、数学的には として定義されます。