Working with Numbers

Photo by Nick Hillier on Unsplash

You would think "numbers would be numbers" in all languages but it truly isn't quite the case. All numbers are internally represented in binary inside the microcontroller/computer but how the programming language provides number support tends to vary from language to language.

Types of Numbers

• Integers
• Floating Point Numbers
• Boolean (true/false)

Often times, you need to know the precision - how big or small the number might be to select the best way to use it in a computer program. This makes using numbers a bit trickier but it is fairly easy to learn.

Arduino

In Arduino, you have the following types of variables:

• int for an integer, a value without a decimal point. typical ranges for an integer are -32,768 to zero to 32,767. Examples are 279, 1001, 0, -23, -990.
• long is a large integer and can be a value from -2,147,483,648 to 2,147,483,647.
• float for floating point numbers (numbers with a decimal point and fractional amount). Examples are 3.1415, -22.2, 0.0, 430000.01. Numbers can be as large as 3 x 10 to the 38th power
• char for a single character. For example, reading serial data may involve a receive function providing a character value when data is received. A character may basically be any symbol on the keyboard (0 to 255).

The types.h library provides a more modern representation of numbers that tend to behave the same on different devices.

An unsigned short integer, uint8_t, is often used by functions, it also ranges from 0 to 255.

Boolean math is generally done with int values. 0 is false and anything that is not zero, such as 1, is true.

Python / CircuitPython

CircuitPython tries to follow the standard Python implementation (often called CPython) as close as possible. Given microcontrollers do not have an operating system, some things are done a bit differently, but these are documented.

Here are the current types in CircuitPython:

• Integers, but not currently long integers
• Floating-Point Numbers.
• Strings
• Boolean

Boolean is an actual type in CircuitPython and can be the values True or False.

Rather than use arrays of characters (null/zero terminated) as Arduino does, CircuitPython has a dedicated Strings type. Note that CircuitPython Strings are NOT null/zero terminated, so use of the length properties is how you work with string length.

Changing the Type of Numbers

In Arduino/C this is called casting. Place the type of the number you want in parenthesis before the variable.

int a;
float b;

b = (float)a;
a = (int)b;

In CircuitPython, you use function-like syntax:

x = 5
y = 3.14

z = float(x)
z = int(y)

Division, One Slash vs. Two

A single forward slash / is floating point division in both languages.

A double slash // in Python is special. It divides and drops any values past the decimal point, often called a floor function.

Example:

# Python code for / and // operators
x = 15
y = 4

# Output: x / y = 3.75
print('x / y =', x/y)

# Output: x // y = 3
print('x // y =', x//y)

Logical Operators

Logical operators are used mainly in if statements and other block / loop statements. These are NOT the operators used for bitwise and/or/not, only for constructing logical comparisons.

 AND OR NOT Arduino / C && || ! Python / CircuitPython and or not
// Arduino / C Logical Operators

a = 5;
b = 7;

if( a > 2 && b < 10) {
Serial.println("Success");
}