Skip to content

Numbers in Programming#

In earlier code examples, we used numbers already. We commonly used them for x and y coordinates, for red, green and blue parts of our colors and even some decimal numbers while we were drawing arcs. Computers distinguish between different "types", and there are even different types of numbers!

Types of Numbers#

  • int: can contain integer (whole) numbers. Goes from roughly –2 billion to 2 billion.
  • long: can also contain integer numbers, but can store ridiculously large numbers from roughly –10 quintillion to 10 quintillion (–9×1018 to 9×1018).
  • float: can contain numbers that we call "floating point" numbers. For now, this just means that this can store a number with a decimal point like 2.5 or 3.1415.
  • double: short for "double precision". Like float it can store numbers with a decimal point, but it can store larger numbers and/or more decimal places.

Integers stay integer

If you declare two ints and divide one by the other, please be aware that the result is also an integer! Meaning that the result of your calculation may not be what you expected!

int screenWidth = 1000;
int columns = 6;
int columnWidth = screenWidth / columns;
println(columnWidth); // this is now 166

println(columnWidth * columns); // hey, where did those 4 pixels go?

Humans would calculate this to be 166.66…, but integer division just throws away the fractional part.

Calculations! Calculations EVERYWHERE!#

You can do calculations almost everywhere while programming. Here's an example that I modified from our christmas tree code. Every coordinate I draw to comes from either treeRadius or treeHeight - but with a tiny bit of calculation in between. It is worth noting that we are not changing the variables. We are using them in our calculations and give the results to the various draw commands.

size(400, 400);

int treeRadius = 40;
int treeHeight = 100;

translate(100, 100);
fill(0, 100, 0);
beginShape();
vertex(0, 0);
// right side
vertex(treeRadius / 2, treeHeight / 3);
vertex(treeRadius / 3, treeHeight / 3);
vertex(treeRadius, treeHeight * 3 / 4);

// left side
vertex(-treeRadius, treeHeight * 3 / 4);
vertex(-treeRadius / 3, treeHeight / 3);
vertex(-treeRadius / 2, treeHeight / 3);

endShape(CLOSE);

// trunk
fill(70, 70, 0);
rect(-treeRadius / 8, treeHeight * 3 / 4, treeRadius / 4, treeHeight / 4); 

Copy and Paste to your processing IDE!

copy the code above into your processing IDE and try different values for treeRadius and treeHeight in lines 3 and 4!

dozens of parametrized trees drawn on top of each other

While it's not the most useful thing in the world, a parametrized tree can create a whole forest like this just by changing a few numbers around!

Changing your variables#

In this next example, we actually do want to change the value of our variable. Let's say your game has some kind of economy and you have credits that you can spend. Buying some item will reduce the amount of credits you own. In some way shape or form these following lines will be in ANY game that fits this description:

// let's start with 1000 credits!
int creditsInWallet = 1000;
int itemPrice = 50;

// this changes our credits variable to a new value
creditsInWallet = creditsInWallet - itemPrice;

// let's see what we've got left
println(creditsInWallet); // this will now print 950

For now, we don't worry where we would have gotten these numbers from, the important part is, that we can change them.