Floating Point Numbers

Not Plain Ol' Numbers - Number with a Dot!

SparkiDuino
Easy

Lessons You Should Know

Floating point numbers are numbers that have decimal points. Unlike integers, floating point numbers let you use partial numbers. This means they are good to use when you do not know if there will be numbers after a decimal point, like when you divide and keep the remainder. Since “floating point numbers” is a mouthful to use every time programmers want to refer to these types of numbers this type of variable is usually called a “float.”Floats can also can be much bigger than integers. As big as 2^128 or small as 2^-127. That’s very, very big, for example: 340,282,340,000,000,000,000,000,000,000,000,000,000.0 or very, very small: 0.000000000000000000000000000000000000000000000014012985Here’s what we’ll be covering on this page:

Declaring a Float Variable

You create a float variable by declaring it in the same way that you declare an integer, except instead of using int you use float:
declareIntVarFoo
This will create a float named “foo” with a stored value of 0.0.

floatZeroC

Assigning Values (Putting Information in the Float Variables)

You can then change the float value by assigning a value to the variable. (Just like you did with integers.) This is done simple by writing the variable name followed by an equals sign, followed by the value you want to put in the variable. Then you end your command for assigning a variable with the all important semicolon. Everything else is pretty self explanatory but the semicolon is there so the computer knows that it has reached the end of a line of instructions that should now be executed. You can think of the semicolon as a green light that tells the computer it has all the parts of a command and it can start moving ones and zeros around.Here’s what it looks like when we assign the value of eight point zero zero six to the float variable “foo.”


floateightsix

You can think of a float variable like a mailbox with a letter in it. You can write any number on the paper inside the letter and the robot will look inside the letter anytime it sees the variable name that is on the mailbox. Then the robot will just insert whatever number is on the letter into the code it is running in place of the variable.You can also declare a float and assign a number to the variable in the same line of code. Here’s what it looks like to declare a float variable named “foo ” with the initial value of six point seven three:
floatSixSeven

Doing Math with Floats

Now that you know about floats, try using them instead of integers with the code we covered before:

Before, when we divided 10 by 4 using an integer we got an answer of 2. You will see that foo/blerg now gives the correct result since we used a float!

image10B

Float Variables Aren’t Always Exact

Floats are useful when you need to store partial numbers, very big numbers, or very small numbers. The downside is that unlike integers they can only store so much of the number. They are also not 100% accurate. If you add 1+1 for an integer, you get 2. If you add 1+1 using a float, you get:2.00000000000001.This is called a rounding error. Most of the time, this is okay, but 2 does not equal 2.00000000000001, so it is not useful when you need to exactly compare numbers. To avoid problems happening in your code, only use floats when you cannot use integers. 

Recap of What We’ve Covered So Far

or

Next Step:

You will often need to work with more than numbers. Sometimes you need to write text. For that, you will need to learn about characters:

Next Lesson – Characters