3 Numbers and Arithmetic
Synopsis
- 16 bit unsigned short values go from 0 to 65535, while 32 bit unsigned long values go from 0 to more than 4 billion, and 64 bit long long integers are truly enormous.
- 16 bit short integers range from -32768 to 32767 while 32 bit long integers cover more than +/- 2 billion, and 64 bit long long integers are truly enormous.
- 32 bit floating point numbers go from -3.4E+38 to 3.4E+38 with about 7 decimal digits of accuracy. Use double precision (64 bit) when you can to to provide about 16 decimal digits of accuracy over a range of 1E+308.
- Integer arithmetic rolls over and wraps around at the limits, resulting in some non-intuitive results that you’ll need to read the rest of the chapter to better understand. Most importantly integer division means 3/2 results in 1 in most languages, but not Python v3 or higher.
Introduction
You already have a basic familiarity with math and numbers that you have developed from many years in school. You know about Natural Numbers, Whole Numbers, Integers, Real Numbers (Rational and Irrational), and maybe even Imaginary and Complex Numbers. You have used calculators that almost always gave you the answer you expected.
Countable Units or Unsigned Integers
If we are counting oranges, the choices are 0, 1, 2, 3,… up to a very large number of oranges, and we could write each orange’s number on the side with a Sharpie. That way we know how many oranges and which is which, assuming for illustration that oranges can’t be divided into smaller slices. It makes sense to talk about orange number 5, but not orange number 5.67. Any time we are talking about countable units, it only makes sense to have 0 or more units, and we can represent the value as a decimal or binary whole number.
The maximum number we can count up to is determined by the number of decimal or binary digits we use to represent the number, e.g. we can only count to 999 on a three digit decimal display. A four bit binary number can range from 0b0000 (0 decimal) to 0b1111 (1+2+4+8 = 15 decimal), sometimes represented as a single hexadecimal digit (0,1,2,…9,A,B,C,D,E,F), e.g. 0xC.
- Eight bits make a byte, ranging from 0b00000000 to 0b11111111 (0xFF hex or 255 decimal), usually used to represent an ASCII character. Also referred to as uint8_t
- 16 bits is the size of an unsigned short integer value, ranging from 0x0000 to 0xFFFF Also referred to as uint16_t
- A 32 bit value is referred to as an unsigned long integer, ranging from 0x00000000 to 0xFFFFFFFF Also referred to as uint32_t
- A 64 bit value is referred to as an unsigned long long integer, ranging from 0x0000000000000000 to 0xFFFFFFFFFFFFFFFF , and may not be available on small processors like microcontrollers. Also referred to as uint64_t
- With enough storage space it is possible to exactly represent an unsigned integer of any size, but we can usually get by with numbers less than 4 billion.
Rollover in Counting and Calculations
When we hit the boundaries we could g