Class 6a ruby math

10
MATH HACKS IN RUBY Class 6a – Ruby Math

description

In this second half of Class 6, we will investigate a few of Ruby's capabiities to complete advanced formatting and mathematical calculation.

Transcript of Class 6a ruby math

Page 1: Class 6a   ruby math

MATH HACKS IN RUBYClass 6a – Ruby Math

Page 2: Class 6a   ruby math

Introduction

Ruby Basics Puts Gets Some basic math operations

“+”, “-”, “*”, “/” Formatting and Advanced Math

Line formatting (right/left justification, centering)

Exponents, roots, the Math Object

Page 3: Class 6a   ruby math

Agenda

Review the Assignment 3 solution Add formatting

to make interface and output more attractive

To make interface more intuitive To make output more functional

Advanced Math Exponents and Roots Absolute and Modulus Math::pi and rand

Page 4: Class 6a   ruby math

Formatting

Example – our class average assignment

Preferred format/ line length of 60 Student MarksSam 75Susan 78George 87Anne 92Joe 67_____ _____Average 76.5

Page 5: Class 6a   ruby math

Demonstration

IPO Input Processing Output

Code

Page 6: Class 6a   ruby math

Ruby Math: Exponent and Root

Exponent Notation a**b a to the power of b Example

Root Using Math Object

Math.sqrt(val) Using math formula

x**0.5

Page 7: Class 6a   ruby math

Ruby Math: Absolute & Modulus

Absolute Notation a=x-y.abs Example

Modulus (gizinta, what’s left?) Notation a=x%y Example: Kids Inc. Summer Camp

Page 8: Class 6a   ruby math

Ruby Math: Random & Pi

Random Use: rand(max_val) Result is zero based Example

Math:Pi Use Math::Pi*(val) Example: Volume of a cylinder IPO first?

Page 9: Class 6a   ruby math

Summary

Exponent (a**b) Root (sqrt or a**b/c.to_f) Absolute (abs) Modulus (a%b) Random (rand(x)) Pi (Math::PI)

Page 10: Class 6a   ruby math

Project 1: Calculator/Converter

Objectives Apply mathematical formulas to real

problems Apply a programmatic approach to a

solution Apply basic ruby mathematical

operations Apply basic ruby formatting

Grading – in 3 parts Due Date: Week of Nov 15 Next Week: Part of class for project

work, problems, challenges, ideas