Skip to content

Commit d12b9a2

Browse files
committed
Merge pull request coffeescript-cookbook#97 from jlburkhead/master
Added page on exponents and logarithms
2 parents 3e897aa + dd4f0b9 commit d12b9a2

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

chapters/math/generating-random-numbers.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ percentile = Math.floor(Math.random() * 100)
2424
dice = Math.floor(Math.random() * 6) + 1
2525
1 <= dice <= 6
2626
# => true
27+
28+
max = 42
29+
min = -13
30+
range = Math.random() * (max - min) + min
31+
-13 <= range < 42
32+
# => true
2733
{% endhighlight %}
2834

2935
## Discussion
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)