Class 2.5: more on integers and floating point

Integer math

The most important thing to keep in mind here is that division of two integers gives an integer:

1/3 = 0, 2/3 = 0, 3/3 = 1, 4/3 = 1, etc.

In particular:

Integer ranges

[1]This assumes the CPU uses "twos complement" form for signed integers, which all CPUs I know of today do.

Example: 8-bit signed integer

Binary Decimal
00000000 0
00000001 1
01111111 127
10000000 -128
10000001 -127
11111111 -1

In many (but not all) computer systems, a signed char happens to be an 8-bit signed integer.

Real numbers: Floating point representation

Real number math

Math with real numbers (i.e., floating point) works "as you'd expect":

1.0/4.0 = 0.25, 1.0/3.0 = 0.33333333333333331, 1.0/5.0 = 0.20000000000000001

The most important point to keep in mind is if you want to do real number math with a constant that happens to be an integer, just put in a decimal point.

In particular, if you're converting Fahrenheit to Celsius:

They give very slightly different answers for T=55 because neither 5./9. or 9./5. is exactly an integer divided by a power of two.