Hardware Reference
In-Depth Information
Data Types
When creating variables, so far the only data type you've encountered is the
integer, which is meant for storing whole numbers, or in the case of
Example 4-1 , the digital pin states HIGH or LOW. But there are plenty of other
types of data that you can store in memory with Galileo.
int
Since you're already familiar with the basics of the integer, I'll give you a little
more information about it. Because an integer on Galileo is stored with 32
binary bits including one bit for the sign (positive or negative), you can count
up to the integer 2,147,483,647. If you add 1 to that number, it rolls over to
-2,147,483,648.
If 2,147,483,647 isn't high enough for you, you can also use an unsigned in-
teger , which takes away the bit needed to determine whether it's positive or
negative. An unsigned integer can count from 0 to 4,294,967,295. If you add
1 to that maximum, it will roll over to 0. Initializing a new unsigned integer is
as easy as initializing a regular integer:
unsigned int bigNumber = 4294967295;
Other Arduino boards have different ranges for the type inte-
ger. For instance, an integer on Arduino Uno can range be-
tween -32,768 and 32,767. Keep this in mind if you're porting
code to or from other boards.
Another important thing to know about integers is that if you do a math op-
eration such as division and expect a number with a decimal, the result will
simply be the whole number with the decimal chopped off. That's right, if you
were to divide two integers and normally expect a result of 2.75, the result
you'll actually get is 2! Try 11 divided by 4 on a calculator and in your code:
Serial.println(11/4);
// integer division result: 2
float
If you do need to work with decimals, the data type float is there for you. Its
name is short for floating point number . As with int s, you can store float
variables in memory. Here's how:
float cost = 29.95;
 
Search WWH ::




Custom Search