Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions chapters/math/generating-random-numbers.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ percentile = Math.floor(Math.random() * 100)
dice = Math.floor(Math.random() * 6) + 1
1 <= dice <= 6
# => true

max = 42
min = -13
range = Math.random() * (max - min) + min
-13 <= range < 42
# => true
{% endhighlight %}

## Discussion
Expand Down
37 changes: 37 additions & 0 deletions chapters/math/working-with-exponents-and-logarithms.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
layout: recipe
title: Working with Exponents and Logarithms
chapter: Math
---
## Problem

You need to do some calculations that involve exponents and logarithms.

## Solution

Use Javascript's Math object to provide common mathematical functions.

{% highlight coffeescript %}
# Math.pow(x, y) returns x^y
Math.pow(2, 4)
# => 16

# Math.exp(x) returns E^x and is shorthand for 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

{% endhighlight %}

## Discussion

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.