Upload
theawless
View
223
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Scribe for Number Systems.
Citation preview
Number Systems
-Abhinav Singh (140101002)
-Rajan Garg (140101057)
Introduction
A numeral system (or system of numeration) is a writing system for expressing numbers, that is, a mathematical notation for representing numbers of a given set, using
digits or other symbols in a consistent manner.
It can be seen as the context that allows the symbols "11" to be interpreted as the binary symbol for three, the
decimal symbol for eleven, or a symbol for other numbers in different bases.
Contents1. Brief History
2. The Number Sense
3. Classification of Numbers
4. Number System(Definition)
5.Nth Number System
6. Decimal System
7. Binary System
8. Hexadecimal System
9. Does number system affect rationality?
10. Conversion of Decimal to Binary
11. Motivation of the Algorithm
12. Proof of Correctness
13. C++ code Decimal to Nth conversion algorithm
14. Conversion of Binary to Decimal
15. References
Brief History
Thousands of years ago there were no numbers to represent two or three. Instead fingers, rocks, sticks or eyes were used to represent numbers. There was little need for a numeric system until groups of people formed clans, villages and settlements and began a system of bartering and trade that in turn created a demand for currency. How would you distinguish between five and fifty?
Paper and pencils were not available to transcribe numbers. Other methods were invented for means of communication and teaching of numerical systems. Babylonians stamped numbers in clay by using a stick and depressing it into the clay at different angles or pressures and the Egyptians painted on pottery and cut numbers into stone.
Evolution of Numbers
Quipu - An Inca Counting System
Imagine, if you will, a highly advanced civilization. This civilization rules over a million or more people, they built vast cities, developed extensive road systems. Now imagine being able to do all this without a written language. They did this using a memory tool made of knotted strings called a quipu
Yet, it is unknown how a quipu was used but it is believed that the strings were more or less used like a decimal system.
The Mayan Number System
The Mayan number system dates back to the fourth century and was approximately 1,000 years more advanced than the Europeans of that time. This system is unique to our current decimal system, which has a base 10, in that the Mayan's used a vigesimal system, which had a base 20.
This system is believed to have been used because, since the Mayan's lived in such a warm climate and there was rarely a need to wear shoes, 20 was the total number of fingers and toes, thus making the system workable.
The Greek Number System
The Greek numbering system was uniquely based upon their alphabet.
The original Greek alphabet consisted of 27 letters and was written from the left to the right. These 27 letters make up the main 27 symbols used in their numbering system.
Problems with Ancient Number Systems-
1. In the above examples we see that to represent a number we require a huge number of digits or symbols. So to minimize the space required to represent a number we need a more compact numbering system. Space complexity is O(n). This led to problems since not enough paper or rocks were available.
2. No concept 0, neither the tally system nor the Roman numeral system had the concept of 0.
3. No concept of bit addition. That is arithmetic operations couldn’t be done on individual digits
Sense of Numbers
“The number sense is not the ability to count, but the ability to recognize that something has changes in a small collection.”
Some animal species are capable of this.
The number of young that the mother animal has, if changed, will be noticed by all mammals and most birds. Mammals have more developed brains and raise fewer young than other species, but take better care of their young for a much longer period of time.
Many birds have a good number sense. If a nest contains four eggs, one can safely be taken, but when two are removed the bird generally deserts. The bird can distinguish two from three
One might think people would have a very good number sense, but as it turns out, people do not. Experiments have shown that the average person has a number sense that is around four.
Classification of Numbers
Irrational
Complex
Real Rational
Integers Whole Natural
Number Systems
“A number system is a writing system for expressing numbers, that is, a mathematical notation for representing numbers of a given set, using digits or other symbols in a consistent manner.”
The value of a number akak-1…a1a0 which is in base x system is given by (where 0≤ai<x for 0≤x≤k)
akx k + ak-1x k-1+…+a1x 1+a0x 0
Ideally, a numeral system will:
Represent a useful set of numbers (e.g. all integers, or rational numbers)
Give every number represented a unique representation (or at least a standard representation)
Reflect the algebraic and arithmetic structure of the numbers.
Few important points:
1. A number system with base X contain only numbers from 0,1,2….X-1.
Example:
A binary system contains only 1 and 0s.
A octal system(base 8) contains only 0,1,2,3,4,5,6,7.
A hexadecimal system(base 16) contains only
0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F.
2. A number in one system can be changed to a number
in another system.
Example:
A number 1310 is nothing but 11012 .
3. Value of a number akak-1…a1a0 which is in base x
system is given by. :
akxk + ak-1xk-1 + …+ a1x1 + a0x0.
N th Number System
In general, if b is the base, one writes a number in the numeral system of base b by expressing it in the form
anbn + an − 1bn − 1 + an − 2bn − 2 + ... + a0b0
And writing the enumerated digits anan − 1an − 2 ... a0 in descending order.
The digits are natural numbers between 0 and b − 1, inclusive.
That is, Digits={0,……,b-1} if b-1<=9, otherwise we might have to other symbols for example, in case of Hexadecimal number system we have to use Alphabets {A,B,C,D,E,F} too.
But total number of usable digits= b.
In general, numbers in the base b system are of the form:
The numbers bk and b−k are the weights of the corresponding digits.
Decimal Number System
The decimal numeral system (also called base 10) has ten as its base. It is the numerical base most widely used by modern civilizations.
Base (also called radix) = 10
o 10 digits { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
Digit Position
o Integer & fraction
Digit Weight
o Weight = (Base) Position
Magnitude
o Sum of “Digit x Weight”
Formal Notation
o ak10 k + ak-110k-1 + …+ a1101 + a0100
Example:
(493)10 = 4*102 + 9*101 + 3*100.
Position 3 2 1 0 −1 −2 . . .
Weight
Digit
Decimal example weight 1000 100 10 1 0.1 0.01 . . .
Decimal example digit 4 3 2 7 0 0 . . .
More about decimal system.
The Chinese counting rod system and the Hindu-Arabic numeral system (the latter descended from Brahmi numerals) are the first sources of decimal system.
Ten fingers on two hands, the possible starting point of the decimal counting.
Ten is the number which is the count of fingers and thumbs on both hands (or toes on the feet). The English word digit as well as its translation in many languages is also the anatomical term for fingers and toes. In English, decimal (decimus < Lat.) means tenth, decimate means reduce by a tenth, and denary (denarius < Lat.) means the unit of ten.
Positional Notation
Binary Number System Base (also called radix) = 2
o 2 digits { 0, 1 }
Positional notation number system
o Left most bit is “most significant bit” or MSB
MSB Binary Digit LSB
28 27 26 25 24 23 22 21 20
256 128 64 32 16 8 4 2 1
o Right most bit is “least significant bit” or LSB
Formal Notation
o ak2 k + ak-12k-1 + …+ a121 + a020
Example:
(101.01)2 = 1*22 + 0*21 + 1*20 + 0*2-1 + 1*2-2
Binary Number Names
Number of Binary Digits
(bits)Common Name
1 Bit
4 Nibble
8 Byte
16 Word
Why is Binary System used in Digital?
On/Off signals provide very reliable operation. Extremely low possibility of errors when transferring data because it is easy to decide the difference between the on and off state of the circuit and some noise or disruption during the transfer process will not disrupt that decision.
Binary devices modelled after binary number system are Simple and easy to build
Binary Numbers which are transmitted in the form of binary signals are unambiguous (which gives them noise immunity)
Flawless copy can be made of binary data Anything that can be represented with some sort of
pattern can be represented with pattern of bits.
Since entire processing in computer happens in binary digits (base 2), we have our memory cards, pen drives memories as a powers of 2 i.e., 1GB, 2GB, 4GB, 8GB, 16GB, 32GB etc.
Hexadecimal Number System
Base = 16 o 16 digits { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F }
Weightso Weight = (Base) Position
Magnitudeo Sum of “Digit x Weight”
Formal Notationo Example(2E6.A3)16 = 2*162 + E*161 + 6*160 + A*16-1 + 3*16-2
Hexadecimal has its own advantages:Binary coded byte values can range from 0 to 255 (decimal) but may be more conveniently represented as two hexadecimal digits in the range 00 through FF. Hexadecimal is also commonly used to represent computer memory addresses.
Does number system affect rationality?
Note that a number has a terminating or repeating expansion if and only if it is rational; this does not depend on the base. A number that terminates in one base may repeat in another (thus 0.310 = 0.0100110011001...2).
An irrational number stays aperiodic (with an infinite number of non-repeating digits) in all integral bases.
Thus, for example in base 2, π = 3.1415926...10 can be written as the aperiodic 11.001001000011111...2.
Conversion from decimal to binary number system
There are many ways to convert a decimal number to a binary number. Few of them are listed below
Informal Way
1. Given a number N in decimal system.
2. Calculate the highest power of 2 which is less than N. Let it be some K.
Eg:(Lets Solve the problem of converting N=3810 to binary) Highest power of 2 which is less than 38 is 32 (25).
3. Subtract the number K from N and output 1(i.e., push this into a stack).
Eg: When we subtract 32 from 38, we get 6 as result. Push 1 into stack .
4. Now modify N =result obtained and K = next least power of 2
Eg: The result obtained after subtraction is 6. So, Now make N=6 and K=16(Next least power of 2 i.e., 25-1=24)
5. Now compare K with N.
a. If K is greater than N (k don’t have capacity to contribute to N), then output 0 (push 0 into stack) and modify K =Next highest power of 2.
b. If K is less than N (k have capacity to contribute to N), then output 1(push 1 into stack) and subtract K from N . Modify N= result of subtraction and K =Next highest power of 2. Eg: K=16 and N=6. K>N (k don’t have capacity to contribute to N). Hence output 0 and K=8. 6. Repeat step v. until N=0 and then pop all items from the
stack. Finally, 3810= 1001102
Eg: Convert 9710 into binary system. Sol: Let N=97. Highest power of 2 less than 97 is 64. Hence, k=64. Compare k with N .k<N (subtractable) .Hence [output 1] and subtract k from N
Modify N=N-k=97-64=33. (Here output means either print or enqueue into a queue) Modify k=Next highest power of 2 = 32. Compare k with N .k<N (subtractable) .Hence [output 1] and subtract k from N. Modify N = N-k =33-32=1. Modify k=Next highest power of 2= 16. Compare k with N. k>N (not subtractable). Hence [output 0]. Modify k=Next highest power of 2= 8.
Initial value of N=97
Step No.
Value of N
Value of K
Modified value of N
Modified value of K
Output (In Queue)
1 97 64 97-64=33 32 1 2 33 32 33-32=01 16 1 3 01 16 01 08 0 4 01 08 01 04 0 5 01 04 01 02 0 6 01 02 01 01 0 7 01 01 01-01=00 00 1
Since N=0, stop here and dequeue from queue.
Compare k with N. k>N (not subtractable). Hence [output0]. Modify k=Next highest power of 2= 4.
Compare k with N. k>N (not subtractable). Hence [output 0]. Modify k=Next highest power of 2= 2. Compare k with N. k>N (not subtractable). Hence [output 0]. Modify k=Next highest power of 2= 1. Compare k with N .k<N (subtractable) .Hence [output 1] and subtract k from N. Modify N = N-k =1-1=0. Since, N =0, halt the process here. Now dequeue the queue if you have used a queue. So corresponding binary number is 11000012
Hence, 9710=11000012
DISADVANTAGES:
Note that, for larger numbers we need a table containing still higher powers of 2.
For larger numbers it will be very difficult to anticipate the number in terms of powers of 2
Procedural Way
Case I: For Natural Numbers
i. Let a no. N= akak-1ak-2…a1a0 be a decimal number to be converted into base x system.
ii. Divide N by x. Let quotient=q and output the remainder (push into a stack)
iii.Now assign N=q.
iv.Repeat steps ii. And iii. Until N=0.
v. Pop out all the numbers from stack and print them. Hence the binary number is displayed.
Eg : Let’s convert 4310 into binary system.
Sol: Binary system have a base =2. Divide 43 by 2 and collect remainder. Divide the quotient again by 2 and collect remainder and repeat the procedure until we get quotient as 0.
Note that the numbers written after hyphen in each row is the remainder of the corresponding division performed. Write the remainders in the direction of arrow mark and finally answer is
4310 = 1010112
Direction of Counting
Eg : Let’s convert 15610 into binary system.
15610 = 100111002
Eg: Let’s convert 6710 into septal system.
Sol : Septal system have base =7.
Divide 67 by 7 and collect remainder. Divide the quotient again by 7 and collect remainder and repeat the procedure until we get quotient as 0.
Note that the numbers written after hyphen in each row is the remainder of the corresponding division performed. Write the remainders in the direction of arrow mark and finally answer is
6710 = 1247
Direction of Counting
Direction of Counting
Eg : Let’s convert 29310 into nonary (base 9) system.
Sol: Nonary system have a base 9. So, instead of x, put 9 and solve the problem. i.e., Divide 293 by 9 and collect remainder. Divide the quotient again by 9 and collect remainder and repeat the procedure until we get quotient as 0.
Note that the numbers written after hyphen in each row is the remainder of the corresponding division performed. Write the remainders in the direction of arrow mark and finally answer is
29310 = 3559
Direction of Counting
Case II: For Numbers with Decimal points
i. Convert the integer part of number N into base x number system. This will be the integer part of corresponding number of base x.
ii. Now we have to convert fraction part of N into corresponding base x number.
iii.Let fraction part on N be F = .a0a1a2…ak
Multiply F with x. output the integer part of product (either in a queue or in a array or directly print it) and modify F=fractional part of the product.
Eg: Let N = 34.2310 and x = 5 then F = 0.23 Multiply F with 5. Product =5 x 0.23 =1.15. Output 1 and modify F = 0.15
iv.Repeat step iii. Until you get fractional part as zero.
v. Dequeue the queue completely if you have kept the output in a queue.
vi.Sometimes for some numbers we may not be able to get the fractional part=0, even after so many steps. In that case, we get a precision error. This is because, every fractional part cannot be expressed as a sum of 0.5, 0.25, 0.125 and so on.
Eg : Convert 0.687510 into binary system.
Sol : Since the integer part of the number is zero, the corresponding binary number is zero. Now, let’s convert the fractional part of the number(0.6875) into corresponding binary number.
Since the fractional part became zero in 4th step, we can halt the multiplication here. On counting integer parts in shown direction, we get corresponding binary number as 0.1011.
Hence, 0.687510=0.10112
Direction of Counting of integer parts
Eg: Convert 1404.25410 into hexadecimal system.
Sol: In hexadecimal (base 16) system, we have totally 16 symbols for representing numbers. Those are 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F .
For example,
Integer part and fractional part must be converted to hexadecimal system separately. So, first let’s convert 1404 into hexadecimal system.
Hence, 140410=57C16
Direction of Counting
Now, let’s convert fractional part (0.254) into hexadecimal system.
0.254 x 16 = 4.064 [integer part = 4]
0.064 x 16 = 1.024 [integer part = 1]
0.024 x 16 = 0.384 [integer part = 0]
0.384 x 16 = 6.144 [integer part = 6]
0.144 x 16 = 2.304 [integer part = 2]
0.304 x 16 = 4.864 [integer part = 4]
0.864 x 16 = D.824 [integer part = D]
0.824 x 16 = D.184 [integer part = D]
Since we are not achieving fractional part to be zero even after so many steps, we can leave the problem here. But note that
there will be some precision error.
Hence, 0.25410 = 0.410624DD16
Hence, 1404.25410 = 57C.410624DD16
Motivation for the Algorithm
Motivation for the Technique without point
Let given number N=akak-1ak-2…a1a0 in decimal system .Let the number be brbr-1br-2…b1b0 in base x system. Now our task is to find br,br-1,br-2,…b1,b0 . We know that , N= brxr + br-1xr-1 + br-2xr-2 + … + b1x1 + b0
Since every number of br,br-1,br-2,…b1,b0 are present in number system with base x, they are all less than x.
i. Divide N by x. Remainder is b0 and quotient is brxr-1 + br-1xr-2
+ br-2xr-3 + … + b1 . So, we found b0 (nothing but our remainder) .So, output the found remainder. Now let N= brxr-1 + br-1xr-2 + br-2xr-3 + … + b1 .
Clearly we can get all digits by consequent divisions
ii. Again divide N by x. Remainder is b1 and quotient is brxr-2
+ br-1xr-3 + br-2xr-4 + … + b1 . So, we found b1 (nothing but our remainder) .So, output the found remainder.
iii. Repeat above steps until we got quotient=0 (that is, until every value of br,br-1,br-2,…b1,b0 is found).
Motivation for the Technique with point.
Now let’s see for conversion of fractional part of decimal number to corresponding fractional part of base x system.
Let fractional part of given number N (base 10) , is F = .a1a2a3…ak .Let the corresponding fractional part in base x system be R = .b1b2b3…br .
Now, our task is to find R which is nothing but to obtain the values of b1, b2, b3,…br. The value of this R in decimal is given by b1x-1 + b2x-2 + b3x-3 +…+brx-r.
Hence, .a1a2a3…ak = b1x-1 + b2x-2 + b3x-3 +…+brx-r
i. Multiplying by x on both sides yields x(.a1a2a3…ak )= b1 + b2x-1 + b3x-2 +…+brx-r+1
Since, every one of b1, b2, b3,… br are less than x , Fractional part of the product = b2x-1 + b3x-2 +…+brx-r+1
Integer part of the product = b1.
Hence b1 is found and output the value of b1 (nothing but integer part of the product)
ii. Now fractional part is b2x-1 + b3x-2 +…+brx-r+1 . Let this be F . Multiply F with x which results in
Fractional part of the product = b3x-1 +…+brx-r+2 Integer part of the product = b2. Hence b2 is found. Output b2 (which is nothing but integer part of the product)
iii. Repeat the step i. and ii. Until we get all values of b1, b2, b3,…br. Sometimes value of r may be large and we may not be able to find all the values of b1, b2, b3,…br .In that case , we get precision error.
Proof for the Algorithm (Decimal to Binary)
For Conversion of Integral Part
Let us prove by Induction that our claim is right for all numbers greater than 2.
Base case- Conversion of (0)10=(0)2
(1)10=(1)2
(2)10=(10)2 which are trivially right
Inductive Step-To prove that binary conversion of number n is right assuming binary conversion of all numbers less than n is right.
Now from the 1st step in the algorithm we take remainder of n divided by 2 and then we push the answer in the stack and continue the process with number = n/2. Using Mathematical Principle of Strong Induction the Binary Conversion of n/2 from the algorithm is right.
If n is even-Now since if n is even then this algorithm writes the final answer as (an an-1an-2…a1) a0 (a0 =0). 20a0+21a1+22a2…….2nan= 0+2*(20a1+21a2+22a3……2n-1an) = 0+2*(n/2)
If n is even this algorithm gives the right result.
If n is odd-If n is odd then the algorithm will write the final answer (an an-1an-2…a1)a0 (a1=1). 20a0+21a1+22a2…….2nan = 1+2*(20a1+21a2+22a3……2n-1an) = 1+2*(n-1)/2= n (since n is odd n/2 will evaluate to (n-1)/2 by the algorithm)If n is odd this algorithm gives the right result.
QED.
Proof of Conversion of Fractional Part-
Fact 1-The algorithm tries to find a binary representation that is the closest to the fractional part and at the same time is less than equal to the number. In other words the number outputted by the algorithm will not in any scenario be greater than the number whose conversion we are trying to find
We want to prove that the kth digit after decimal printed by the algorithm is correct.
Proof by Induction
Here O refers to the original fractional number. And Fk refers to the fractional number used by the algorithm in the k step. For example In Binary conversion of 0.54 O=0.54And the next number that is used is 2*0.54-1=0.08=F2 since it used in the 2nd step.
Base case-
1st digit after decimal is correct and the F2= 2*O - a-1.
O=a-1/2 +a-2/4 + a-3/8 …….a-2/4 + a-3/8….<= 1/4+1/8+1/16…….=1/2 (Follows from the sum of an infinite GP)
if O>=1/2 the a-1 will be 1 ,else 0. O*2>=1 then a-1 is 1 else 0. (1) (This is what the algorithm does)
If a-1 is 1 then clearly 2*O>=1 and F2=2*O-1 (assignment) Else F2=2*O (assignment)
Hence F2 is 2*O - a-1 where we have calculated a-1 correctly.
Inductive Step –
Using induction we know that the algorithm at k-1 step usesFk-1 =2k-1O - 2k-2a-2 - 2k-3a-3 ……….-a-k+1 (2)Using Original EquationO=a-1/2 +a-2/4 + a-3/8…. + a-k+1/2k-1 + a-k/2k + a-k-1/2k+1…………
Doing some manipulations we can reduce it to2k-1O - 2k-2a-2 - 2k-3a-3 ……….-a-k+1 =a-k/2 +a-k-1/4Using (2) we getFk-1=a-k/2 +a-k-1/4…… This is equivalent to the base case and we can use the same logic to prove that the algorithm computes the k digit after decimal correctly and also that Fk =2*Fk-1 - a-k. Fk = 2kO - 2k-1a-2 - 2k-4a-3 ……….-2*a-k+1-a-k
This proves the inductive step
QED
C++ code for the Algorithm to convert decimal to N th number system
#include<iostream>
#include<stdlib.h>
using namespace std;
class stack //Declaring stack class
{
private:
int top,num;
int *a;
public:
stack(int x=10) //to set the default value of size as 10
{
a=new int[x];
top=-1;
num=x;
}
void push(int data)
{
if(top<=num-1)
{
a[top++]=data;
}
else if(top==num-1)
{
num=num+5;
a=(int *)realloc(a,num*4);
a[top++]=data;
}
}
int pop()
{
if(top==-1)
cout<<"stack is empty"<<endl;
else
return a[--top];
}
int gettotal()
{
return (top+1);
}
};
int main()
{
int n,x,i;
float k,data,f;
stack a;
cout<<"enter a number : ";
cin>>data;
cout<<endl<<"Enter the destination number system:";
cin>>x;
n=(int)data; //storing integer part of data in n
f=data-n; //storing fractional part of data in f
while(n!=0)
{
a.push(n%x); //pushing the remainder
n=n/x; //updating the value of n
}
cout<<endl<<data<<" in base "<<x<<" system is : ";
int num=a.gettotal();
for(i=0;i<num;i++) //popping all bits
{
cout<<a.pop();
}
cout<<"."; //for giving a decimal point7
for(i=0;i<20;i++) //to get a precision upto 20 digits
{
k=f*x;
cout<<(int)k;
f=k-(int)k;
if(f==0)
break;
}
}
ALGORITHM TO CONVERT BINARY TO DECIMAL
To convert a binary number into a decimal number, we multiply each bit to it corresponding weight. The weights are in the sequence of 20, 21, 23,…, 2n,… So the decimal representation is nothing but the sum of the product of each bit with its corresponding weight.
(anan-1an-2...a0.a-1a-2a-3...) 2 = ∑0
n
ak 2k + ∑1
∝
a-k 2 -k
Eg-
(101.11)2 = 1*22+0*21+1*20+1*2-1+1*2-2
= (5.75)10
Eg- Convert 1010110102 from binary to decimal.
2 + 8 + 16 + 64 + 256 = 34610
Other base system to Decimal System
Step 1 - Determine the column (positional) value of each digit (this depends on the position of the digit and the
base of the number system). Step 2 - Multiply the obtained column values (in Step 1) by
the digits in the corresponding columns. Step 3 - Sum the products calculated in Step 2. The total is
the equivalent value in decimal.
Eg:
Step Octal Number Decimal Number
Step 1 258 ((2 x 81) + (5 x 80))10
Step 2 258 (16 + 5 )10
Step 3 258 2110
Octal Number: 258 = Decimal Number: 2110
References
https://en.wikipedia.org/wiki/Numeral_system
http://www.math.wichita.edu/history/topics/num-
sys.html
http://betterexplained.com/articles/numbers-and-
bases/
http://www.tutorialspoint.com/
computer_fundamentals/
computer_number_conversion.htm