Skip to content

Commit dd4f0b9

Browse files
committed
add recipe on exponents and logarithms
1 parent 2d16a57 commit dd4f0b9

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
layout: recipe
3+
title: Working with Exponents and Logarithms
4+
chapter: Math
5+
---
6+
## Problem
7+
8+
You need to do some calculations that involve exponents and logarithms.
9+
10+
## Solution
11+
12+
Use Javascript's Math object to provide common mathematical functions.
13+
14+
{% highlight coffeescript %}
15+
# Math.pow(x, y) returns x^y
16+
Math.pow(2, 4)
17+
# => 16
18+
19+
# Math.exp(x) returns E^x and is shorthand for Math.pow(Math.E, x)
20+
Math.exp(2)
21+
# => 7.38905609893065
22+
23+
# Math.log returns the natural (base E) log
24+
Math.log(5)
25+
# => 1.6094379124341003
26+
Math.log(Math.exp(42))
27+
# => 42
28+
29+
# To get a log with some other base n, divide by Math.log(n)
30+
Math.log(100) / Math.log(10)
31+
# => 2
32+
33+
{% endhighlight %}
34+
35+
## Discussion
36+
37+
For more information on the Math object see the documentation on the [Mozilla Developer Netword](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math). Also refer to [Converting Radians and Degrees](/chapters/math/radians-degrees) for discussion of the various constants in the Math object.

0 commit comments

Comments
 (0)