Coffeescript-math-sqrt
提供:Dev Guides
CoffeeScript Math-sqrt()
説明
- sqrt()*メソッドは数値を受け入れ、その平方根値を返します。 数値の値が負の場合、sqrtはNaNを返します。
構文
以下に、JavaScriptの* sqrt()*メソッドの構文を示します。 CoffeeScriptコードで同じメソッドを使用できます。
Math.sqrt ( x )
例
次の例は、CoffeeScriptでの* sqrt()メソッドの使用法を示しています。 このコードを *math_sqrt.coffee という名前のファイルに保存します。
value = Math.sqrt 0.5
console.log "The square root of 0.5 is : " + value
value = Math.sqrt 81
console.log "The square root of 81 is : " + value
value = Math.sqrt 13
console.log "The square root of 13 is : " + value
value = Math.sqrt -4
console.log "The square root of -4 is : " + value
- コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c math_sqrt.coffee
コンパイル時に、次のJavaScriptが提供されます。
//Generated by CoffeeScript 1.10.0
(function() {
var value;
value = Math.sqrt(0.5);
console.log("The square root of 0.5 is : " + value);
value = Math.sqrt(81);
console.log("The square root of 81 is : " + value);
value = Math.sqrt(13);
console.log("The square root of 13 is : " + value);
value = Math.sqrt(-4);
console.log("The square root of -4 is : " + value);
}).call(this);
次に、*コマンドプロンプト*を再度開き、以下に示すようにCoffeeScriptファイルを実行します。
c:\> coffee math_sqrt.coffee
CoffeeScriptファイルを実行すると、次の出力が生成されます。
The square root of 0.5 is : 0.7071067811865476
The square root of 81 is : 9
The square root of 13 is : 3.605551275463989
The square root of -4 is : NaN