Author : Mike Dane
Last Updated : October, 2017
Computers and numbers are like bread and butter. In fact the term computer actually used to refer to people who's job it was to do calculations and "compute" things all day. Needless to say we've come a long way since then, and modern computers are capable of performing billions of calculations in the time it takes a normal human to even write a number down.
Not to nerd out here too much, but when you get down to it, all computer instructions, all the code that we've written in this course, and all the code that's currently running on your computer, is actually just a collection of complex patterns of 0s & 1s running through your processor. And that same processor that's running everything on your computer is made up of a bunch of ALUs which stands for Arithmetic Logic Unit, basically a digital circuit which does basic math really really fast!
Integers vs Floats
The first thing I'd like to discuss is a very fundamental concept which is integral to mathematics as well as computer programming, which is the difference between an integer and a float.
These are two different types of numbers that we can work with in our programs, and it's important that you have a grasp of the differences so you can use them appropriately.
Let's start with integers which are whole numbers, or counting numbers. That means both negative numbers, positive numbers and zero, as long as they don't have decimal places after them.
Unlike integers, floats are any numbers with decimal places. Even if a float is technically a whole number like 42, in order for it to be a float it would have to have a .0 at the end of it 42.0.
Any time you're working with numbers in your programs, you always want to be aware of the type of number you're using, be it an integer or a float. Failure to do say may result in aspects of your program not performing as intended, or math calculations not giving precise enough answers.
Now that we have a grasp on the different types of numbers, let use them! You can tell a computer to perform any of the 4 basic math operations, addition, subtraction, multiplication and division by simply writing out the problem like you would normally.
* to indicate multiplication and the
/ to indicate division.
document.write( 2 * 3 + "<br>" ); // Basic Arithmetic: +, -, /, * document.write( 2**3 + "<br>" ); // Exponents document.write( 10 % 3 + "<br>" ); // Modulus Op. : returns remainder of 10/3 document.write( 1 + 2 * 3 + "<br>" ); // order of operations document.write(10 / 3.0 + "<br><br>"); // int's and doubles
Above are some common examples of math operation you might use.
The modulo operator will take the number on the left, divide it by the number on the right and then return the remainder. So if we ask the computer to perform
10 % 3 we would get back a 1, because 10/3 is 3 with a remainder of 1.
In the code above we are not making use of variables, but often times it's extra useful to store numbers in variables because numbers, unlike strings don't give us any context. If I see a random string in my program I can at least try to infer what it's purpose is by reading the text, the same can't always be said about numbers.
document.write( 2 * 3 + "<br>" ); // Basic Arithmetic: +, -, /, * document.write( 2**3 + "<br>" ); // Exponents document.write( 10 % 3 + "<br>" ); // Modulus Op. : returns remainder of 10/3 document.write( 1 + 2 * 3 + "<br>" ); // order of operations document.write(10 / 3.0 + "<br><br>"); // int's and doubles var num = 10; num += 100; // +=, -=, /=, *= document.write(num + "<br>"); num++; document.write(num + "<br><br>"); // Math class has useful math methods document.write( Math.pow(2, 3) + "<br>" ); document.write( Math.sqrt(144) + "<br>" ); document.write( Math.round(2.7) + "<br>" );