Bummer! This is just a preview. You need to be signed in with a Treehouse account to view the entire video.
Start a free Basic trial
to watch this video
When writing programs, you may need to do trigonometric or transcendental math. Learn how to access this functionality using the Math library.

0:00
[? upbeat music ?] [Treehouse]

0:08
In this video we'll be learning about the math library.

0:11
You can use the math library in case you ever need to perform any geometric or trigonomic functions on the numbers you're working with.

0:20
Occasionally you may find yourself needing to do higher level math

0:25
or dealing with trigonometric or transcendental functions.

0:30
Ruby has a library for this called appropriately the math library.

0:35
You can find the documentation for the math library on the ruby.doc.org website.

0:43
Let's take a brief look at how we could use the math library in our day to day programming.

0:49
You'll see in the math library that there are 2 constants that we have that we can use:

0:54
the constant E, constant PI.

0:57
Let's take a look at how we can use those in our programs.

1:00
Ruby already loads the math library for us,

1:04
so if we want to access these things we can use the name of the module,

1:09
in this case math, and if we want to get at the constants, in this case E,

1:16
we do that by typing 2 colons.

1:20
That lets us get to a constant inside of the module,

1:26
and we can see the value of E right there.

1:30
This also works for PI,

1:36
and we can see Ruby gives us back the value that it has for PI.

1:41
You can browse the math library to see what kind of functions you might need to use throughout your code.

1:47
There are functions for signs, cosigns, tangents, square roots,

1:52
a lot of things that you might need.

1:55
When you're looking at documentation you'll see that some functions accept arguments.

2:02
In the case of the log function right here

2:05
it's listed as accepting 2 arguments.

2:09
The first one can be a number and the second one can be the base.

2:14
The second argument is not specifically required.

2:18
If it's given, they'll use that as the base of the logarithm.

2:24
If not, it'll use base 10.

2:27
Let's see how that works right now.

2:33
And that returns the log of 12.

2:36
Now let's try sending in a different base.

2:40
And you can see we get a different answer back.

2:44
If you'd like to browse the math library you can do so on the ruby.doc.org website.

2:50
[? upbeat music ?]

2:52
And that was an overview of the math library available to you.

2:56
If you'd like more information you can check out the documentation in the Ruby standard library.

3:02
[? upbeat music ?] [Treehouse]
You need to sign up for Treehouse in order to download course files.
Sign up