ow+框架_ow! 在JavaScript中使用指数和根

本文介绍了JavaScript中的指数运算,包括使用`Math.pow()`函数和ES7中的`**`运算符。同时讲解了平方根和立方根的使用,并探讨了负数和分数在指数运算中的应用。此外,还提到了JavaScript中浮点数计算的精度问题以及平方根和立方根的近似计算方法。
摘要由CSDN通过智能技术生成

ow+框架

An exponent - also known as an index or a power - multiplies a base number by itself a certain number of times. root is the opposite: a number that, when multiplied by itself a certain number of times, equals a certain defined value. Exponents and roots are used constantly in everyday life, including measurement of size and volume, finance and physics simulations.

指数(也称为索引 )将基数自身乘以一定次数。 相反:一个数字,当自身乘以一定次数时,等于某个定义的值。 指数和根在日常生活中经常使用,包括大小和体积的测量,财务和物理模拟。

In JavaScript, exponents are represented by the wonderfully named Math.pow() function (short for “power”) and the new * * operator in ES 7.

JavaScript中 ,指数由奇妙的Math.pow()函数(“ power”的缩写)和ES 7中新的* *运算符表示。

ow! (Pow!)

To raise a number to the nth power, use the Math.pow() function with the base number defined first, followed by the exponent, separated by a comma. Using the console as a scratchpad:

要将数字提高到n次幂,请使用Math.pow()函数,该函数首先定义基数 ,然后定义指数 ,并用逗号分隔。 将控制台用作暂存器:

Math.pow(3,2)
> 9

That is, 3 squared, or 3 × 3, produces 9. We can go as high as we like, of course:

也就是说,3平方或3×3产生9。当然,我们可以升到任意高:

Math.pow(5,3);
> 125

That is, 5 cubed, or 5 × 5 × 5, equals 125.

也就是说,5 立方或5×5×5等于125。

ECMAScript 7, the next version of JavaScript, has a proposed exponent operator - * * - as an easier way of integrating powers into calculations.

ECMAScript 7(JavaScript的下一个版本)具有建议的指数运算符- * * -作为将幂集成到计算中的简便方法。

3 ** 2 
> 9

Right now support for this operator is very new, limited to nightly builds of a few browsers, so it can’t yet be used in production.

目前,此操作员的支持是非常新的,仅限于每晚构建一些浏览器,因此尚不能在生产中使用。

Exponents come in handy in all kinds of situations. A simple example would be calculating the number of seconds in an hour: Math.pow(60,2).

指数在各种情况下都派上用场。 一个简单的示例将是计算一个小时中的秒数: Math.pow(60,2)

扎根 (Root Down)

Math.sqrt() and Math.cbrt() are the opposite to the Math.pow() function. The square root of a number is a value that, when multiplied by itself, produces that number:

Math.sqrt()Math.cbrt()Math.pow()函数相反 。 一个数字的平方根是一个值,当与它自身相乘时,会产生该数字:

Math.sqrt(9)
> 3

…while a cube root is a value multiplied by itself twice to produce a target number:

…虽然立方根是一个值自身乘以两次以产生目标数:

Math.cbrt(125)
> 5

Math.cbrt() is a relatively recent addition to the JavaScript specification, and therefore only supported in modern browsers: Chrome 38+, Firefox & Opera 25+, and Safari 7.1+. You’ll note that Internet Explorer and Edge are missing from that list: for that reason, you’ll probably want to use the cube root polyfill featured below in your production code for the immediate foreseeable future.

Math.cbrt()是JavaScript规范中相对较新的功能,因此仅在以下现代浏览器中受支持:Chrome 38 +,Firefox&Opera 25+和Safari 7.1+。 您会注意到该列表中缺少Internet Explorer和Edge:出于这个原因,您可能希望在可预见的将来使用生产代码中下面提供的立方根polyfill

替代现实 (Alternate Realities)

Of course, we don’t have to use whole numbers in any of the functions:

当然,我们不必在任何函数中使用整数:

Math.pow(1.25, 2);
> 1.5625

Math.cbrt(56.57)
> 3.8387991760286138

Note that it’s entirely possible to use a negative number as the base for an exponent:

请注意,完全有可能使用负数作为指数的基数:

Math.pow(-5,2)
> 25

Or as the exponent itself:

或作为指数本身:

Math.pow(10,-2)
> 0.01

A negative number multiplied by another will always yield a positive. However, it is never possible to express the square root of a negative as a number:

负数乘以另一个将始终产生正数。 但是, 永远不可能将负数的平方根表示为数字:

Math.sqrt(-9)
> NaN

The square roots of negatives pushes us into the world of imaginary numbers - very useful in the production of fractals - which I’ll get to in a future article.

负数的平方根将我们带入数世界-在产生分形时非常有用-我将在以后的文章中介绍。

You can use fractions in Math.pow() to find the square and cube roots of numbers. The square root uses an exponent of 0.5:

您可以在Math.pow()使用分数来查找数字的平方根和立方根。 平方根使用0.5的指数:

Math.pow(5, 0.5);
> 2.23606797749979

Due to the vagaries of floating-point math, you cannot assume that finding the square root of a number and multiplying that by itself will yield the same initial value:

由于浮点数学的变幻莫测,您不能假设找到数字的平方根并将其乘以本身会产生相同的初始值:

Math.pow(2.23606797749979,2)
> 5.000000000000001

You’ll normally need to do some truncation or rounding to produce a clean result.

通常,您需要进行一些截断或舍入以产生干净的结果。

The same formulation can be used to produce the cube root of a number, by using ⅓ as an exponent. Because browser support for Math.cbrt is limited at the moment, you may want to use the technique to produce a polyfill, as suggested by the Mozilla Developer Network site:

通过使用⅓,可以使用相同的公式产生数字的立方根。 作为指数。 由于目前浏览器对Math.cbrt支持是有限的,因此您可能希望使用该技术来生成polyfill,如Mozilla开发人员网络网站建议的

Math.cbrt = Math.cbrt || function(x) {
  var y = Math.pow(Math.abs(x), 1/3);
  return x < 0 ? -y : y;
};

注意 (Not e)

Beginners in JavaScript often understandably confuse Math.pow() function with Math.exp(), which is not an exponential function for numbers in general, but Euler’s number, the base of natural logarithms. I’ll be looking at that function, and the associated Math.log(), next.

可以理解,JavaScript的初学者经常将Math.pow()函数与Math.exp()混淆,后者通常不是数字的指数函数,而是自然对数的底数Euler的数字 。 我将查看该函数以及相关的Math.log()

翻译自: https://thenewcode.com/1080/Pow-Using-exponents-and-roots-in-JavaScript

ow+框架

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值