Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

CoffeeScript Exponential arithmetic


May 09, 2021 CoffeeScript


Table of contents


Exponential dication

Problem

You need to perform operations that contain exponents and numbers.

Solution

Use JavaScript's Math object to provide commonly used mathematical functions.

# Math.pow(x, y) 返回 x^y
Math.pow(2, 4)
# => 16

# Math.exp(x) 返回 E^x ,被简写为 Math.pow(Math.E, x)
Math.exp(2)
# => 7.38905609893065

# Math.log returns the natural (base E) log
Math.log(5)
# => 1.6094379124341003
Math.log(Math.exp(42))
# => 42

# To get a log with some other base n, divide by Math.log(n)
Math.log(100) / Math.log(10)
# => 2

Discuss

To learn more about mathematical objects, see the documentation on the Mozilla Developer Network. You can also refer to the Mathematical Constants discussion of the various constants in mathematical objects.