Giter Site home page Giter Site logo

programming-univbasics-arithmetic-expressions-dumbo-web-062419's Introduction

Arithmetic Expressions

Learning Goals

  • Trace Resolution of Variables to Scalar Values to Return Value
  • Recognize + as the symbol for addition
  • Demonstrate the addition operator
  • Recognize - as the symbol for subtraction
  • Demonstrate the subtraction operator
  • Recognize * as the symbol for multiplication
  • Demonstrate the multiplication operator
  • Recognize / as the symbol for division
  • Demonstrate the division operator
  • Identify how division in Ruby differs from normal division

Introduction

When we first introduced expressions, we presented a series of operators and their corresponding operations that were familiar from basic arithmetic:

Operator Operation Note
+ Addition
- Subtraction
* Multiplication We use * instead of × because it looks like x-the-letter
/ Division We use / instead of ÷ because that symbol's not on keyboards
** Exponentiation We use ** instead of ^ because ^ means something else in programming languages
() Association Expressions inside of () get evaluated earlier

Back then, we didn't know how to use variable lookup expressions yet. We had an intuitive grasp of how operators worked with constant expressions like 5 + 1. Knowing what 5 and 1 and + mean meant that we could guess that the expression would evaluate to 6.

But how does Ruby work with an expression like x + 1? It uses the "variable lookup expression!"

Box shake out

When Ruby sees a variable in an expression it swaps in the variable's value as if a constant expression were there.

Trace Resolution of Variables to Scalar Values to Return Value

x = 1 # Assignment expression of constant value 1 to variable x
x + 2 #=> 3 ; Variable lookup "replaces" x with 1 and then evaluates 1 + 2 => 3

Keep in mind that swapping in a constant value, for Ruby, is the same as swapping in a complex expression that must be evaluated to produce a value.

x = 3 * (10 - 4)
x + 2 # Ruby "sees" (3 * (10 - 4)) + 2
(3 * (10 - 4)) + 2
(3 * 6) + 2
(18) + 2
18 + 2 #=> 20; Ruby does the necessary substitutions until it evaluates data and operators

Recognize + as the Symbol for Addition

Addition's symbol should look familiar. The symbol that is used to perform this operation is +.

Demonstrate the Addition Operator

To get a feel for performing addition operations in Ruby, let's experiment. Open up IRB by opening your terminal; type irb and hit enter and type the commands below:

10 + 1 #=> 11
5 + -1 #=> 4

Recognize - as the Symbol for Subtraction

Like addition, the symbol for subtraction, -, is also the same as in common arithmetic.

Demonstrate the Subtraction Operator

Let's experiment again. Open IRB and type these commands below:

4 - 13 #=> -9

And don't forget that losing negative things is positive!

11 - -11 #=> 22

Recognize * as the Symbol For Multiplication

The symbol for the multiplication is an asterisk, * so that we don't confuse it with the letter x.

Demonstrate the Multiplication Operator

Use IRB and try typing the following the multiplication commands:

10 * 10 #=> 100
11 * -11 #=> -121

If everything was typed correctly, your results should be 100 and -121. Nothing out of the ordinary, right?

Recognize / as the Symbol For Division

The symbol for the division is a forward slash: /. There's no handy ÷ character on most keyboards, so programmers adopted /.

Division behaves differently in Ruby than you might expect

Pay attention as we demonstrate it.

Demonstrate the Division Operator

Open up IRB and try typing the following the division commands:

8 / 3 #=> 2
4 / 13  #=> 0

That's surprising! Division is a little bit different in Ruby-land.

Identify How Division in Ruby Differs from Normal Division

Now we just saw something a little bit strange, and it's related to "data type," which we just learned about in the previous lesson.

In Ruby and most programming languages, numbers can be Integers (whole numbers), or Floats (decimal numbers). When you divide Integers by one another, Ruby doesn't want to upset you by returning a non-Integer, it thinks you're a "big picture" kinda thinker — "just stuff to the left of the decimal, please."

So, 9 / 2 is 4 and the remainder (½) is helpfully left off.

By the same reasoning, if one of the numbers in the expression were a Float, Ruby would get the hint that we want a precise answer, and it would respond correctly.

9.0 / 2 #=> 4.5

Obviously, two Floats in a division tells Ruby you're very concerned about details and will return a Float:

9.0 / 2.0 #=> 4.5

Take those "surprising" results from the previous section, make one, the other, or both Floats and verify that you're back to getting the precision you expect.

Bonus Operator - the Modulo

In addition to the addition, subtraction, multiplication, and division operators, Ruby also has a fifth operator for math, the Modulo: %. When used, Ruby will return the remainder from dividing the first number from the second:

4 % 2 #=> 0

Four is evenly divided by two, leaving no remainder.

5 % 2 #=> 1

Five, however, is not evenly divided by two, leaving a remainder of one. This can be a handy way to tell if a number is even or odd - if a number is evenly divisible by two, it is even.

Conclusion

We now have the ability to perform most complex arithmetic calculations. We're on our way to launching rockets to the Moon!

programming-univbasics-arithmetic-expressions-dumbo-web-062419's People

Contributors

maxwellbenton avatar sgharms avatar

Watchers

 avatar Mohawk Greene avatar Victoria Thevenot avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar  avatar  avatar Ben Oren avatar Matt avatar Antoin avatar  avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Kaeland Chatman avatar Lisa Jiang avatar Vicki Aubin avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.