Advertisement

Base 10 to base 5 counting systems

Started by October 11, 2002 01:45 PM
7 comments, last by black_mage_s 22 years, 4 months ago
I looked all over the place to try and find a formula that would be able to convert between the 2, but all I could find were programs that didn''t give me the formula. What is teh formula?
"Luck is for people without skill."- Robert (I Want My Island)"Real men eat food that felt pain before it died."- Me
If I understand correctly you want to go from a decimal system to a quintet(?) system.

To exchange between numbersystems, you''ll have to understand how they work first. Let''s take the decimal system. 15 could be written as 5*10^0 + 1*10^1. Thats the whole idea behind those systems

To go from a five base system, stuff could be done like

124

4*5^0+2*5^1+1*5^2

Eitherway, I''m pretty tired so I could be wrong or even answering the wrong question

Hope it helps

-- Divide
Advertisement
from base10 -> base5, we take an example,let say 120:

120:5 = 24
0

24:5 = 4
4

4:5 = 0
4

then we take the rest from bottom to top,and we have 440,then 120 in base5 is 440.

from base5 -> base10, we just have to do:
4*5^2 + 4*5^1 + 0*5^0 = 120

i.e. we multiply each number by the corresponding power of 5.
Like you can see there is no formula,it's an algorithm.

HPolloni

PS: Sorry for my lame english.



[edited by - hpolloni on October 11, 2002 3:32:00 PM]
quote:
Original post by black_mage_s
I looked all over the place to try and find a formula that would be able to convert between the 2, but all I could find were programs that didn''t give me the formula. What is teh formula?


So, are you developing a number conversion game?

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
Nope, I just want to put a number conversion system into my game. For the time being, I''ve been converting between base 5,8 and 10 by hand, so numbers over 100 can get REAAAAAAAAAALY tedious
"Luck is for people without skill."- Robert (I Want My Island)"Real men eat food that felt pain before it died."- Me
You''re best off converting base b1 to base 10, and then base 10 to base b2, because all the math and operators are and assume base 10, so that''s your "common ground" for conversion. And converting anything to and from base 10 is quite simple
Advertisement
quote:

You''re best off converting base b1 to base 10, and then base 10 to base b2, because all the math and operators are and assume base 10, so that''s your "common ground" for conversion. And converting anything to and from base 10 is quite simple



How and which operators are or assume base 10?
i don''t understand your problem ... in the computer, the number is not really in any particular base (obviously it''s base 2 bitwise, but not when thinking of it as int or short or whatever ... adding an int which is 10 in decimal to an int which is 5 in deimal, yeilds an int equal to 15 in decimal ... but the internal number is the same no matter what input and output base you want to use ...)

the only time a base is considered is when reading or writing strings, or desiring to deal in arrays of digits ... and so at that time, you convert from or to base 5 just like base 10 ...

the general method to generate an array of digits in any base is to take your source number, take the remainder of dividing the number by the base, and put that as the right digit of the array, then divide the number by the base and feed that back into this algorithm recursively, such as:


  // only works for positive numbers and type which// overload the / and % operatorstemplate <typename NumericType, typename ContainerType>void BuildDigitArray(NumericType numeral, ContainerType &array, unsigned base)  {  NumericType digit;  do    {    digit = numeral % base;    array.push_front(digit);    numeral /= base;    }  while(numeral != 0)  }  


you could add support for specifying a DigitType different than the NumericType, and then the line:

digit = numeral % base;

would become

digit = static_cast(numeral % base);
When you perform operations on the computer, is you are entering them in base 10. (10 / 5), (var1 * var2), (a + b). Those numbers don''t represent base 8, base 16, base 7, base 19, base 84... they''re base 10 by default (of course, you can make it use base 16 by appending an ''h'', or base 8 by prepending a ''0''). But in general, it assumes that all the numbers you "pass" to it are in base 10. Even though internally it''s all base 2, the interface is normally base 10.

This topic is closed to new replies.

Advertisement