JavaScript Math cbrt()方法

下面是Math cbrt()方法的示例。

null
  • 例子:

    <script type= "text/javascript" >
    document.write( "cbrt of 64 : "
    + Math.cbrt(64) + "<br>" );
    document.write( "cbrt of 27: "
    + Math.cbrt(27) + "<br>" );
    document.write( "cbrt of 0: "
    + Math.cbrt(0) + "<br>" );
    document.write( "cbrt of -1: "
    + Math.cbrt(-1) + "<br>" );
    document.write( "cbrt of -27: "
    + Math.cbrt(-27) + "<br>" );
    document.write( "cbrt of Infinity : "
    + Math.cbrt(Infinity));
    </script>

    
    

  • 输出:
    cbrt of 16: 4
    cbrt of 27: 3
    cbrt of 0: 0
    cbrt of -1: -1
    cbrt of -27: -3
    cbrt of Infinity : Infinity

这个 数学cbrt() 方法用于查找数字的立方根。 语法:

Math.cbrt(x)

参数: 此方法接受一个参数,如上所述,如下所述:

  • x: 这个参数只是一个需要找到其立方根的数字。

返回: 它返回给定数字的立方根。

下面的示例演示了JavaScript中的cbrt()方法: 例子: 这里,8的立方根被计算为2,因为当任意3次重复时,立方根内部存在任何数字,那么只有一个数字作为立方根的值。 ∛8. = ∛2*2*2 = 2

Input: Math.cbrt(8)
Output : 2

以上方法的更多代码如下:

项目1: 错误和异常,这是一个错误的情况,因为无法找到复数的立方根,这就是为什么它的参数给出错误。

<script type= "text/javascript" >
// Cube root of complex number can
// not be calculated.
document.write( "cbrt of Complex no : "
+ Math.cbrt(1 + 2i));
</script>


输出:

Error: Invalid or unexpected token

项目2: 无法找到字符串的立方根,这就是函数的字符串参数给出NaN的原因,即,不是数字。函数的参数只能使用整数值。

<script type= "text/javascript" >
// Only number can be used as the parameter
// here string as parameter gives NaN i.e,
// not a number.
document.write( "cbrt of a string : "
+ Math.cbrt( "gfg" ));
</script>


输出:

cbrt of a string: NaN

方案3: 每当我们需要得到任何数字的立方根时,我们都需要数学的帮助。JavaScript中的cbrt()函数。

<script type= "text/javascript" >
// Here the Math.cbrt() function
// calculates cube root for
// different numbers taken as
// function's parameter.
document.write( "Output : "
+ Math.cbrt(125) + "<br>" );
document.write( "Output : "
+ Math.cbrt(23) + "<br>" );
</script>


输出:

Output : 5
Output : 2.8438669798515654

支持的浏览器:

  • 谷歌浏览器38.0
  • Internet Explorer 12.0
  • 火狐25.0
  • 歌剧25.0
  • Safari 8.0
© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享