Mathematics and Physics for Programmers
What is a number’s Base?
So numbers can be represented in different ways , for instance the number 5 can be represented geometrically as a Pentagon , you could also represent it as a set of 5 dots. You can represent it physically as 5 beads on an abacus. And another approach is to use the symbol “5”, which is defined to mean what you want it to mean. Each representation has its own advantage. eg an abacus has an advantage as you can simply count beads back and forth instead of sides on a polygon. However when you get to larger numbers involving many beads using symbols has an advantage.
As it is it is not possible to have a unique number for each number , that would be terribly impractical. So we use the base system to do so where you have a number b which is the base. Here is my long program in C++ defining this.
So within this function recursion is used, as we have a tree accumulation process within the function, as each remainder is of the number and base is recorded in the string. The base is like a defined sized bucket, we measure how much the number overflows from the bucket.
This is burying lower and lower into exponents of the base. Each time it circles in the function represents a new exponent for the base hence the modulus is used. Once we have this reduced number which is the original number minus the remainder we then lower it by dividing it by base^1. 354 in base 10 operates like so
- The remainder when 354 is divided by 10 is 4
- 4 is written into the output
- Then its minused from 354 → 350
- 350 is then shrunk by 10 → 35
- 35 remainder of 10 is 5 and is written in front of output
- 5 is taken away from 35 → 30
- 30 is then shrunk by 10 → 3
- 3 is less than 10 so it writes that in the output finally bouncing out and returning 354.
Here is the psuedocode for what we have just performed with some good ol comments, defining what is going on.
To perform this in reverse you do this here, I used the cmath library to conduct exponents, I was pretty happy to see that int already directly converts chars into their codes so no need for .chr method in ruby, all I had to do was take away 48 to find the integer that the character code denotes. Additionally I kept an integer variable (n) outside of the recursive program, because I wanted to keep track of how many levels deep it was going into the number or in other words how many exponents of the base is it going into.
This is the psuedocode for the baseStringToValue to apply in your own language.
I put less comments here because I just explained the reverse of this process before hand and it should be self-explanatory what is going on.
When you write a number in a base you assign a slot in each digit (in slot number n)corresponding with the base to the nth power,
so for instance the number 14, assigns slot number 0 ( 4) and slot number 1 (1) , 10 to the power of zero to the number 4 and 10 to the power of one to the number 1 so 10⁰ x 4 = 4 , 10¹ x 1 = 10 , 10+4 = 14
A larger number 1428 can also be analyzed with these slots
10⁰ x 8 = 8 , 10¹ x 2 = 20 , 10² x 4 = 400 , 10³ x 1 = 1000,
1000 + 400 + 20 + 8 = 1428
My next post is to explore is particular bases used by computers Binary, Decimal and Hexadecimal which are other ways of saying Base 2, Base 10 and Base 16 respectively.