Giter Site home page Giter Site logo

simple-math-ldn-b-elamar's Introduction

Math

Objectives

  1. Practice doing basic math in Ruby
  2. Understand how division in Ruby differs from normal division
  3. Understand the modulo operator
  4. Play with the Math module
  5. Create appropriate behavior for methods

Introduction

Programming languages have basic arithmetic built-in. The +, -, *, and / methods are part of Ruby.

Ruby can perform any operation that a simple calculator can. Open up IRB (open your terminal then type irb and hit enter) and type the commands below:

  • 1 + 1
  • 1 * 3
  • Math.sqrt(81)
  • 9 ** 2

Order of Operations

Enclosing an expression in parentheses () defines an order of operations, like you would expect.

  • 98 + 59/13 * 8 * -5 = -62
  • 98 + (59/(13*8)) * -5 = 98

Division and Intro to Floats

Type 9 / 2. You would expect the result to be 4.5, but it's actually 4. This is because of the type of number you are using. In Ruby, and most programming languages, numbers can be integers (whole numbers), or floats(decimal numbers). When you divide integers, they return integers without rounding. The decimal is just removed. If you type 9.0, you're creating a float, so 9.0 / 2.0 = 4.5. Numbers with decimals are called floats and when you divide floats a float is returned. If you divide a float and an integer, a float will be returned, so 9.0/2 = 4.5.

Modulo operator

There is another operator that is really useful in programming. It's the modulo, and it's represented with the percent sign %. The modulo operator gives you the remainder of a number divided by another number. So 5 % 3 = 2.

Math module

Ruby has built-in methods for common math functions. The basic trigonometric and transcendental functions are available in the Math module. This gives you access to mathematic tools such as square root, sine, cosine, and tangent via Ruby methods. To access these in your code simply refer to the module Math and call the method on the module. For example, to get the square root of 9 you'd write Math.sqrt(9). Read more about the Math module and the available methods.

Instructions

Fork and clone this repo and open lib/math.rb. You'll find a bunch of empty methods that take numbers as arguments. Build the appropriate behavior for each of the following methods:

  • addition
  • subtraction
  • multiplication
  • division
  • modulo
  • square root
  • order of operation

The lab is test driven, so get the tests to pass.

View Math on Learn.co and start learning to code for free.

simple-math-ldn-b-elamar's People

Contributors

ahimmelstoss avatar annjohn avatar arelenglish avatar changamanda avatar deniznida avatar dfenjves avatar dougtebay avatar drewprice avatar fislabstest avatar fs-lms-test-bot avatar irmiller22 avatar joshuabamboo avatar kthffmn avatar msuzoagu avatar p1xt avatar sarogers avatar sophiedebenedetto avatar

Watchers

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