【问题标题】:How do I use the '^' in this function in JavaScript? [duplicate]如何在 JavaScript 中使用此函数中的“^”? [复制]
【发布时间】:2017-10-17 02:12:41
【问题描述】:

编写一个名为“computeCompoundInterest”的函数。

给定本金、利率、复利频率和 时间(年),“computeCompoundInterest”返回金额 产生复利。

var output = computeCompoundInterest(1500, .043, 4, 6);
console.log(output); // --> 438.8368221341061

参考: https://en.wikipedia.org/wiki/Compound_interest#Calculation_of_compound_interest 这显示了用于计算产生的总复利的公式。

问题是我正在尝试使用此公式但无法正确使用

function computeCompoundInterest(p, i, compoundingFrequency, timeInYears) {

  p = p * (1 + (i/4)))^(compoundingFrequency*timeInYears)

}

我尝试逐步完成每个计算,看起来一旦我到达:

p = 1500 * (1 + 0.043/4)^ 4 x 6 //compoundingFrequency = 4 and timeInYears = 6

我做错了什么。当你(1 + (i/4)))^(compoundingFrequency*timeInYears) 时,这个website 似乎得到一个十进制数

【问题讨论】:

  • 你试过 Math.pow(x, y) 吗?
  • ^ 是按位异或不是幂。
  • ^bitwise XOR - 不是 pow。你可能想要p = 1500 * Math.pow((1 + 0.043/4), 4) * 6

标签: javascript


【解决方案1】:

^ 运算符是XOR operator

对于指数,您应该使用函数Math.pow(如Math.pow(2,3) === 8)或** operator(如2**3 === 8

【讨论】:

    猜你喜欢
    • 2012-12-18
    • 2012-09-03
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多