Numeric Data Types
The information that is stored and manipulated by
computer programs is referred to as data.
There are two different kinds of numbers!
(5, 4, 3, 6) are whole numbers – they don't have a
fractional part
(.25, .10, .05, .01) are decimal fractions
Fundamentals of Computer & Programming 2
Numeric Data Types
Inside the computer, whole numbers and decimal
fractions are represented quite differently!
We say that decimal fractions and whole numbers
are two different data types.
The data type of an object determines what values it
can have and what operations can be performed on
it.
Fundamentals of Computer & Programming 3
Numeric Data Types
Whole numbers are represented using the integer
(int for short) data type.
These values can be positive or negative whole
numbers.
Fundamentals of Computer & Programming 4
Numeric Data Types
Numbers that can have fractional parts are
represented as floating point (or float) values.
How can we tell which is which?
A numeric literal without a decimal point produces
an int value
A literal that has a decimal point is represented by a
float (even if the fractional part is 0)
Fundamentals of Computer & Programming 5
Numeric Data Types
Python has a special function to tell us the data type
of any value.
>>> type(3)
<class 'int'>
>>> type(3.1)
<class 'float'>
>>> type(3.0)
<class 'float'>
>>> myInt = 32
>>> type(myInt)
<class 'int'>
>>>
Fundamentals of Computer & Programming 6
Numeric Data Types
Operations on ints produce ints, operations on floats
produce floats (except for /).
>>> 3.0+4.0 >>> 10 // 3
7.0 3
>>> 3+4 >>> 10.0 // 3.0
7 3.0
>>> 3.0*4.0 >>> abs(-5.4)
12.0 5.4
>>> 3*4 >>> abs(3)
12 3
>>> 10.0/3.0
3.3333333333333335
>>> 10/3
3.3333333333333335
Fundamentals of Computer & Programming 7
Type Conversions & Rounding
We know that combining an int with an int
produces an int, and combining a float with a
float produces a float.
What happens when you mix an int and float in
an expression?
x = 5.0 * 2
y = 3.44 + 6
What do you think should happen?
Fundamentals of Computer & Programming 8
Type Conversions & Rounding
For Python to evaluate this expression, it must either
convert 5.0 to 5 and do an integer multiplication, or
convert 2 to 2.0 and do a floating point
multiplication.
Converting a float to an int will lose information
Ints can be converted to floats by adding ".0"
Fundamentals of Computer & Programming 9
Type Conversions & Rounding
In mixed-typed expressions Python will convert ints
to floats.
>>> 4 * 2.5
10.0
Sometimes we want to control the type conversion.
This is called explicit typing.
Converting to an int simply discards the fractional
part of a float – the value is truncated, not
rounded.
Fundamentals of Computer & Programming 10
Type Conversions & Rounding
To round off numbers, use the built-in round
function which rounds to the nearest whole value.
If you want to round a float into another float
value, you can supply a second parameter that
specifies the number of digits after the decimal point.
Fundamentals of Computer & Programming 11
Type Conversions & Rounding
>>> float(22//5)
4.0
>>> int(3.9)
3
>>> float(int(3.3))
3.0
>>> round(3.9)
4
>>> round(3)
3
>>> round(3.1472926, 2)
3.15
Fundamentals of Computer & Programming 12
Type Conversions & Rounding
Even for converting a numerical string to the number
>>> int("32")
32
>>> float("32")
32.0
>>> float("32.5")
32.5
Remember that the string must be a number
>>> int('this 22')
ValueError: invalid literal for int()
Fundamentals of Computer & Programming 13
Type Conversions & Rounding
This is useful as a secure alternative to the use of
eval for getting numeric data from the user.
x = int(input("please enter a whole number:"))
please enter a whole number:12
Fundamentals of Computer & Programming 14
Type Conversions & Rounding
Using int instead of eval ensures the user can
only enter valid whole numbers – illegal (non-int)
inputs will cause the program to crash with an error
message.
x = int(input("please enter a whole number:"))
please enter a whole number:6.75
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10:
'6.75'
Fundamentals of Computer & Programming 15
Type Conversions & Rounding
One downside – Using int instead of eval does not
accommodate simultaneous input.
x, y = int(input("please enter two whole number:"))
please enter two whole number:4, 5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10:
'4, 5'
Fundamentals of Computer & Programming 16
Using the Math Library
Besides (+, -, *, /, //, **, %, abs), we
have lots of other math functions available in a math
library.
A library is a module with some useful
definitions/functions.
Fundamentals of Computer & Programming 17
Using the Math Library
Let's write a program to compute the roots of a
quadratic equation!
𝑎𝑥 2 + 𝑏𝑥 + 𝑐
b b 2 4ac
x
2a
The only part of this we don't know how to do is find
a square root… but it's in the math library!
Fundamentals of Computer & Programming 18
Using the Math Library
Flowchart
Fundamentals of Computer & Programming 19
Using the Math Library
To use a library, we need to make sure this line is in
our program:
import math
Importing a library makes whatever functions are
defined within it available to the program.
Fundamentals of Computer & Programming 20
Using the Math Library
To access the sqrt library routine, we need to
access it as math.sqrt(x).
Using this dot notation tells Python to use the sqrt
function found in the math library module.
To calculate the root, you can do
discRoot = math.sqrt(b*b – 4*a*c)
Fundamentals of Computer & Programming 21
Using the Math Library
# quadratic.py
# A program that computes the real roots of a quadratic
equation.
# Note: This program crashes if the equation has no real roots.
import math # Makes the math library available.
print("This program finds the real solutions to a quadratic")
a, b, c = eval(input("Please enter the coefficients (a, b, c): "))
discRoot = math.sqrt(b * b - 4 * a * c)
root1 = (-b + discRoot) / (2 * a)
root2 = (-b - discRoot) / (2 * a)
print("The solutions are:", root1, root2 )
Fundamentals of Computer & Programming 22
Using the Math Library
This program finds the real solutions to a quadratic
Please enter the coefficients (a, b, c): 3, 4, -1
The solutions are: 0.215250437022 -1.54858377035
Fundamentals of Computer & Programming 23
Using the Math Library
What do you suppose this means?
This program finds the real solutions to a quadratic
Please enter the coefficients (a, b, c): 1, 2, 3
ValueError: math domain error for discRoot = math.sqrt(b*b-4*a*c)
If a = 1, b = 2, c = 3, then we are trying to take the square root of
a negative number!
Fundamentals of Computer & Programming 24
Using the Math Library
Python Mathematics English
pi An approximation of pi
e e An approximation of e
sqrt(x) x The square root of x
sin(x) sin x The sine of x
cos(x) cos x The cosine of x
tan(x) tan x The tangent of x
asin(x) arcsin x The inverse of sine x
acos(x) arccos x The inverse of cosine x
atan(x) arctan x The inverse of tangent x
Fundamentals of Computer & Programming 25
Using the Math Library
Python Mathematics English
log(x) ln x The natural (base e) logarithm of x
log10(x) log10 x The common (base 10) logarithm of x
exp(x) ex The exponential of x
ceil(x) x The smallest whole number >= x
floor(x) x The largest whole number <= x
Fundamentals of Computer & Programming 26
Using the Math Library
Output:
Fundamentals of Computer & Programming 27
Accumulating Results:
Factorial
Say you are waiting in a line with five other people.
How many ways are there to arrange the six people?
720 -- 720 is the factorial of 6 (abbreviated 6!)
Factorial is defined as:
n! = n(n-1)(n-2)…(1)
So, 6! = 6*5*4*3*2*1 = 720
Fundamentals of Computer & Programming 28
Accumulating Results:
Factorial
How we could we write a program to do this?
1. n Input number to take factorial of it
2. fact Compute factorial of n
3. Output fact
Fundamentals of Computer & Programming 29
Accumulating Results:
Factorial
How did we calculate 6!?
6*5 = 30
Take that 30, and 30 * 4 = 120
Take that 120, and 120 * 3 = 360
Take that 360, and 360 * 2 = 720
Take that 720, and 720 * 1 = 720
Fundamentals of Computer & Programming 30
Accumulating Results:
Factorial
What's really going on?
We're doing repeated multiplications, and we're
keeping track of the running product.
This algorithm is known as an accumulator, because
we're building up or accumulating the answer in a
variable, known as the accumulator variable.
Fundamentals of Computer & Programming 31
Accumulating Results:
Factorial
The general form of an accumulator algorithm looks
like this:
Initialize the accumulator variable
Loop until final result is reached
update the value of accumulator variable
Fundamentals of Computer & Programming 32
Accumulating Results:
Factorial
It looks like we'll need a loop!
fact = 1
for factor in [6, 5, 4, 3, 2, 1]:
fact = fact * factor
Let's trace through it to verify that this works!
Fundamentals of Computer & Programming 33
Accumulating Results:
Factorial
Why did we need to initialize fact to 1? There are a
couple reasons…
Each time through the loop, the previous value of
fact is used to calculate the next value of fact.
By doing the initialization, you know fact will have
a value the first time through.
If you use fact without assigning it a value, what
does Python do?
Fundamentals of Computer & Programming 34
Accumulating Results:
Factorial
Since multiplication is associative and commutative,
we can rewrite our program as:
fact = 1
for factor in [2, 3, 4, 5, 6]:
fact = fact * factor
Great! But what if we want to find the factorial of
some other number??
Fundamentals of Computer & Programming 35
Accumulating Results:
Factorial
What does range(n) return?
0, 1, 2, 3, …, n-1
range has another optional parameter!
range(start, n) returns
start, start + 1, …, n-1
But wait! There's more!
range(start, n, step)
start, start+step, …, n-1
list(<sequence>) to make a list
Fundamentals of Computer & Programming 36
Accumulating Results:
Factorial
Let's try some examples!
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(5,10))
[5, 6, 7, 8, 9]
>>> list(range(5,10,2))
[5, 7, 9]
Fundamentals of Computer & Programming 37
Accumulating Results:
Factorial
Using this souped-up range statement, we can do
the range for our loop a couple different ways.
We can count up from 2 to n:
range(2, n+1)
(Why did we have to use n+1?)
We can count down from n to 2:
range(n, 1, -1)
Fundamentals of Computer & Programming 38
Accumulating Results:
Factorial
Our completed factorial program:
# factorial.py
# Program to compute the factorial of a number
n = eval(input("Please enter a whole number:"))
fact = 1
for factor in range(n,1,-1):
fact = fact * factor
print("The factorial of", n, "is", fact)
Fundamentals of Computer & Programming 39
The Limits of Int
What is 100!?
Please enter a whole number: 100
The factorial of 100 is
93326215443944152681699238856266700490715968264
38162146859296389521759999322991560894146397615
65182862536979208272237582511852109168640000000
00000000000000000
Wow! That's a pretty big number!
Fundamentals of Computer & Programming 40
The Limits of Int
similar program written using Java:
#run 1
Please enter a whole number: 6
The factorial is: 720
#run 2
Please enter a whole number: 12
The factorial is: 479001600
#run 3
Please enter a whole number: 13
The factorial is: 1932053504
We know that 13! = 6227020800
Java program has given us an incorrect answer!
Fundamentals of Computer & Programming 41
The Limits of Int
Newer versions of Python can handle it, but…
Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)]
on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
Please enter a whole number: 13
13
12
11
10
9
8
7
6
5
4
Traceback (innermost last):
File "<pyshell#1>",
File "C:\PROGRA~1\PYTHON~1.2\fact.py", line 5, in main
fact=fact*factor
OverflowError: integer multiplication
Fundamentals of Computer & Programming 42
The Limits of Int
What's going on?
While there are an infinite number of integers, there
is a finite range of ints that can be represented.
Fundamentals of Computer & Programming 43
The Limits of Int
The most fundamental unit of computer memory can
be a tiny magnetic region on a hard disk, a tiny dent
in the reflective material on a CD or DVD, or a tiny
transistor on a memory stick.
Whatever the physical implementation, computer
memory is composed of electrical "switches,"
each of which can be in one of two possible states,
basically on or off.
Fundamentals of Computer & Programming 44
The Limits of Int
Each switch represents a binary digit or bit of
information.
One bit can encode two possibilities, represented with
the numerals 0 (for off) and 1 (for on).
A sequence of bits can be used to represent more
possibilities.
A collection of 8 bits is called a byte
A collection of 4 bytes, or 32 bits, is called a word.
Fundamentals of Computer & Programming 45
The Limits of Int
With two bits, we can represent four things:
2*2
It includes 00, 01, 10, 11
This representation is called binary notation
Fundamentals of Computer & Programming 46
The Limits of Int
Three bits allow us to represent eight different
values:
2*2*2
It includes 000, 001, 010,
011, 100, 101, 110,111
Fundamentals of Computer & Programming 47
The Limits of Int
Each extra bit doubles the number of distinct
patterns.
In general, n bits can represent 2n different values.
The number of bits to represent an int depends on
the design of the CPU.
Fundamentals of Computer & Programming 48
The Limits of Int
Typical PCs use 32 bits or 64.
That means there are 232 possible values
Unsigned numbers: from 0
The range then is 0 to 232-1
Signed numbers: centered at 0
This range then is -231+1 to 231-1
• We need to subtract one from the top end to account for 0.
Fundamentals of Computer & Programming 49
The Limits of Int
Range for 4 bits integer
unsigned vs. signed
Fundamentals of Computer & Programming 50
The Limits of Int
1 bit 2 different values
2 bits 4 different values
3 bits 8 different values
4 bits 16 different values
5 bits 32 different values
6 bits 64 different values
…
n bits 2𝑛 different values
What about 29, 65 or 41 different values?
Fundamentals of Computer & Programming 51
The Limits of Int
Map decimal size to binary approximation and
needed bits
Fundamentals of Computer & Programming 52
The Limits of Int
32 bits (4 byte) signed int example
00000000 00000000 00000000 00000001 as 1
00000000 00000000 00000000 00000010 as 2
10000000 00000000 00000000 00000011 as -3
10000000 00000000 00000000 00000100 as -4
00000000 00000000 00000000 00000101 as 5
Respectively i=0,i=1,…
(−1) sign * σwhen bit=1 2i = number
Fundamentals of Computer & Programming 53
The Limits of Int
32 bits (4 byte) unsigned int example
00000000 00000000 00000000 00000001 as 1
00000000 00000000 00000000 00000010 as 2
10000000 00000000 00000000 00000011 as 2147483651
10000000 00000000 00000000 00000100 as 2147483652
00000000 00000000 00000000 00000101 as 5
Respectively i=0,i=1,…
σwhen bit=1 2i = number
Fundamentals of Computer & Programming 54
The Limits of Int
32 bits (4 byte) signed int
Example: 00000000000000000000000000111001
Sign is positive (+1)
+1∗(20 +23 +24 +25 ) = +1∗(1+8+16+32)=57
Example: 10000000000001000000100100000000
Sign is negative (-1)
−1∗(28 +211 +218 ) = −1∗(256+2048+262144)=−264448
Fundamentals of Computer & Programming 55
The Limits of Int
32 bits (4 byte) unsigned int
Example: 00000000000000000000000000111001
20 +23 +24 +25 = 1+8+16+32=57
Example: 10000000000001000000100100000000
28 +211 +218 +231 = 256+2048+262144+2147483648
=2147748096
Fundamentals of Computer & Programming 56
The Limits of Int
Decimal to binary
Fundamentals of Computer & Programming 57
The Limits of Int
Decimal to binary
Fundamentals of Computer & Programming 58
The Limits of Int
let's try to make sense of what's happening in the
Java factorial example.
If the Java program is relying on a 32-bit int
representation, what's the largest number it can
store?
>>> 2**31-1
2147483647
This value lies between 12! and 13!
The Java program is fine for calculating factorials up to 12,
but after that the representation "overflows" and the results
are garbage.
Fundamentals of Computer & Programming 59
The Limits of Int
Overflow
In computer programming, an integer overflow occurs
when an arithmetic operation attempts to create a
numeric value that is outside of the range that can be
represented with a given number of digits
• either larger than the maximum or lower than the minimum
representable value.
For two bits, in unsigned system 4 is outside of range.
• Number 4 (binary: 100) needs 3 bits.
• We can see just two bits of them; namely 00.
Fundamentals of Computer & Programming 60
The Limits of Int
Adding unsigned numbers
For one bit:
• 0+0 = 0, with no carry,
• 1+0 = 1, with no carry,
• 0+1 = 1, with no carry,
• 1+1 = 0, and you carry a 1 #1+1=10
For 3 bits:
• 110+111=101, you carry a 1
• it returns 101 but actually is 1101
Fundamentals of Computer & Programming 61
The Limits of Int
But our 100! in python is much larger than this. How
does it work?
Fundamentals of Computer & Programming 62
Handling Large Numbers
Does switching to float data types get us around
the limitations of ints?
If we initialize the accumulator to 1.0, we get
Please enter a whole number: 30
The factorial of 30 is 2.652528598121911e+32
We no longer get an exact answer!
30! = 265252859812191058636308480000000
Fundamentals of Computer & Programming 63
Handling Large Numbers
Very large and very small numbers are expressed in
scientific or exponential notation.
2.652528598121911e+32 means
2.652528598121911 * 1032
Here the decimal needs to be moved right 32
decimal places to get the original number, but there
are only 16 digits, so 16 digits of precision have been
lost.
Fundamentals of Computer & Programming 64
Handling Large Numbers
For n bits, we can only go as high as the integer 2n-1
but at least we know that we can account for all of
the integers up to a point.
We cannot do the same thing for reals.
Starting from 0, what should be the step?
• 0.1? 0.01? 0.00000001?
Suppose we chose 0.01,
• It makes 0, 0.01, 0.02, 0.03, 0.04,…
• We can now only go as high as 0.01 * (2n-1), and we
have missed all of the numbers between 0.01 and 0.02
Fundamentals of Computer & Programming 65
Handling Large Numbers
This is another important limitation of storing
information on a computer: there is a limit to the
precision that we can achieve when we store real
numbers.
Most real values cannot be stored exactly on a
computer.
Examples of this problem include not only exotic values
such as transcendental numbers (e.g., pi and e), but
1
also very simple everyday values such as or even 0.1.
3
Fundamentals of Computer & Programming 66
Handling Large Numbers
This is not as dreadful as it sounds, because even if the
exact value cannot be stored, a value very very close
to the true value can be stored.
>>>0.3-0.2
0.09999999999999998
So for practical purposes this is usually not an issue.
The limitation on numerical accuracy rarely has an
effect on stored values
because it is very hard to obtain a scientific measurement with
this level of precision.
Fundamentals of Computer & Programming 67
Handling Large Numbers
However, when performing many calculations, even tiny
errors in stored values can accumulate and result in
significant problems.
>>>0.1 + 0.1 + 0.1
0.30000000000000004
So, Floats are approximations.
Floats allow us to represent a larger range of values
than a 32-bit int, but with fixed precision.
Fundamentals of Computer & Programming 68
Handling Large Numbers
(24.589)10
index … 3 2 1 0 . -1 -2 -3 …
Value … 103 102 101 100 . 10-1 10-2 10-3 …
base10
*
digit … 0 0 2 4 . 5 8 9 …
number … 0 + 0 + 2*101 + 4*100 . 5*10-1 + 8*10-2 + 9*10-3 …
Fundamentals of Computer & Programming 69
Handling Large Numbers
(101.001)2
index … 3 2 1 0 . -1 -2 -3 …
Value … 23 22 21 20 . 2-1 2-2 2-3 …
base2
*
digit … 0 1 0 1 . 0 0 1 …
number … 0 + 1*22 + 0 + 1*20 . 0 + 0 + 1*2-3 …
Fundamentals of Computer & Programming 70
Handling Large Numbers
The number is
(24.589)10 = 2*101 + 4*100 + 5*10-1 + 8*10-2 + 9*10-3
(101.001)2 = 22 + 20 + 2-3 #5.125
Scientific notation
(923.52)10 = 9.2352 * 102
(101011.101)2 = 1.01011101 * 25 #43.625
1.xxxxxx * 2y
Fundamentals of Computer & Programming 71
Handling Large Numbers
IEEE-754 Floating Point
When a single word (32 bits) is used to store a real
number,
a typical arrangement uses 8 bits for the exponent
and 23 bits for the mantissa (plus 1 bit to indicate
the sign of the number).
1.xxxxxx * 2y
y is from here xxxxxx is this
Fundamentals of Computer & Programming 72
1.xxxxxx * 2y
Handling Large Numbers
The exponent mostly dictates the range of possible
values.
8 bits allows for a range of integers from -127 to
127, which means that it is possible to store numbers
as small as 2-127 and as large as 2127
The mantissa dictates the precision with which
values can be represented.
The issue here is not the magnitude of a value
With 23 bits, it is possible to represent 223 different
real values,
• which is a lot of values, but still leaves a lot of gaps.
Fundamentals of Computer & Programming 73
Handling Large Numbers
For example, if we are dealing with values in the range 0
1
to 1, we can take steps of 23 ≈0.0000001,
2
so, we cannot represent any of the values between
0.0000001 and 0.0000002.
in other words, we cannot distinguish between numbers
that differ from each other by less than 0.0000001.
If we deal with values in the range 0 to 10000000, we
10000000
can only take steps of ≈1,
223
so we cannot distinguish between values that differ from
each other by less than 1.
Fundamentals of Computer & Programming 74
Handling Large Numbers
Below are the real values stored in IEEE-754 Floating Point
standard.
00111111 10000000 00000000 00000000 as 1*20*1=1.0
01000000 00000000 00000000 00000000 as 1*21*1=2.0
11000000 01000000 00000000 00000000 as -1*21*1.5=-3.0
11000000 10000000 00000000 00000000 as -1*22*1=-4.0
01000000 10100000 00000000 00000000 as 1*22*1.25=5.0
x Respectively i=1,i=2,…
(−1)sign * 2x-127 *(1+σwhen bit=1 2−i )= number
Fundamentals of Computer & Programming 75
Handling Large Numbers
Example: convert 11000000101101000000000000000000 into decimal
Sign is negative (-1)
Exponent
10000001 = (129)10
129-127 = 2
22 = 4
Mantissa
1 1 1
1+ 2−2 +2−3 +2−5 =1+ 4 + 8 + 32 =1.40625
Sign*Exponent*Mantissa = (-1)*(4)*(1.40625) = -5.625
Fundamentals of Computer & Programming 76
Handling Large Numbers
Example: convert 11000001110100000000000000000000 into decimal
Sign is negative (-1)
Exponent
10000011 = (131)10
131-127 = 4
24 = 16
Mantissa
1 1
1+ 2−1 +2−3 =1+ 2 + 8 =1.625
Sign*Exponent*Mantissa = (-1)*(16)*(1.625) = -26
Fundamentals of Computer & Programming 77
Handling Large Numbers
Example: convert 43.625 into 32-bit floating point representation
(43.625)10 = (101011.101)2 0.625 × 2 = 1.25 1
0.25 × 2 = 0.5 0
101011.101 = 1.01011101 * 25
0.5 × 2 = 1.0 1
Sign bit is positive (0)
Exponent
5 + 127 = 132 = (10000100)2
Mantissa
01011101000000000000000
43.625 = 01000010001011101000000000000000
Fundamentals of Computer & Programming 78
Handling Large Numbers
Example: convert -17 into 32-bit floating point representation
(17)10 = (10001)2 17 doesn't have
fractional part
10001 = 1.0001 * 24
Sign bit is negative (1)
Exponent
127+4=131 = (10000011)2
Mantissa:
00010000000000000000000
-17 = 11000001100010000000000000000000
Fundamentals of Computer & Programming 79
Handling Large Numbers
Example: convert -0.1 into 32-bit floating point representation
0.1 × 2 = 0.2 0
(0.1)10 = (0.00011001…)2 0.2 × 2 = 0.4 0
0.4 × 2 = 0.8 0
0.00011001… = 1.1001… * 2-4 0.8 × 2 = 1.6 1
Sign bit is negative (1) 0.6 × 2 = 1.2 1
0.2 × 2 = 0.4 0
Exponent 0.4 × 2 = 0.8 0
-4 + 127 = 123 = (01111011)2 0.8 × 2 = 1.6 1
…
Mantissa
10011001100110011001101
-0.1 = 10111101110011001100110011001101
Fundamentals of Computer & Programming 80
Handling Large Numbers
Example: convert 1.7 into 32-bit floating point representation
0.7 × 2 = 1.4 1
(1.7)10 = (1.101100110011…)2 0.4 × 2 = 0.8 0
0.8 × 2 = 1.6 1
1.101100… = 1.101100… * 20 0.6 × 2 = 1.2 1
Sign bit is positive (0) 0.2 × 2 = 0.4 0
0.4 × 2 = 0.8 0
Exponent 0.8 × 2 = 1.6 1
0 + 127 = 127 = (01111111)2 0.6 × 2 = 1.2 1
…
Mantissa
10110011001100110011010
1.7 = 00111111110110011001100110011010
Fundamentals of Computer & Programming 81
Handling Large Numbers
The number 7/10 or 0.7 is a repeating fraction in binary, just
as the fraction 1/3 is a repeating fraction in decimal.
We cannot represent this exactly as a floating point number. The
closest we can come in 23 bits is 0.10110011001100110011010
Since we already have a leading 1, the best 32-bit number we can
make is 1.10110011001100110011010
• Indeed, 1.7 is stored as 1.7000000476837158203125
But 0.5 is OK.
(0.5)10 = (00111111000000000000000000000000)2
• 0.5 is stored exactly as 0.5
Fundamentals of Computer & Programming 82
Handling Large Numbers
How can you tell if an arbitrary decimal has an
equivalent binary that terminates or repeats?
convert the decimal to an integer over a power of ten and
then do binary division. If you get a remainder of zero, the
binary is terminating; if you encounter a working dividend
you've seen before, the binary is repeating.
a decimal has an equivalent terminating binary if and only if
the decimal, written as a proper fraction in lowest terms,
has a denominator that is a power of two.
• 0.1 = 1/10, and 10 is not a power of 2
• 0.5 = 5/10 = 1/2, and 2 is a power of 2
Fundamentals of Computer & Programming 83
Handling Large Numbers:
Long Int
Python has a solution for large, exact values.
Expanding ints
Python ints are not a fixed size and expand to handle
whatever value it holds.
Newer versions of Python automatically convert your
ints to expanded form when they grow so large as to
overflow.
We get indefinitely large values (e.g. 100!) at the cost
of speed and memory
Fundamentals of Computer & Programming 84
Handling Large Numbers:
Long Int
In order to perform operations on larger numbers,
Python has to break down the operations into smaller
units that the computer hardware is able to handle.
• similar to the way you might do long division by hand.
These operations will not be as efficient (they require
more steps),
• but they allow our Python ints to grow to arbitrary size.
o And that's what allows our simple factorial program to
compute some whopping large results.
This is a very cool feature of Python.
Fundamentals of Computer & Programming 85
Handling Large Numbers
Summary
Both ints and floats are represented on the underlying
computer using a fixed-length sequence of bits. This
imposes certain limits on these representations. Hardware
ints must be in the range (-231+1) . . . (231-1) on a 32-
bit machine. Floats have a finite amount of precision and
cannot represent most numbers exactly.
Python's int data type may be used to store whole
numbers of arbitrary size. lnt values are automatically
converted to longer representations when they become too
large for the underlying hardware int. Calculations involving
these long ints are less efficient than those that use only
small ints.
Fundamentals of Computer & Programming 86
Handling Text
character
'A', '*' , '3', ' ', '?', …
for storing each character, computer first convert it to
an int then save the int value
• 'A' is saved as (65)10 = (01000001)2
• '_' is saved as (95)10 = (01011111)2
• '3' is saved as (51)10 = (00110011)2
ASCII is an encoding representing each typed character by a
number
• Each number is stored in one byte
Fundamentals of Computer & Programming 87
Handling Text
ASCII to Decimal
Fundamentals of Computer & Programming 88
Handling Text
string
putting chars together makes a string.
“How are you?", "658", …
The text "hello" (104, 101, 108, 108, 111)
would look like this: 01101000 01100101
01101100 01101100 01101111.
Fundamentals of Computer & Programming 89
Handling Text
string
100 typed letters takes up 100 bytes
Fundamentals of Computer & Programming 90
Encoding and Decoding
Encoding refers to the way data is understood within
the computer for storage.
Since, a computer can only understand two values 0
and 1, each value that you want to store or send
across has to be converted into a sequence of 0's
and 1's.
When storing something, the value will be encoded
and then stored. And when reading it back, it will be
decoded and then displayed to you.
Fundamentals of Computer & Programming 91