Sign In | Subscribe

Enter your Sign on user name and password.

Forgot password?
  • Follow us on:
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to Ruby
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!

Math Operations, Part 1

  • Numeric is the base class for numbers
  • It gives you many methods to describe different numbers
  • RDoc:
  • Float class holds real or floating-point numbers
  • Fixnum holds integer values represented by native machine word (minus 1)
  • Bignum holds a class of integers outside the range of Fixnum
  • Rational is a paired integer number
  • The Math module holds math related functions for trignometric and transcedental functions
  • There is a operator precedence that is followed to which specific operators will be processed before others can processed

Math Operations, Part 1

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Math Objects 0:12
    • Numeric
    • Integer
    • Float
    • Fixnum
    • Bignum
    • Rational
    • Math
  • Math Operations 2:36
    • Example
    • div(numeric)
    • divmod(numeric)
    • modulo(numeric)
    • quo(numeric)
    • remainder(numeric)
  • Operation Precedence 1 of 3 10:35
  • Operation Precedence 2 of 3 13:18
  • Operation Precedence 3 of 3 14:28
  • Abbreviated Math Operations 14:54
    • Move the operator in front of the equal sign
  • Numbers 16:36
    • Numeric Class
  • Numeric Methods 18:41
    • ceil
    • floor
    • round
  • Example with Numbers 20:20
  • Numeric Methods (Cont.) 22:20
    • truncate
    • num.step(limit, step)
    • Numeric Rdoc

Transcription: Math Operations, Part 1

Welcome back to

Today's lesson is on math operations, part 1.0002

First, let's go over a review of some of the math objects--first, the Numeric class, and then some of the math modules.0009

First, you have your Numeric class: this is the base class for all of your numbers.0023

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.0034

Then, you have your Float: Float is a class for real or floating-point numbers.0057

You then have your Fixnum, which is the most common one--which is your Integers.0065

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.0071

This is the bound--max bound.0090

If it flows outside of that bound, it's going to be automatically upcased to a Bignum.0098

You have your Fixnum, your Bignum...and Bignum is a class of integers outside the range of Fixnum.0107

Then, we have our class called Rational, which is a paired Integer number.0118

The last thing we want to do is--we have this Math module; the Math module holds math-related functions.0128

Mostly, it holds more basic ones that are for trigonometry and transcendental functions.0137

First, let's go over some of the basic operations.0153

You have your normal add, subtract, multiply, divide; you have the modulo, which gets the remainder of the division.0158

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.0169

We can go ahead and go through some basic ones in the IRB.0185

I can show you the addition 10+2; it's 12; got 12, too; 10-5...2-7...does it not like my comma?0191

10-5; 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...0210

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.0231

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.0242

If I do 15 modulo(5), since there is nothing, it's going to return 15modulo (5) returns 0; there is no remainder.0252

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.0264

That is basic operations.0284

Next, not only do you have these operators you can use; you can still use the methods--like the divide.0292

We have this method called div: it takes an argument--you pass in a numeric value to divide by.0302

It will perform division, and then it is going to convert the result to an integer.0310

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.0317

So out of the 24, I called the .div(2), and I'm going to return the result 12.0331

13.div(4)--it's going to return 3.0340

We do have that remainder, 1, and notice it doesn't worry about that.0346

This method says, "I'm just dividing and returning you what I'm able to divide by."0353

25.0--this is a Float--so notice, for these first two ones, we had Integers; for this one, we're doing a Float.0358

It's going to be 25.0 divided by 2.0, and it still returns an integer--it returns 12 still.0373

Next, we're looking at this method called divmod; this one gets us more specific to what we want.0387

Not only do we want what it divides by--we want the remainder, too.0391

This will return an Array that contains the quotient and the modulus.0395

Again, we go ahead with our 13 divided by 4, but this time, we call divmod; it's going to return an Array.0403

Here is our value 3, which is our quotient, and then we have our remainder, our modulus, which is 1.0413

15.divmod(5)--we have 3 and 0; 17.5.divmod(5)--we get 3 and 2.5.0425

Next, we are going to look at the method modulo.0440

This returns the modulus obtained by dividing the num by the Numeric.0444

This is where...we've talked about the quotient, we've talked about both, and now this will just return the modulus remainder.0453

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).0461

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.0476

Let's look at some more methods that you might possibly use.0495

The next one: we have this method called quo, and again, it takes a Numeric value.0503

This returns a Rational or a Float, and it performs division.0510

This one--you can get a much more specific value, since we're getting Float types or Rational types with it.0517

If I do 10.quo(5.1), we get this huge number--very specific.0527

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.0534

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.0550

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.0563

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 do that, it actually does a calculation using truncate.0571

It does the same thing as this here.0592

What it's doing is x-y times x/y; and it's doing the .truncate.0597

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.0609

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.0633

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."0660

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.0669

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.0677

We are going to go through three slides of it, from the highest precedence to the lowest.0688

The first one that has precedence is just element reference; notice that you can get the element reference using just square brackets, equal.0693

The next one that, surprisingly, has second precedence is the exponential--the raised to a power with the ^.0707

Third is the unary plus and minus--you have your +, -, your !, and your ~.0718

Then, the operations--math operations are next: your multiply, divide, modulo...and right below that is addition and subtraction, so your plus and minus...0727

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.0744

After that, your next precedence is the comparison operators: you have your less than/equals, less than, greater, and greater than/equals signs.0763

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...0771

We have gotten through our first slide.0794

Next, logical 'and' and 'or' come next on our list of precedence.0797

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.0804

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.0823

The next precedence is assignment: you see you have your assignments here, with add things bitshifts...0844

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.0856

Next, let's look at the abbreviated math operations.0890

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.0897

One of these don't have to repeat yourself and keep, in this case, with the addition/subtraction/multiplication/division, you can actually shorten it.0906

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.0920

It's going to add the value in, and it's going to set it back to itself.0935

I can do that same thing with -=6, so it's x=x-6; same with multiplication and division.0940

The only thing you need to do to do this is, you just move the operator in front of the equals sign.0950

It would be x, operator, equals sign, and then whatever numeric value we have.0959

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.0978

Next, let's look at numbers.0994

First, to do this, we will look at the Numeric class.0997

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.1006

This Numeric class gives you many methods to describe different numbers.1022

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?"1029

And zero? is false; it isn't zero; it would return true if it were.1045

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.1052

Notice, it returns 100 in this case.1068

The next method is real?--"Is it real?"--it returns true; 100 is real.1074

"Is 100 an integer?"--it returns true.1079

Next, we have this method called absolute value, abs; this will get you your absolute value.1085

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.1101

Let's look at a few more Numeric methods.1121

Here, we have...first, we will look at ceil: it returns the smallest integer greater than or equal to num.1125

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.1139

Next, let's look at floor: floor returns the largest integer less than or equal to num.1167

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.1174

Then, you have this method called round; round will round a number to the nearest integer.1185

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.1197

Let's look at another example with the numbers--bring all of these concepts in together.1218

First, I'm going to set x to a Float, 100.5.1225

Next, I'm going to say on my next line, "If this is not an integer, I want you to round that number."1231

So, what I'm doing is I'm saying, "I want to make this an integer if it isn't already."1241

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.1246

It says, "OK, if this is true, I want to round this number."1268

It's going to round that number, and it's going to get 101.1273

My next statement says, "Is it real?"--so, is 101 real?1277

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.1286

x is real; it's going to divide this; and the end result is 10.1317

At the end of this code, it's going to do puts x, and that is going to output this value, 10.1325

Let's look at one more method, truncate: truncate returns num truncated to an integer.1339

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.1349

If I do 1.1.truncate, it's going to truncate all of that out, so it's going to just be 1.1359

If I do math.PI, which is a constant, and do .truncate, it's just going to return 3.1369

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.1380

You can also pass in a limit, which says, "Continue incrementing until it hits this limit."1398

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.1406

First, it's going to invoke the block, starting at the number, and then it's going to increment it by the step value.1424

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.1434

An example is if I call 3.step, and I 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.1442

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.1473

Afterwards, it stops, because then the value would go over the limit.1486

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.1493

That is all for the step method; let's go ahead and look at the RDoc for the Numeric class.1516

You will see that a lot of the methods here, we have covered.1527

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."1534

Abs: absolute value of num; if you run abs2, it returns a square of itself.1555

There are some other methods: phase, angle; we talked about ceil; we talked about coerce in an earlier Integer lesson.1565

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."1578

"This is left to subclasses."1596

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.1601

divmod returns an Array; it has a nice chart of how it does it, too, with the divmod, the modulo, and the remainder.1613

It has the eql? method--"returns true if num and numeric are the same type and have equal values"1630

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.1639

There are floor; complex values; imaginary; integer...1649

Magnitude also does the absolute value; polar returns an Array.1659

Real "returns true if num is a Real" number--non-complex.1666

Remainder..."singleton_method_added" traps "attempts to add methods to Numeric objects"--you could add your own method to the Numeric class.1672

Step: we already talked about it: they have a very specific definition to how it works.1682

You can look at it there, if you want to dig into it.1687

They have methods to convert to a complex value, integer...and we already talked about truncate and zero?.1693

That is the Numeric RDoc.1702

That concludes our first part of our Math Operations.1706

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.1712

Thank you for watching, and see you next time!1721