WEBVTT computer-science/introduction-to-ruby/mui
00:00:00.000 --> 00:00:02.000
Welcome back to Educator.com.
00:00:02.000 --> 00:00:09.000
Today's lesson is on math operations, part 1.
00:00:09.000 --> 00:00:23.000
First, let's go over a review of some of the math objects--first, the Numeric class, and then some of the math modules.
00:00:23.000 --> 00:00:34.000
First, you have your Numeric class: this is the base class for all of your numbers.
00:00:34.000 --> 00:00:57.000
There are quite a bit of numbers: first, you have your Integer class--this is the basis for Fixnum and your Bignum class: it's the basic Integer values for that.
00:00:57.000 --> 00:01:05.000
Then, you have your Float: Float is a class for real or floating-point numbers.
00:01:05.000 --> 00:01:11.000
You then have your Fixnum, which is the most common one--which is your Integers.
00:01:11.000 --> 00:01:30.000
There is a max bound it holds: it holds integer values represented by your native machine word -1; this is based on your local environment that you are coding in.
00:01:30.000 --> 00:01:38.000
This is the bound--max bound.
00:01:38.000 --> 00:01:47.000
If it flows outside of that bound, it's going to be automatically upcased to a Bignum.
00:01:47.000 --> 00:01:58.000
You have your Fixnum, your Bignum...and Bignum is a class of integers outside the range of Fixnum.
00:01:58.000 --> 00:02:08.000
Then, we have our class called Rational, which is a paired Integer number.
00:02:08.000 --> 00:02:17.000
The last thing we want to do is--we have this Math module; the Math module holds math-related functions.
00:02:17.000 --> 00:02:33.000
Mostly, it holds more basic ones that are for trigonometry and transcendental functions.
00:02:33.000 --> 00:02:38.000
First, let's go over some of the basic operations.
00:02:38.000 --> 00:02:49.000
You have your normal add, subtract, multiply, divide; you have the modulo, which gets the remainder of the division.
00:02:49.000 --> 00:03:05.000
That is with the percentage; your caret gets you the exponent; so this is equivalent to a to the power of b: it performs exponential calculations.
00:03:05.000 --> 00:03:11.000
We can go ahead and go through some basic ones in the IRB.
00:03:11.000 --> 00:03:30.000
I can show you the addition 10+2; it's 12; 2+10...it got 12, too; 10-5...2-7...does it not like my comma?
00:03:30.000 --> 00:03:51.000
10-5; 2-7...it allows negative numbers or positive, no issue...10 times 10, 100...5 times -10, 50...I can still do division--20 divided by 4, 100, 10, 12, 10...
00:03:51.000 --> 00:04:02.000
Notice, for 12 divided by 10, it is not a floating number; it's just a 1: there is a remainder, and it cuts that off.
00:04:02.000 --> 00:04:12.000
Let's go ahead and look at the modulo: I can do 12 modulo(10), and then you see, there I get my remainder, 2.
00:04:12.000 --> 00:04:24.000
If I do 15 modulo(5), since there is nothing, it's going to return 0...so 15modulo (5) returns 0; there is no remainder.
00:04:24.000 --> 00:04:44.000
If I want to do 2 to the power of 3, I just do 2^3, and the value is 8; and I can do 3^3, and the output is 27.
00:04:44.000 --> 00:04:52.000
That is basic operations.
00:04:52.000 --> 00:05:02.000
Next, not only do you have these operators you can use; you can still use the methods--like the divide.
00:05:02.000 --> 00:05:10.000
We have this method called div: it takes an argument--you pass in a numeric value to divide by.
00:05:10.000 --> 00:05:17.000
It will perform division, and then it is going to convert the result to an integer.
00:05:17.000 --> 00:05:31.000
This is defined by the subclass of Numeric, so in this case, since we are used to doing Integer values, it's going to be defined in the Fixnum class or the Bignum class, depending on where it's being set; this would be the Integer class.
00:05:31.000 --> 00:05:40.000
So out of the 24, I called the .div(2), and I'm going to return the result 12.
00:05:40.000 --> 00:05:46.000
13.div(4)--it's going to return 3.
00:05:46.000 --> 00:05:53.000
We do have that remainder, 1, and notice it doesn't worry about that.
00:05:53.000 --> 00:05:58.000
This method says, "I'm just dividing and returning you what I'm able to divide by."
00:05:58.000 --> 00:06:13.000
25.0--this is a Float--so notice, for these first two ones, we had Integers; for this one, we're doing a Float.
00:06:13.000 --> 00:06:27.000
It's going to be 25.0 divided by 2.0, and it still returns an integer--it returns 12 still.
00:06:27.000 --> 00:06:31.000
Next, we're looking at this method called divmod; this one gets us more specific to what we want.
00:06:31.000 --> 00:06:35.000
Not only do we want what it divides by--we want the remainder, too.
00:06:35.000 --> 00:06:43.000
This will return an Array that contains the quotient and the modulus.
00:06:43.000 --> 00:06:53.000
Again, we go ahead with our 13 divided by 4, but this time, we call divmod; it's going to return an Array.
00:06:53.000 --> 00:07:05.000
Here is our value 3, which is our quotient, and then we have our remainder, our modulus, which is 1.
00:07:05.000 --> 00:07:20.000
15.divmod(5)--we have 3 and 0; 17.5.divmod(5)--we get 3 and 2.5.
00:07:20.000 --> 00:07:24.000
Next, we are going to look at the method modulo.
00:07:24.000 --> 00:07:33.000
This returns the modulus obtained by dividing the num by the Numeric.
00:07:33.000 --> 00:07:41.000
This is where...we've talked about the quotient, we've talked about both, and now this will just return the modulus remainder.
00:07:41.000 --> 00:07:56.000
13.modulo(5)--this allows us to divide by 2, and we have this modulus of 3, so that is the end result of 13.modulo(5).
00:07:56.000 --> 00:08:15.000
Notice, if we do a Float--I do 11.0, a Float, and I do a modulo 5.0, which is another Float, it's going to return 1.0.
00:08:15.000 --> 00:08:23.000
Let's look at some more methods that you might possibly use.
00:08:23.000 --> 00:08:30.000
The next one: we have this method called quo, and again, it takes a Numeric value.
00:08:30.000 --> 00:08:37.000
This returns a Rational or a Float, and it performs division.
00:08:37.000 --> 00:08:47.000
This one--you can get a much more specific value, since we're getting Float types or Rational types with it.
00:08:47.000 --> 00:08:54.000
If I do 10.quo(5.1), we get this huge number--very specific.
00:08:54.000 --> 00:09:10.000
If I do 10.quo(2), it's going to return me a Rational value, which is 5 over 1, and then here we have our Float.
00:09:10.000 --> 00:09:23.000
It will return whatever it finds best to get you the value you need it to be--Rational is more specific, but if it needs to be, it will return you that Float.
00:09:23.000 --> 00:09:31.000
The next method we will look at is remainder: very similar to the modulus, but it does have a different way to calculate the remainder.
00:09:31.000 --> 00:09:52.000
It returns a remainder, and how it does it is...let's say x is your self, that's the number--you call your remainder...Numeric value...to do that, it actually does a calculation using truncate.
00:09:52.000 --> 00:09:57.000
It does the same thing as this here.
00:09:57.000 --> 00:10:09.000
What it's doing is x-y times x/y; and it's doing the .truncate.
00:10:09.000 --> 00:10:33.000
If you call this method--you know now the innerlyings--this 10.remainder(2), it will return 0; 10.remainder(3.3), it will return 0.1 and a bunch of zeros and 53.
00:10:33.000 --> 00:11:00.000
Next, let's look at some of the operator-operation precedence; you've been looking at a lot of different code piece examples, but if you have this huge line that is all this code of math, there is a precedence it follows--some steps, some operators, versus equality pointing to references--they all have a scale of how the compiler is using it.
00:11:00.000 --> 00:11:09.000
It looks at this line of code, and it says, "This piece has precedence over this piece in this line, so I'm going to do this action first, before I do any of the other pieces."
00:11:09.000 --> 00:11:17.000
It is very important that you look at the operator precedence; you can also control it using parentheses to scope out which one you want to give higher priority.
00:11:17.000 --> 00:11:28.000
But, it's always good to look at the precedence so you know what the compiler is doing and how it thinks to break down your code.
00:11:28.000 --> 00:11:33.000
We are going to go through three slides of it, from the highest precedence to the lowest.
00:11:33.000 --> 00:11:47.000
The first one that has precedence is just element reference; notice that you can get the element reference using just square brackets, equal.
00:11:47.000 --> 00:11:58.000
The next one that, surprisingly, has second precedence is the exponential--the raised to a power with the ^.
00:11:58.000 --> 00:12:07.000
Third is the unary plus and minus--you have your +, -, your !, and your ~.
00:12:07.000 --> 00:12:24.000
Then, the operations--math operations are next: your multiply, divide, modulo...and right below that is addition and subtraction, so your plus and minus...
00:12:24.000 --> 00:12:43.000
After those, your next precedence is the right and left bitwise shift, with the > > < <, and then you have your bitwise operations: bitwise and, bitwise or, and regular or.
00:12:43.000 --> 00:12:51.000
After that, your next precedence is the comparison operators: you have your less than/equals, less than, greater, and greater than/equals signs.
00:12:51.000 --> 00:13:14.000
Then equality and pattern match operators come next: notice our Enumerable sort method is here; we have our equality, equality by types, our not equal, approximately like for regular expressions...
00:13:14.000 --> 00:13:17.000
We have gotten through our first slide.
00:13:17.000 --> 00:13:24.000
Next, logical 'and' and 'or' come next on our list of precedence.
00:13:24.000 --> 00:13:43.000
Then it's your range, your inclusive and exclusive, two dots and three dots, and then your ternary if...then...else: we haven't actually done any code with this, but it's kind of a shortcut that allows you to do if...else statements all on one line.
00:13:43.000 --> 00:14:04.000
It would be like if I have a condition; I can do this and say, "If this condition holds true, do a; if it doesn't hold true, do b."--that is a ternary if...then...else.
00:14:04.000 --> 00:14:16.000
The next precedence is assignment: you see you have your assignments here, with add things together...subtract...divide...do bitshifts...
00:14:16.000 --> 00:14:50.000
Next is logical negation--not--and now we have gotten to our last three: logical composition--our or and our and; then, it's our expression modifiers--if, unless, while, until; and then the last one on our operation precedence is the block expression, which is begin and end.
00:14:50.000 --> 00:14:57.000
Next, let's look at the abbreviated math operations.
00:14:57.000 --> 00:15:06.000
A lot of times, when you code, you are going to see yourself calmly doing some things over and over again, so Ruby does allow some shortcuts.
00:15:06.000 --> 00:15:20.000
One of these is...you don't have to repeat yourself and keep trying...so, in this case, with the addition/subtraction/multiplication/division, you can actually shorten it.
00:15:20.000 --> 00:15:35.000
So, if I wanted to add 6 to this value, x, I can just do x+=6, and this is equivalent to x=x+6.
00:15:35.000 --> 00:15:40.000
It's going to add the value in, and it's going to set it back to itself.
00:15:40.000 --> 00:15:50.000
I can do that same thing with -=6, so it's x=x-6; same with multiplication and division.
00:15:50.000 --> 00:15:59.000
The only thing you need to do to do this is, you just move the operator in front of the equals sign.
00:15:59.000 --> 00:16:18.000
It would be x, operator, equals sign, and then whatever numeric value we have.
00:16:18.000 --> 00:16:34.000
This can be taken for plus, minus, multiplication, division...and now, we just shorten our code a few characters to save us a little typing.
00:16:34.000 --> 00:16:37.000
Next, let's look at numbers.
00:16:37.000 --> 00:16:46.000
First, to do this, we will look at the Numeric class.
00:16:46.000 --> 00:17:02.000
The Numeric class is the basis parent class for Bignum, Fixnum...all these classes...they all have an ancestral link, and they all are part of the Numeric class.
00:17:02.000 --> 00:17:09.000
This Numeric class gives you many methods to describe different numbers.
00:17:09.000 --> 00:17:25.000
We're going to look at five of them: I have this num--I've set it to value 100--and the first method we're looking at is, "Is this zero?"
00:17:25.000 --> 00:17:32.000
And zero? is false; it isn't zero; it would return true if it were.
00:17:32.000 --> 00:17:48.000
Also, there is this next method called nonzero?; nonzero says that if it is nonzero, it's to return self--in this case it would return 100--or else it's going to return nil, so if it is 0, it's going to return nil.
00:17:48.000 --> 00:17:54.000
Notice, it returns 100 in this case.
00:17:54.000 --> 00:17:59.000
The next method is real?--"Is it real?"--it returns true; 100 is real.
00:17:59.000 --> 00:18:05.000
"Is 100 an integer?"--it returns true.
00:18:05.000 --> 00:18:21.000
Next, we have this method called absolute value, abs; this will get you your absolute value.
00:18:21.000 --> 00:18:41.000
Notice, I have my minus sign, -12, and I just call .abs, which will call that method, and it will return me the absolute value, which is 12.
00:18:41.000 --> 00:18:45.000
Let's look at a few more Numeric methods.
00:18:45.000 --> 00:18:59.000
Here, we have...first, we will look at ceil: it returns the smallest integer greater than or equal to num.
00:18:59.000 --> 00:19:27.000
It does kind of a round up; so, for example, I have 1.5, and I call .ceil; it's going to go to the next highest integer--in this case, it's 2--the next highest integer, the smallest one.
00:19:27.000 --> 00:19:34.000
Next, let's look at floor: floor returns the largest integer less than or equal to num.
00:19:34.000 --> 00:19:45.000
So, if I call 1.5.floor, what's the lowest one from 1.5? That's 1, so that is what it is going to return to me.
00:19:45.000 --> 00:19:57.000
Then, you have this method called round; round will round a number to the nearest integer.
00:19:57.000 --> 00:20:18.000
For example, 1.5.round will round up, so it rounds up to 2, and if I call 1.4.round, it's going to round down to 1.
00:20:18.000 --> 00:20:25.000
Let's look at another example with the numbers--bring all of these concepts in together.
00:20:25.000 --> 00:20:31.000
First, I'm going to set x to a Float, 100.5.
00:20:31.000 --> 00:20:41.000
Next, I'm going to say on my next line, "If this is not an integer, I want you to round that number."
00:20:41.000 --> 00:20:46.000
So, what I'm doing is I'm saying, "I want to make this an integer if it isn't already."
00:20:46.000 --> 00:21:08.000
100.5 is not an integer, so this statement is going to return true, total, because it's not an integer, and I'm using the exclamation point to negate it, so it's going to return true.
00:21:08.000 --> 00:21:13.000
It says, "OK, if this is true, I want to round this number."
00:21:13.000 --> 00:21:17.000
It's going to round that number, and it's going to get 101.
00:21:17.000 --> 00:21:26.000
My next statement says, "Is it real?"--so, is 101 real?
00:21:26.000 --> 00:21:57.000
It will return true for that, and so it's going to say, for this statement...remember to break it down; this is an abbreviated statement--this is doing x=x/10, and this is actually 101 divided by 10, and the end result is just going to be 10, which is this here.
00:21:57.000 --> 00:22:05.000
x is real; it's going to divide this; and the end result is 10.
00:22:05.000 --> 00:22:19.000
At the end of this code, it's going to do puts x, and that is going to output this value, 10.
00:22:19.000 --> 00:22:29.000
Let's look at one more method, truncate: truncate returns num truncated to an integer.
00:22:29.000 --> 00:22:39.000
We are not doing any rounding; we're not doing ceil or floor; it's just going to cut off whatever is at the end of that decimal point, and make it an integer.
00:22:39.000 --> 00:22:49.000
If I do 1.1.truncate, it's going to truncate all of that out, so it's going to just be 1.
00:22:49.000 --> 00:23:00.000
If I do math.π, which is a constant, and do .truncate, it's just going to return 3.
00:23:00.000 --> 00:23:18.000
The last one is this step method: it will take whatever integer you give it, and it will increment it one by one, depending on what step you want to do.
00:23:18.000 --> 00:23:26.000
You can also pass in a limit, which says, "Continue incrementing until it hits this limit."
00:23:26.000 --> 00:23:44.000
For example, we have our num and our method step; here is our limit: pass that first, then pass the step--how much is it incrementing it by each time?--then, you pass in your code block.
00:23:44.000 --> 00:23:54.000
First, it's going to invoke the block, starting at the number, and then it's going to increment it by the step value.
00:23:54.000 --> 00:24:02.000
Then, the loop is complete when the value is greater than the limit; once it goes over that limit, then it's going to stop.
00:24:02.000 --> 00:24:33.000
An example is if I call 3.step, and I do...here is my limit: 10 is my limit...and then 3 is my step, and in my block, I just say, "Print which value I'm at now; what is my current step value?" and then add a space.
00:24:33.000 --> 00:24:46.000
When I do that, it's going to start with the initial value 3, so it's going to print 3, 6, and then 9.
00:24:46.000 --> 00:24:53.000
Afterwards, it stops, because then the value would go over the limit.
00:24:53.000 --> 00:25:16.000
If I do that again, this time starting with the value 1, the limit is 10 and the step is 2 (again, we are doing the same with printing it out), it's going to be 1, 3, 5, 7, and 9.
00:25:16.000 --> 00:25:27.000
That is all for the step method; let's go ahead and look at the RDoc for the Numeric class.
00:25:27.000 --> 00:25:34.000
You will see that a lot of the methods here, we have covered.
00:25:34.000 --> 00:25:55.000
Let's look at some of the methods: we talked about modulo...there is a unary plus and minus..."Returns the receiver's value"; "Returns the receiver's value, negated."
00:25:55.000 --> 00:26:05.000
Abs: absolute value of num; if you run abs2, it returns a square of itself.
00:26:05.000 --> 00:26:18.000
There are some other methods: phase, angle; we talked about ceil; we talked about coerce in an earlier Integer lesson.
00:26:18.000 --> 00:26:36.000
Conjugate; denominator; here is our div--you can see it even has our same reference here--it "uses the / to perform division, then converts the result to an integer."
00:26:36.000 --> 00:26:41.000
"This is left to subclasses."
00:26:41.000 --> 00:26:53.000
Also, notice that it tells you the result: it returns an integer, which is interesting: when we cast a Float in, it will return an Integer, too--that is the reason it's defined here in the RDoc.
00:26:53.000 --> 00:27:10.000
divmod returns an Array; it has a nice chart of how it does it, too, with the divmod, the modulo, and the remainder.
00:27:10.000 --> 00:27:19.000
It has the eql? method--"returns **true** if *num* and *numeric* are the same type and have equal values"
00:27:19.000 --> 00:27:29.000
It looks at the type and the value: don't let that throw you off--if you look at two different types, it's going to throw false at you.
00:27:29.000 --> 00:27:39.000
There are floor; complex values; imaginary; integer...
00:27:39.000 --> 00:27:46.000
Magnitude also does the absolute value; polar returns an Array.
00:27:46.000 --> 00:27:52.000
Real "returns **true** if *num* is a **Real**" number--non-complex.
00:27:52.000 --> 00:28:02.000
Remainder..."singleton_method_added" traps "attempts to add methods to **Numeric** objects"--you could add your own method to the Numeric class.
00:28:02.000 --> 00:28:07.000
Step: we already talked about it: they have a very specific definition to how it works.
00:28:07.000 --> 00:28:13.000
You can look at it there, if you want to dig into it.
00:28:13.000 --> 00:28:22.000
They have methods to convert to a complex value, integer...and we already talked about truncate and zero?.
00:28:22.000 --> 00:28:26.000
That is the Numeric RDoc.
00:28:26.000 --> 00:28:32.000
That concludes our first part of our Math Operations.
00:28:32.000 --> 00:28:41.000
We have had quite a bit of Numeric methods that we have gone over, and we will go over some more in the next lesson.
00:28:41.000 --> 00:28:47.000
Thank you for watching Educator.com, and see you next time!