Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Last active August 29, 2015 14:01
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwaldron/ebe0f4d2d267370be882 to your computer and use it in GitHub Desktop.
Save rwaldron/ebe0f4d2d267370be882 to your computer and use it in GitHub Desktop.
Exponentiation Operator: **

Exponentiation Operator

Performs exponential calculation on operands. Same algorithm as Math.pow(x, y)

  • Commonly used in albegra, geometry, physics and robotics.
  • Nice to have "inline" operator

Prior Art

  • Python
    • math.pow(x, y)
    • x ** y
  • CoffeeScript
    • x ** y
  • F#
    • x ** y
  • Ruby
    • x ** y
  • Perl
    • x ** y
  • Lua, Basic, MATLAB, etc.
    • x ^ y

Usage

// x ** y

let squared = 2 ** 2;
// same as: 2 * 2

let cubed = 2 ** 3;
// same as: 2 * 2 * 2
// x **= y

let a = 2;
a **= 2;
// same as: a = a * a;



let b = 3;
b **= 3;
// same as: b = b * b * b;

ExponentiationExpression : 
  UnaryExpression
  UnaryExpression ** ExponentiationExpression
  
MultiplicativeExpression :
  ExponentiationExpression
  MultiplicativeExpression * ExponentiationExpression
  MultiplicativeExpression / ExponentiationExpression
  MultiplicativeExpression % ExponentiationExpression


AssignmentOperator : one of
  =
  *=
  /=
  %=
  +=
  -=
  <<=
  >>=
  >>>=
  &=
  ^=
  |=
  **=

Status

Implemented as option in Traceur

Reviewed By:

  • Erik Arvidsson
  • Dmitry Lomov
@liorean
Copy link

liorean commented Mar 30, 2015

Is the right recursive version because of desired operator right associativity so "a ** b ** c" -> "(a ** (b ** c))"?
Most ECMAScript grammar productions are written as left recursive, but of course, that would make "a ** b ** c" -> "((a ** b) ** c)".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment