Lesson 5: Arithmetic Operators and Data Types


Arithmetic Operators

Arithmetic operators are used in equations.

=

Assignment operator

+

Add

-

Subtract

*

Multiply

/

Divide

%

Modulo division.

Returns the remainder of a a division.

++

Increments the current value by 1

Pre-increment (++i), Post-increment (i++)

--

Decrements the current value by -1

Pre-decrement (--i), Post-decrement (i--)

+=
-=
+=
/=

Compound addition (x += y is same as x = x + y)
Compound subtraction (x -= y is same as x = x - y)
Compound multiplication (x *= y is same as x = x * y)
Compound division (x /= y is same as x = x / y)

Data Types

short

Whole numbers, no decimal points, 16-bit signed

Max value is 215 - 1 (32,767)

Min value is -215 (-32,768)

int

Whole numbers, no decimal points. 32-bit signed

Trying to assign a double to an int variable type will always result in an int. The decimals are truncated not rounded.

Max value: 231 – 1 (2,147,483,647)

Min value: -231 (-2,147,483,648)

Default value is 0

long

Whole numbers, no decimal points. Similar to int. Long variables are extended size variables for number storage and store 32 bits (4 bytes)…which means the min and max values are the same as for a short.

Max value: 263 – 1 (9,223,372,036,854,775,807)

Min value: -263 (-9,223,372,036,854,775,808)

Default value is 0L

 

If doing math with integers, at least one of the numbers must be followed by an L, forcing it to be a long.

e.g.

long speedOfLight = 186000L;

float

Data type for storing floating-point numbers (a number with decimal points).

Max value: 3.4028235 x 1038

Min value: -3.4028235 x 1038

Stored as 32 bits (4 bytes) of information.

Floats have only 6 – 7 decimal digits of precision. That is, the total number of digits…not the number to the right of the decimal point.

Default value is 0.0

 

Floating point numbers are not exact and may yield strange results.

e.g.

6.0 / 3.0 may not equal 2.0

 

Note:

Floating point math is much slower than integer math in performing calculations, so should be avoided if, for example, a loop has to run at top speed for a critical timing function.

double

Double precision floating point number. On the Uno and other ATMEGA based boards, this occupies 4 bytes…meaning that a double is exactly the same as a float, with no gain in precision.

On Arduino Due, double is 8-byte (64-bit) precision and is more precise than float.

Default value is 0.0

Double data type is generally used as the default data type for decimal values

char

Single 16-bit Unicode character

Char data type is used to store any character

boolean

Boolean data type represents 1 bit of information

Only two possible choses: TRUE, FALSE

Default value is FALSE (0)