We've Got Your Number

We're going to start with some mat.... WAIT! Wait wait wait, not that type of math, calm down.

The only math you're going to need for this book is some basic Algebra. If you know what a variable is you're fine. If you don't, well you will within a page or two.

Maybe that doesn't quite add up yet, but it will in time. We'll subtract the math, divide it out into some chapters, and multiply our knowledge.

What's up Doc? The Documentation! If you ever want some more info take a look at the documentation, there's a lot more detail in there. This round we're taking a look at Integer and Float.

Docs

As well as some helpers in Math:

Basic Maths

Math in Ruby works pretty much like a regular ol' calculator, you put in a few numbers and symbols and get more numbers and symbols back out:

2.4.2 :001 > 1 + 1
 => 2
2.4.2 :002 > 1 - 1
 => 0
2.4.2 :003 > 2 * 2
 => 4
2.4.2 :004 > 4 / 2
 => 2
2.4.2 :005 > 3**2
 => 9

Though they went and hid some extra functions in this thing called Math:

https://ruby-doc.org/core/Math.html

2.4.2 :006 > Math.sqrt(9)
 => 3.0

Granted we could just use **(0.5) instead:

2.4.2 :007 > 9**(0.5)
 => 3.0

Note though that you can't use 1/2 there for reasons detailed later in the chapter.

Math is where you end up finding a lot of things like Trigonometric functions, logarithms, and other stuff we won't really need for this book. Nice to know it's there though.

Variable Situations

Now I said Algebra, but really all we need to worry about from it for now is what a variable is. Variables are fun, they let us put a name to a value:

2.4.2 :001 > a = 2
 => 2

A single equals sign assigns the value 2 to a variable we call a.

Now any time we mention a anywhere else around here it'll know we actually mean two. Nifty huh? We can store all types of stuff in variables, but for now a number will do. Try using it for a few things real quick, make sure it hasn't gone hiding from us:

2.4.2 :002 > a + 2
 => 4
2.4.2 :003 > a
 => 2

So just adding something doesn't change it, how would we add something to a and make it stick? We'd use = to set it:

2.4.2 :004 > a = a + 2
 => 4
2.4.2 :005 > a
 => 4

Maybe that's why they call them variables, they can vary a bit. So they're able to vary, vary-able. Etymologists eat your hearts out.

Personally though I don't like to type more than I have to, so say we want to just add one to a and have it stay there:

2.4.2 :006 > a += 1
 => 5
2.4.2 :007 > a
 => 5

So Ruby gives us some nice shorthand to play with too. Same thing works with *= and /=, give it a try!

Whatever Floats your Boat

Now one thing you may not notice is that Ruby likes to truncate numbers. What happens if you try 5 / 2? Before you type it, guess what it gives you. If you're already skeptical of it being 2.5, good for you, you've discovered Integer division and why the above 1/2 wouldn't work like one-half.

2.4.2 :001 > 5 / 2
 => 2

Integer is just a fancy word for number without a decimal point after it. In Ruby, if you divide two numbers like that it's just going to chop off the end so it can keep playing by Integer's rules.

( Pic - Samurai Lemur cutting off decimal )

That means no 2.5, you just get 2 instead. No rounding, we just cut the entire thing off of there. A division returns 2.8? Nope, still 2. How about 2.9999999999? Not this round, we still have 2.

If only there were a way to get those decimal points back, we could Float right on by that nasty truncation. Well, we could just tell it we want it to be a float:

2.4.2 :002 > 5 / 2.to_f
 => 2.5

Now that one of them is a Float, Integer has to play by its rules. That said, Float has a few of its own rules...

What happens if you try and add 0.1 and 0.2? What do you expect to happen?

2.4.2 :003 > 0.1 + 0.2
 => 0.30000000000000004

Ack! Curse you Dastardly Lemur! That's not what we wanted at all. What gives?

Well it turns out computers aren't so great with decimal points when it comes to Floats. Turns out I lied a bit, there's a bit more maths there, but for simplicities sakes we can say the computer gets close enough.

( Pic - Lemur with pie )

Remember that we used to say 3.14 was close enough for Pi in class? Pretty much that.

Really short version: close enough gets you in trouble when it comes to money when close enough just ain't good enough. Most other stuff? You should be fine.

If you do need to get a float to behave though, there are ways to make it:

2.4.2 :004 > 0.30000000000000004.round(1)
 => 0.3

Ah, that's better. Now it's a nice round number.

Computers work with something called Binary. That's a number system that has two digits, as opposed to the one you're used to with 10 digits.

That means if something's not a clean fraction ( 1/2, 1/4, etc vs 1/3 ) then Float is going to do some weird things.

Asking The Right Questions

So in Ruby, everything's an Object. (Well, maybe not everything) What's an Object? Well that doesn't matter for now, what it means is we can ask it questions and tell it things! They're called methods.

Here's the fun part, you've already been using methods on Objects. An Integer (or Number) is most certainly an Object, and it most certainly has methods:

2.4.2 :005 > 1 + 2
 => 3
2.4.2 :006 > 1.+(2)
 => 3

Wait wait, you mean that operators are methods? Yeah! Isn't that awesome? I doubt any of you write them like that, but it's fun to know and will come in particular handy later. You're already seen another one too, remember times?

2.4.2 :007 > 8.times { print 'Na' }
NaNaNaNaNaNaNaNa => 8
2.4.2 :008 > print ' LemurMan!'
 LemurMan! => nil

Turns out that's a method on an Integer too!

What an odd concept though, right?

2.4.2 :009 > 1.odd?
 => true

Why Lemur

Stop that!

Laughing Lemur

results matching ""

    No results matching ""