最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

math - How to write Mathematical formula with "^" (caret) in JavaScript? - Stack Overflow

matteradmin6PV0评论

I need some help how to make this math formula in javascript. i have tried searching but couldn't really find cause i dont even know what ^ is called in English.

Thanks in advance

Math.floor(20*(1.1^(x-10)));

I need some help how to make this math formula in javascript. i have tried searching but couldn't really find cause i dont even know what ^ is called in English.

Thanks in advance

Math.floor(20*(1.1^(x-10)));
Share Improve this question edited Aug 1, 2015 at 6:42 user2864740 62.1k15 gold badges158 silver badges229 bronze badges asked Jan 28, 2013 at 20:44 LilithLilith 613 bronze badges 3
  • 3 Here's the Math object documentation. – Pointy Commented Jan 28, 2013 at 20:45
  • 5 a^b does not raise a to the b power in JavaScript. You'll want to use Math.pow. – zzzzBov Commented Jan 28, 2013 at 20:45
  • In English, people generally say A^B as A to the Bth power. In your case, 1.1 to the (x minus 10)th power. See also Exponentiation: en.wikipedia/wiki/Exponentiation – DACrosby Commented Jan 28, 2013 at 20:51
Add a ment  | 

4 Answers 4

Reset to default 3
Math.floor(20*(Math.pow(1.1, (x-10))));

^ is the bitwise XOR operator - not what you want. Use the Math.pow function for exponentiation:

Math.floor( 20 * (Math.pow(1.1, x - 10)) );

Set this up in a function so you can use x for whatever value it may be:

var eq = function(x) {
    return Math.floor( 20 * (Math.pow(1.1, x - 10)) );
};

Math.pow() is what you are looking for.

^, as used in other languages, is called the power or exponential operator, but in Javascript, it serves a different purpose, it is the bitwise XOR operator.

Math.floor(20*(Math.pow(1.1, x - 10)));
Post a comment

comment list (0)

  1. No comments so far