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 - The largest positive integer that can be represented is 230-1, 1073741823, and the most negative integer possible is -230, -1073741824.
- As of CircuitPython 3.0, Express boards have arbitrarily long integers as in Python.
- Floating-Point Numbers - Floating point numbers are single precision in CircuitPython (not double precision as in Python). The largest floating point magnitude that can be represented is about +/-3.4e38. The smallest magnitude that can be represented with full accuracy is about +/-1.7e-38, though numbers as small as +/-5.6e-45 can be represented with reduced accuracy.
- Strings - 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.
- Boolean - Boolean is an actual type in CircuitPython and can be the values True or False.
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)
A single forward slash / is floating point division in both languages.
A double slash // in Python is special. It divides and rounds the result down to the nearest whole number, often called floor division.
Example:
# Python code for / and // operators x = 15 y = 4 z = -15 v = 4 # Output: x / y = 3.75 print('x / y =', x/y) # Output: x // y = 3 print('x // y =', x//y) # Output: z / v = -3.75 print('z / v =', z/v) # Output: z // v = -4 print('z // v =', z//v)
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"); }
# CircuitPython Logical Operators a = 5 b = 7 if a > 2 and b < 10: print("Success")
Text editor powered by tinymce.