I am trying to make a base 10 to binary converter that doesn't take up an extremely large amount of RAM. I have continuously been failing, and wondered if anyone had any ideas.
I have actually done this!
What I did was this: you use repeat loops to start doubling the value of a variable (initially set to one) so that it stops doubling the value once it is greater than the initial input number. Once the variable is greater than the initial input, it divides that variable by two and stores it to a list. It then resets the variable that was being doubled back to one and does this again (via the repeat loop). It will stop doing this once it gets to the exact number. Then it takes the list and eherever ther is a value greater than 0, it puts a one in there. I don't think I have to explain how to show the ouptut (correct me if I am wrong). Here is a pseudo code
Repeat Sum(list1) = Variable1
1 -> Variable2
Repeat until Variable2 > Variable1
Variable2 * 2 -> Variable2
Variable2 -> list1(dimensionlist1 + 1)
Sorry about the pseudocode as opposed to TI-BASIC
I have my Base 10 to binary converted grouped and don't remember all the little mechanics of it, so I basically gave you a wrough sketch of the code since my explanation seemed a bit inadequate.
Here is a way to convert a number in N (base 10) to base B (2 to 36):
Input "NUMBER:",N Input "BASE:",B "0→Str1 While N sub("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",1+round(BfPart(N/B),0),1)+Str1→Str1 iPart(N/B→N End Str1 If Ans≠"0 sub(Ans,1,length(Ans)-1 Disp Ans
For specifically binary:
Input "NUMBER:",N "0→Str1 While N sub("01",1+BfPart(.5N),1)+Str1→Str1 iPart(.5N→N End Str1 If Ans≠"0 sub(Ans,1,length(Ans)-1 Disp Ans
47%? Take a look and try to imagine how cool 100% will be. This has won zContest 2011 and made news on TICalc. This compromise between Assembly and BASIC parses like BASIC and is fast like assembly. Grammer 2
Oh. That's much better than mine. I have a really simple binary to base 10 converted sort of like this, but my base 10 to binary is no where near as good as this. Time to scrap that clunker!
Is there a code for decimals? The only thing I've thought of is:
shorten Str1 so it includes the stuff before the decimal, and store the rest into C
Go though that process
take C and find C*BASE, convert that number to Str1, go through that process with it again, then output the original answer, a ".", and the new answer.
It's rather long, and it doesn't work for repeating decimals, is there any other way?
I didn't realize that decimals were ever expressed in Binary.
Well, from what I understand of decimals in other bases (even though dec means base 10), you take the number behind the decimal place and divide by the base to the power of the number of digits behind the decimal.
0.171310 is 171310/(10^4) = 1713/10000, which is 0.713
This can work for other bases:
0.10112 is 10112/(2^4)=11/16
Now the tricky part is repeating decimals.
(1/3)10 doesn't work because it can't be expressed as X/(10^Y).
So you express it in the ratio X/((10^Y)-1), which is 9, 99, 999, etc. in base 10.
The number of digits in the denominator it the length of the repeating part.
1/3 = 3/9, and 9 is 1 digit long, so it is 0.333333
But in 1/99, 99 is 2 digits long, so it is .01010101 repeating
This works in all other bases.
That's 1/101, and it can't be expressed as X/(2^Y), so it is repeating.
(2^X)-1 is 1, 11, 111, 1111 etc.
(1/101)2=(11/1111)2 Since 1111 (the denominator) has 4 digits, it it is 4 long.
That's a lot of digits, so it's pretty accurate. Let's plug it in:
It's 24 digits long, so 2^24=16777216
3355443/4194304=0.1999999881, which is close to .2
That works for any base.
I'm pretty sure that's how decimals in bases work, but don't quote me on it.
At first I completely didn't understand, but now I kind of get it. I don't understand the repeating decimals at all though.
Take 5/9,. It's decimal value is 0.55555
From x = 0.5555555555
We get 2 equations
10x = 5.555555555
x = 0.5555555555
10x - x = 5.555555555 − 0.555555555555
9x = 5
Divide both sides by 9
x = 5/9
Notice how you take the base^1 and subtract 1 to make it simple.
It works for 23/99=0.2323232323
This same logic will work in all bases.
It's strange, though. There are math sites that explain how all this math works in base 10. But when you try to find math tricks for other bases, all there is is 'how to convert bases' There are no cool tricks listed for different bases, like BASE^X-1 is always the highest value the base allows repeated X times.
I think bases are really interesting; there should be tricks like these for them.
Mine returns ".259" when converting 4 to binary using the universal converter.