EARTH 801
Computation and Visualization in the Earth Sciences

Lesson 3: Expand our Repertoire with Floats

PrintPrint

0. Other games you can play

What if we want to make the lightning bolt move faster? Change the line where we are incrementing the value of x to something like:

x = x + 5;

What if we want to make the lightning bolt move slower? Change the line where were are incrementing the value of x to something like:

x = x + 0.5;

If you haven't programmed before, then it may take some extra thinking to grasp the meaning of an expression such as x = x + 5;. If this were math class, then you'd quickly realize that there is no such value of x that will make the equation x = x + 5 work. You'd collect like terms and be left with the expression 0 = 5 which is no good. But this isn't math class.

Two things to remember here:

  1. Recall what I said before about equals signs. One is telling, two is asking.
  2. Math and math-like actions are done right to left in pretty much all procedural programming languages, including this one.

So the expression x = x + 5; is not there to ask the computer to solve for x. What it is saying is: take the value of x that you have in memory right now, add 5 to it, and then take whatever answer you get and reassign that number to x. (Tell x to be the new value you just computed.)

If x were 1, then after performing the command x = x + 5, the value of x will be 6. Any other command or expression performed from here on out will substitute "6" every time x shows up in the program, until x gets reassigned to something else.

1. Float variables

There's no rule that says we have to use whole numbers, except for the fact that when we first declared x in our program, we declared it as an integer. That means we cannot give it a fractional part. Therefore if you write x=x+0.5 like I suggested you try out, you will get an error telling you "cannot convert from float to int." What's that about?

If we want to be able to allow x to have a fractional part, we need x to be a floating point number instead of an integer. These are called "floats" in Processing. A float is a floating-point number, which is a number that is not an integer, such as 1.5 or PI (π). Why bother with the difference between ints and floats? Why not just call them all “numbers” or something? Well, one difference between a float and an int is the amount of memory your computer has to allocate to store it. Also, integers are precise and floats are not.  
Let’s say I have a cookie. I can declare int numCookie = 1 to tell Processing that I have exactly one cookie. If I’m going to share it with 6 other friends of mine I will break it into seven pieces. If I am really accurate at cookie-breaking we will all get exactly one seventh of the cookie. We can write this precisely as 1/7 on paper. However, if we wanted to express that in a Processing program, we’d probably declare something like

float cookiePortion0 = 0.14;
float cookiePortion1  = 0.14;
float cookiePortion2  = 0.14;
float cookiePortion3  = 0.14;
float cookiePortion4  = 0.14;
float cookiePortion5  = 0.14;
float myCookiePortion = 0.14;

If we add up all the floats we get 0.98, not 1. We could write out each number to more decimal places. 1/7 is actually 0.142857 . . .  but it is a repeating decimal. We can’t write an infinite number of digits past the decimal because we don’t want our entire computer’s memory taken up by deciding how much of a cookie each person is getting. The computer has to truncate the number at some point, and it will probably be good enough, but it is not perfect. That's the difference between an int and a float.

In our lightning bolt program, we'll change the declaration of the variable x up at the top to say

float x = 10;

and then we are all set to increment x by a float later on in the program.

We also don't have to make the lightning bolt move from right to left. It can go wherever we want, limited by imagination only.

To make it go down instead of sideways, increment y instead of x, like so:

y++;

if (y == height) {

   y = 0;

}

If you want the lightning bolt to move diagonally, change both x and y with each loop through draw.

If you want the lightning bolt to move back and forth, you have to find a way to use the if test to change the direction of motion in x or y or both. Try it yourself and then check my screencast below to see how I did it.

cartoon Eliza alerting you that instructions come nextHere's a screencast example of a program in which the lightning bolt moves back and forth horizontally across the screen (3:15).
Screencast of moving lightning bolt.
Click here for a transcript of the moving lighning bolt video.

There's a couple of new things in this program. One is the addition of this variable, which is shorthand for x direction. And we're giving it the value of one to start with. So when we increment x we're saying x equals x plus 1. So that's not really changing anything that we had before. But now, down here, we're going to have an if test, and we're going to say if x becomes greater than the width of the window, then we are going to multiply x direction by negative 1 so that it becomes a negative number. Then the next time processing loops through draw, x will be going the other direction, because this is negative 1 instead of positive 1. All right. So that looks like this. And what we're hoping is that the lightning bolt's going to turn around when it gets here. And it does. That's good. That is what we wanted. But now we have a problem, which is it runs off the left side of the picture never to return. So what we really want to do is not just say that if x equals the width of the window to turn around, we want it to say, if x equals the width of the window or if it comes back and equals 0, we want it to turn around again. So the way to do that is to add something into this if test. We want to tell it if x becomes greater than the width of the window, or x becomes less than 0, to multiply the direction by negative 1. So that way-- you can see what's going to happen. If it gets to the edge of the window on the right side, XDir becomes negative 1. Then when it gets back to 0, we'll multiply this by negative 1. Again, it will become positive 1, it will go back the other way. This thing that I typed here means or, and it's accomplished by typing two pipes, two vertical lines. And where you find that on your keyboard is normally on the same key as the Backspace key, at least on a Mac keyboard, between delete and return. So look for that. Let's watch this one run. That's good. Yeah, that's what we wanted. And this will just keep going infinitely back and forth until I tell it to stop. I actually think it would be more aesthetically pleasing if the lightning bolt on the right side didn't run all the way off the page before it came back, and we can do that by making it bounce back at a different place. So specifically, the widest this lightning bolting ever is I think is x plus 40, and that occurs in two places. So why don't we tell it a turnaround when it gets to the value of width minus 40? I bet that will look better. Let's try that. Oh good, that's what I wanted. And this will keep going over and over again until we tell it to stop by shutting it down here.

E. Richardson