Reading The Contents Of User Input, And Calculator Improvement

reading the contents of user input, and calculator improvement

This post will cover how to actually determine WHAT the user has typed, instead of just how long it is. It will also include how to interpret what the user enters as a binary number, so that its easier to type.

An Essential part of making it interpret binary numbers is making it double numbers repeatedly.

This actually has a few ways that can be done, so this is one of the first situations where coding style for this problem might differ from person to person. Because of this, I will say more than one way to do it.

The first way to do this it to copy the number twice, and then start from zero and add both of the copies. This is relatively inefficient, and would take

a copy thing, consisting of two bifurcates (which would take a little time)

where the size of the initial number is N, 2N normal bifurcates, 2N reverse bifurcates, and 4N lines relating to the actual loop

assuming each command takes the same amount of time (which is an oversimplification) this would take 9N+C line times. (C is a constant) This might be acceptable, but there is a more efficient and nicer looking way.

The second way is nicer looking, but still not the most effecient. However, when multiplying by a larger number(such as 3, or 4, or even large numbers), this method is part of what would be used.

The second method is essentially copying the number (using a reverse bifurcate and a normal bifurcate), and then adding the number to zero, except instead of each loop increasing the new number by one, it increases it by two. This is shorter, and it looks nicer. It also only takes half as many normal bifurcates. As a result, the number of steps it would take (again assuming each step is the same length) is 8N+C, instead of 9N+C 

this one I will write out, but it is still not the best way:

//N is the number initially BIFURCATE [NULL,NULL]2NULL; BIFURCATE [N,N]G; BIFURCATE G[NCOPY,JUNK]; BIFURCATE 2NULL[RESULT,JUNK]; ~ATH(NCOPY){ BIFURCATE NCOPY[JUNK,NCOPY]; BIFURCATE [BLAH,RESULT]RESULT; BIFURCATE [BLAH,RESULT]RESULT; } 

ok, so yeah. that takes N, and puts twice N into RESULT, but it is still inefficient.

A more efficient version is to copy the initial number, and add the number to itself. This way you only have to do half the number of reverse BIFURCATE statements. This is much more efficient, taking instead the steps:

a copy thing, consisting of two bifurcates (which would take a little time)

where the size of the initial number is N, N normal bifurcates, N reverse bifurcates, and 2N lines relating to the actual loop

This has 7N+C steps, which is a significant improvement. I think it is the fastest way to double a number in drocta ~ATH.

It is as follows (N is the number)

BIFURCATE [N,N]G; BIFURCATE G[NCOPY,RESULT]; ~ATH(NCOPY){ BIFURCATE NCOPY[JUNK,NCOPY]; BIFURCATE [BLAH,RESULT]RESULT; } 

This is shortest and fastest solution I have found. If you find a shorter or faster method, please tell me.

Ok. Now we can double numbers. That is good. That is an important step. But we still haven't gotten user input to be read in any reasonable way.

Hang on, I'm GETTING TO THAT. GEEZ. (I'm kidding, no one has been complaining about my taking so long, other than myself)

Ok, so here goes:

To interpret the binary number input and convert it to a "number", we can follow the following algorithm:

start with zero.(this is before the loop)

If there are any characters left, double the number that is being created.

remove the first character from the remaining characters. If it is "1" or whatever symbol (or alternatively if it is not "0"), add one to the number that is being created. Otherwise, continue onto step 4 without doing anything first.

go back to the start of the loop (step 2)

Ok. thats the algorithm we are going to use. But I STILL haven't explained how to recognize what the next character is. Seriously what is up with that?

What you do is you bifurcate the rest of the input into [the next character,the rest of the input].

Now you have the next character. Then what you do is you reverse bifurcate it with some other object, and then you check whether that object is already dead or not.

But how do you make it so the combination is already dead? How do you get the object for the character before the user has even inputed it?

Answer: You don't. Not in the current version of drocta ~ATH anyway. You will have to tell the user to enter all the characters they will be using ahead of time. Yes this is horrible and stupid. No its not exactly like that in the comic. Its ~ATH what do you expect? :P

that might change in future versions, but I will try to stay backwards compatible with that.

but anyway, back to comparing it:

so you say something along the lines of:

import comparingobject CMP1; othercodehere makeNEQ1besomethingalive BIFURCATE [CMP1,CHAR]EQ1; BIFURCATE [NULL,NULL]2NULL; ~ATH(EQ1){ print yep, they are equal; BIFURCATE 2NULL[EQ1,NEQ1]; } ~ATH(NEQ1){ print nope, they are not equal; BIFURCATE 2NULL[NEQ1,JUNK]; }  

in the othercodehere you get the character a head of time, and say BIFURCATE[CMP1,THECHARTHATMATCHESWITHCMP1]D; D.DIE();

That makes it so that it will go through the one section of code if the character is the right one, but something else if it is something else.

Which is what we want.

So to put it all together, and make the thing that interprets the input as a binary number(hold on tight(ok, what, why did I say that), this will be a long one(why am I talking like this?)):

import blah BLAH; print please enter whatever character you will be using for binary zero.; INPUT ZEROCHAR; BIFURCATE ZEROCHAR[ZEROCHAR,JUNK]; import chrcmp CMP0; BIFURCATE [CMP0,ZEROCHAR]D; D.DIE(); print please enter whatever character you will be using for binary one.; INPUT ONECHAR; BIFURCATE ONECHAR[ONECHAR,JUNK]; import chrcmp CMP1; BIFURCATE [CMP1,ONECHAR]D; D.DIE(); BIFURCATE [NULL,NULL]2NULL; BIFURCATE 2NULL[OUTNUM,JUNK]; print please input the binary number you want.(it will be converted to unary); INPUT BINNUM; ~ATH(BINNUM){ BIFURCATE [OUTNUM,OUTNUM]G; BIFURCATE G[NCOPY,OUTNUM]; ~ATH(NCOPY){ BIFURCATE NCOPY[JUNK,NCOPY]; BIFURCATE [BLAH,OUTNUM]OUTNUM; }  BIFURCATE BINNUM[CHAR,BINNUM]; BIFURCATE [CMP0,CHAR]NEQ0; ~ATH(NEQ0){ BIFURCATE [BLAH,OUTNUM]OUTNUM; BIFURCATE 2NULL[NEQ0,JUNK]; } } print ok, going to print it out in unary, with each digit on one line. If the number you entered was large you might want to close the program instead of hitting enter.; INPUT JUNK; BIFURCATE [OUTNUM,OUTNUM]GOUTNUM; BIFURCATE GOUTNUM[OUTNUMCOPY,JUNK]; ~ATH(OUTNUMCOPY){ BIFURCATE OUTNUMCOPY[JUNK,OUTNUMCOPY]; print 1; } print Am I a terrible person for writing this?; 

Oh gosh. I wish I could indent in tumblr. that is terrible to read. tumblr is a terrible source code editor.

One time someone called me a masochaist for writing this type of stuff.

And then we just have to put that together with the adding thing, and then maybe add a better way of outputting the number. maybe in binary.

HAHAHAHAH

ok, yeah, I'm going to put it together in the next post, not this one, because I have to homework now.(using the noun homework as a verb was intentional)

yeah. putting it together in the next post.

As always, if something was confusing, please ask for clarification.

More Posts from Learn-tilde-ath and Others

8 years ago

How exactly would one go about downloading and running your ~ATH interpretation? Where can we download it and what is needed to run it?

“Hi! My interpreter is on my github account [here], along with a number of example files.

To run it, you need to have python 2.7 installed. (Other versions of python 2 may also work. I don’t think it quite works in python 3, but running it through a python 2 to python3 converter should probably give you a working python 3 version)

You can get python 2 at https://www.python.org/downloads/ .

The current version of python 2 is 2.7.13 . Currently that python download page has a link directly to the download on it. (If anyone is reading this post much later than January 1 2016 and the page has changed in the meantime, feel free to send me an ask asking where the current download link for python 2 is).

Once you have python 2 installed, download the interpreter from my github, https://github.com/drocta/TILDE-ATH . There should be a green button labeled “Clone or download”. If you click this, one of the options it will give will be “download ZIP” . Choose this option to download a .zip file of all the files for drocta ~ATH.

Unzip these files into a folder somewhere. Put the .~ATH files that you want to run in the same folder. Use python2 to run the python file interp_2.py . The program will then expect you to type in the filename of the .~ATH file you want to run and then press enter once. It will then load and run the ~ATH program. When it has finished running the program, it will say "press enter to close" . (this is so you can see the output of your ~ATH program before it closes, in case you run it in certain ways.). When you press enter, the python program will finish.

More details on how to run interp_2.py, in case you don’t know how to:(I am assuming you are on windows. if this is not the case, please send me another ask. It should work fine on other OSs, but these instructions for how to run interp_2.py with python might be slightly different)once python 2 is installed, if you open the command line, navigate to the folder with interp_2.py, and type “python interp_2.py” (without spaces), it should run it. Alternatively you can double click on the interp_2.py, it will probably work.One potential problem you might have is if you have python3 installed as well as python2. In this case you have to make sure you run it with the right version of python. If you are using the command line,“C:\Python27\python.exe interp_2.py” instead of “python interp_2.py” should probably work.

If any of this is unclear or you want any more help with this, please do not hesitate to send another ask.

I’m very pleased that this is still interesting for people, and am happy to help!

11 years ago

I made a parser

I made a parser for ~ATH in addition to the interpreter. I might make the interpreter use the parser at some point in the future, or I might not. If I made the interpreter use the parser, the code for the interpreter would probably be a little cleaner, and possibly a little faster.

The parser is available from my github.

To use it, call tokenize on the text, and then read_all_from on the result of tokenize.

The output will be a list of lists

https://github.com/drocta/TILDE-ATH-Parser

(haven't updated lately because of other unrelated projects, and also other reasons that aren't necessary to describe)


Tags
7 years ago

I am aware that this will probably not get answered but for what its worth: Is it possible to access the 'variables' generated within the ~ATH code externally? I.e. if I import an alive object would it be possible to use the python code to test if that object is alive or not (and potentially change that)? I have taken a look at the code but am unsure if it is possible or even coded in that manner. Any response is appreciated but I will not mind and understand if this goes unanswered.

Hey,Assuming I understand what you mean by your question:Yes it would be possible to do that with not too much of a change to the code, but no the code wasn’t designed specifically to facilitate that.The way the code is written, the python function “evalScript” takes two arguments, the first of which is the text of the drocta ~ATH code to run, and the second is a drocta ~ATH object to give as input to the script.In this function, there is a python variable called ATHVars which stores a dictionary of variable names as the keys, and the values being the ~ATH object that is currently pointed to by the variable. If you want to have python do something with a ~ATH object that a particular ~ATH variable points to, you could use this dictionary in order to get that object.At the end of the “evalScript” function, it returns the ~ATH object stored in the return_obj variable. This can be set by sayinganyothervariable.DIE(theObjectYouWantToReturn);So, one way that might work to do what you are wanting to do, is in the ~ATH script you want to run, you would first take all the objects you want to check if they are alive at the end of the script, use BIFURCATE or SPLIT .... ah phooey I never actually put split in this version I guess? ok repeated use of BIFURCATE it is then I guess.Anyway, you would use BIFURCATE to get an object which is the combination of all the objects you are interested in, in whatever way you put them together, and then you make it so that that object is the one you return at the end.If you have one of the ~ATH objects in python, the way to check if it is alive or not is to check the theobjectinquestion.living attribute (see bif.py for this part if you want).

You said “if I import an alive object“, which I’m not sure if might suggest that I haven’t explained how the “import” statement works in this clearly enough.

“import” is a bit of a misnomer here. For the most part, “import” is more of a way to declare new objects, not to import existing objects from a libraryIf I set aside a bunch of time to work on this more I would maybe add support for them actually being libraries?---Oh! If you want the python code to inspect (and possibly change) whether the object that a variable currently points to is alive or not while in the middle of the ~ATH script , a good way to do that would probably be to modify the part which it handles calling functions, and make it support calling functions which are written in python and included in a dictionary under some name, similar to how it uses the funCodes dictionary.

However, that might be kind of tricky to do if you uh, weren’t the one to write the code, because the code I wrote is rather messy? I started cleaning it up at one point by starting to write a parser instead of using the terrible regex that it currently uses, but I never finished writing the parser and integrating it into the interpreter. Bler...

I would like to do that at some point. Hmm...

Maybe I should schedule some time to do that? I have had more experience writing parsers and interpreters since then, so it shouldn’t be /that/ hard for me to clean some of this stuff up and add support for functions written in python.

I probably shouldn’t try working on that like, right now, because I have schoolwork I should be getting to, but, hm.

Hey, if you can, could you send me another ask in a few days to remind me to maybe schedule a day on which to write some stuff for this?

Anyway, thanks for asking. I’m happy to answer questions about this.

12 years ago

Syntax explanation

~ATH is a programming language. A programming language is a language that can be used to write programs in, that can afterwards be run. ~ATH appears in Homestuck, and drocta ~ATH is my attempt at making it a real thing.

drocta ~ATH is limited to what is physically possible of course, because it actually exists. It is also limited to fit well with the comic, and how much of it I have created so far.

As such, so far all the programs that can be written in drocta ~ATH are text only, do not yet accept input (input in progress), and tend to be stupidly long.

It is however capable of computing anything a computer can given enough time and memory.

~ATH is based on objects, and the lives thereof. 

For example, in homestuck, there are programs that relate things to the lifespan of a universe, or a person, etc.

Of course, in order to make an interpreter possible, we have to limit ourselves to virtual objects. Ones that do not actually exist.

References to these objects are stored in things called variables. You probably know what these are already, but if you don't, think of a variable as a box that can have a thing in it. Each box has a name. You can say "do something with whatever is in the box called "apple".

In ~ATH, every object is either "alive" or "dead".  Note that it is the object that is alive or dead, not the variable that refers to the object.

Now onto the actual syntax!:

Currently(as of build 7), drocta ~ATH has 5 different commands: import, ~ATH(){}, .DIE();, print, and BIFURCATE.

First we will go over the import statement. The import statement has the purpose of creating a new variable and a new object. Unlike other operations that can create a new object, the object created by import is initially unrelated to all of the other objects.

The syntax of the import command is:

import anything other than semicolons here VARIABLENAME;

The things between import and the last space before the semicolon are ignored.  

In future versions, the import statement might also do additional things, such as using things from other files. But for now it just initializes variables.

EXAMPLES:

If you wanted to make a new variable called BANANA, and you wanted the reader to know that BANANA is a fruit, you would say:

import FRUIT BANANA;

or you could say

import YELLOW FRUIT BANANA;

The things that go before the variable name don't actually matter, you can say

import ghsdgh hgsdkhg hgksdhg hgskdjg BANANA;

if you wanted.

Now for the next command: The eponymous ~ATH loop!

The syntax of it is as follows:

~ATH(VARNAME){ Some other code goes here }

What this does, is each time the code execution gets to the ~, it checks what the variable in the parentheses is, and then checks the object the variable points to. If the object is alive, it continues. If the object is dead, it skips to after the }. When the Code execution reaches the }, (that is, if it did not skip to after it), it will jump back to the corresponding ~. Code execution will keep going around in this loop until the object pointed to by the variable is not alive. In later versions, there may be an additional requirement that the } be followed by EXECUTE(code here); Where code here can be replaced with ~ATH code, NULL, or possibly a file name. But currently, this is not the case. Now onto the command BIFURCATE!: Bufurcate actually has 2 forms, the standard bifurcate, And the reverse bifurcate. The first of the two has syntax as follows:

BIFURCATE VARNAME1[VARNAME2,VARNAME3];

What this does, is it takes the object pointed to by the first variable, and determines two objects, which are stored in the other two variables. The two objects determined will always be the same for a particular object.  If you for example say:

BIFURCATE V1[V2,V3]; V2.DIE(); BIFURCATE V1[V4,V5]; 

then V4 and V2 will point to the same object, which will be dead.

The other form of the BIFURCATE command has syntax as follows:

BIFURCATE [VAR1,VAR2]VAR3;

This is pretty much the inverse operation. That is, it undoes the other one. If you say for example:

BIFURCATE A[B,C]; BIFURCATE [B,C]D; 

then A and D will point to the same object. To be clear, undoing the other is not the only time you can use it. It will take any two objects, and determine an object from those two. If there is already an object for the combination of those two, then that object is the resultant object. If none has been created yet (and the two arent split from something in that order), it will create a new object, which it will put in VAR3.

To be clear, if you say:

BIFURCATE A[B,C]; BIFURCATE [C,B]D; 

 A and D will NOT point to the same object, unless you create A such that B and C are the same. That is, if you say:

BIFURCATE [Y,Y]A; BIFURCATE A[B,C]; BIFURCATE [C,B]D; 

 A and D WILL point to the same object, and B and C will both point to the same object as Y (and as each other).

The Final command is print.

print is pretty simple, but its syntax might change somewhat.

Currently the syntax is:

print the text you want to output;

Note the semicolon. the semicolon is important.

So hello world would be

print Hello, World.;

SYNTAX EXPLANATION COMPLETE! PLEASE SUGGEST CLARIFICATIONS IF NECESSARY.

MESSAGE END.


Tags
11 years ago

re: "computationalalchemist answered: You could use Prolog-style lists for split."

"computationalalchemist answered: You could use Prolog-style lists for split."

I'm not totally sure what you mean, but my best guess as to what you meant is probably pretty similar to how I making it.

I implemented it and I think it works, I need to double check though.

With what I have now when I use 

split [THIS,THIS,THIS,THIS,THIS,THIS,NULL]COUNTER; ~ATH(COUNTER){ print "bananas"; BIFURCATE COUNTER[JUNK,COUNTER]; }EXECUTE(print "ok";); print "whee!";

it yields 

bananas bananas bananas bananas bananas bananas ok whee!

Which seems to make sense to me, and also, fits with how lisp lists work, and apparently also prolog lists.

also where it says split it will also accept bifurcate. they are actually treated as the same command.

import statements aren't fully implemented yet though.

I think I will put this version on github pretty soon.

Thank you for the advice.


Tags
12 years ago

Heyho, I now red alot of your sources and i started to understand your interpretation of ~ATH. You do alot of things with the bifurcate command.. I actually thought that bifrucate JUST splits the programm in different colors like here : "bifurcate THIS[THIS(red), THIS(blue)]; " I think that you can actually split your script into N colors while N > 0. like this: bifrucate THIS[THIS(COLOR(0),THIS(COLOR(1),THIS(COLOR(2),THIS(COLOR(3),THIS(COLOR(4),THIS(COLOR(5),THIS(COLOR(6)] and so on.

Yes. I do use BIFURCATE often.

Personally, I doubt that one could say

BIFURCATE A[D,B,C]; (for some names and colors for a b c and d),

because of the "Bi" part of the word "bifurcate", meaning two. (or in some cases, "both")

Further, I would defend my use of it to BIFURCATE things other than  THIS with http://mspaintadventures.com/?viewmap=6 which, for the start of act 5, says

BIFURCATE ACT_5[red(ACT_1), blue(ACT_2)];

which seems to indicate that things other than THIS can be BIFURCATED.

To split something into more than 2 parts, I would just bifurcate it multiple times. *

I don't know what the etiquette is for whether I should click publish or answer privately, so I am going to pick one and hope for the best.

5 years ago

How do you use this?

Just to be super clear, though you probably understand this, “drocta ~ath” is not for practical purpose. It is purely an amusement.

That being said, to run this, you need to have python 2 installed. (Yes, currently most new python projects are in python 3. Unfortunately I haven’t gotten around to making any of the updates I’ve wanted to on this project, and it has been years since I’ve worked on it.)

To run it: download the github repository from https://github.com/drocta/TILDE-ATH

then, (probably from the command line, though running it in other ways may also work) navigate into the folder where you put all those files, use python 2 in order to run interp_2.py  . [1] Then, it will allow you to type something in. It would be good if it gave some sort of prompt saying that it is accepting input, but it currently does not. What you have to type in is the file name of the drocta ~ath program that you want to run.

for example, you might type:

python interp_2.py looptest.~ATH

in order to run the program looptest.~ATH , and then you would see the output:that alternates between “APPLE” and “ORANGE” a number of times (like, 5 times I think).

If it isn’t working for you, let me know and I can try and help you troubleshoot what’s going on.

If you are asking, not “how do I run the programs in this language” but “how do I write programs in this language”, uh, read through the rest of this blog I guess. It isn’t complete, but the point of this blog was meant to be a tutorial for how the language works. If you have any particular questions about how to do a particular thing in the language, then ask that. But I don’t currently have time to re-do the whole project of this blog and put a tutorial for the language as a whole in one response to an ask.

P.S. I am currently in grad school for math (I made this language while in high school). I haven’t been doing all that much programming lately unfortunately.

([1] What’s that? “interp_2.py” is a weird name for the main file? Indeed it is. Originally I had “interp.py” and then before I started using git I made a new version which I called interp_2.py, and then, for basically no good reason, I kept that name for the file. If I go back to this at all, I suspect that I will change that to just “interp.py” or maybe “main.py” or something. idk.)

11 years ago

Hello again all!

So recently I started kind of working on this again for a bit. I have fixed some bugs with the parser that I haven't pushed yet. I am also writing an improved interpreter that will use the parser instead of the hacky thing that just goes through strings.

However, for the time being, even after I release this version, I would recommend maybe using the older version for a while if anyone is using it, because this version is probably even more buggy.

However, you know how a few posts ago (but more than a year ago (wow) ) I posted that post where I said that I didn't think bifurcate can be used to split values into more than 2 values?

Well I still kind of think that, but on the map page for homestuck on act 6, it says split Act_6[Act_1,Act_2,Act_3,<etc>];

So this is something I intend to implement, and something I am implementing.

And like I said before I would like it to be done with repeated bifurcation, as a sort of syntactic sugar.

And I am thinking I want it to be like

[a,b,c] means the same thing as [a,[b,c]]

so split Z[A,B,C];

would be the same as

BIFURCATE Z[A,BCTEMP]; BIFURCATE BCTEMP[B,C];

and that split [A,B,C]Z;

would be the same as

BIFURCATE [B,C]BCTEMP; BIFURCATE [A,BCTEMP]Z;

But the way the splits would be done could also be backwards

so [a,b,c] could be the same as [[a,b],c]

I'm pretty sure I prefer the first way, but the second way is actually easier to implement.

or at least cleaner looking to implement.

Why doesn't my code look clean ever?

Anyway, my reason for this post is this:

Does anyone have any opinions about how split is implemented?


Tags
12 years ago

unary to binary (back again, an objects journey) (part 1)

ok, so last time I didn't actually get to converting from unary to binary again.

So I guess I will do that here. (HAHA I JUST GOT IT TO DIVIDE WITH REMAINDER. APPLYING THAT TO THE CONVERSION IN PART 2)

so to do this we have to find the largest power of two not greater than the number,  then the largest power of two not greater then what is remaining of the number, and repeat until we reach zero.

OR we can

find the largest power of two not greater than the number, then for each power of two less than the number, if it is not greater than it, use a 1 and subtract it from the remaining number.

and then for both of these, of course, reverse the result at the end so it is in the right order. 

I think so far the second one of these sounds better.

but come to think of it, repeated integer division by two might work well.

that is, instead of repeated doubling, and then checking which one is larger, it might be faster to do repeated halving. (with a remained of either zero or one)

this could be then used to make a integer base 2 logarithm, with a remainder thing.

and instead of checking each power of two less than it, we could just find the integer base 2 log, and the base 2 log of the "remainder" and the base 2 log of THAT remainder and so on.

that seems like its the best way to me so far, but how do we take the integer logarithm AND get the log remainder thing?

getting the logarithm is fairly simple, just keep dividing by the number until you reach 1.

but how do you get the remainder part? and also how do you do the integer division in the first place with these number like things that we have created?

well lets answer the first of those questions first.

It seems like the remainder of the logarithm is probably related to the remainders of the divisions that make it up.

so lets look at taking the integer base 2 logarithm of 9:

9 4 (rem 1) 2 1

that took 3 halvings, and there was one remainder of one.

what if we try on 10 or something though?

10 5 2 (rem 1) 1

that also took 3 steps and one remainder, but the remainder was on a different step.

now lets try 11

11

5 (rem 1) 2 (rem 1) 1 (rem 0)

now wait, theres a pattern here, which might allow for converting it even faster.

the remainders of the divisions are the remainders of the logs expressed in binary!

we COULD turn them back into unary, so we could take the log of it again,

but what we are doing it converting it into binary anyway! (so that would be a silly step)

so if we look at what we were doing with the repeated division again, but looking at the numbers in binary, it will be pretty evident how to convert it in a better way.:

lets try 9 again (which is 1001)

1001

100 (rem 1) 10 (rem 0) 1 (rem 0) 0 (rem 1)

whats that? the remainders are the number we want in binary?

and now that I think of that it was kind of obvious?

yes. I for some reason did not think of that earlier.

so yeah, thats what we will do, we will repeatedly integer divide the number by 2 until we reach zero, and the remainders will be the number in binary.

but wait, we haven't even written out how to divide the number by two in the first place!

well turns out its not actually that hard to do.

in a loop (that keeps looping provided the number is not zero) subtract one, and if its still not zero, subtract one again. if it is zero after the first subtracting, the remainder of that division is 1. (only the last loop will cause the remainder to be 1) count the number of times where 2 was subtracted. 

so like for 5 that would be

5 3 1 1 2 0 2 (remainder 1)

so that gives the correct answer (2 with remainder zero)

ok, now to implement this integer division by 2 in drocta ~ATH (wow! already? that was fast! /sarcasm)

//given that NUM is the variable that has the name we are halving BIFURCATE [NUM,NUM]2NUM; BIFURCATE 2NUM[NUMCPY,JUNK]; BIFURCATE [BLAH,BLAH]2BLAH; BIFURCATE 2NULL[SUBCOUNT,REMAINDER]; ~ATH(NUMCPY){ BIFURCATE 2BLAH[UNEVEN,JUNK]; BIFURCATE NUMCPY[JUNK,NUMCPY]; BIFURCATE [NUMCPY,NUMCPY]2NUMCPY; BIFURCATE 2NUMCPY[NUMCPYCPY,JUNK]; ~ATH(NUMCPYCPY){ BIFURCATE [BLAH,SUBCOUNT]SUBCOUNT; BIFURCATE NUMCPY[JUNK,NUMCPY]; BIFURCATE 2NULL[UNEVEN,NUMCPYCPY]; } ~ATH(UNEVEN){ BIFURCATE [BLAH,REMAINDER]REMAINDER; BIFURCATE 2NULL[UNEVEN,JUNK]; } } BIFURCATE [NUMCPY,NUMCPY]2NUMCPY; BIFURCATE 2NUMCPY[NUMDIV2,JUNK]; 

ok, that should divide the number by 2, put the result in NUMDIV2, and put the remainder in REMAINDER.

this takes linear time based on the size of the unary number. (provided I didn't make a mistake)

so now it is time to make sure it works, hold on a second while I check it. (I mean, im not going to post this until after I check it, so it doesn't really make sense to tell you to wait, because you just keep reading, but as I am writing this, I am about to test it.)

ok, I tested it. and it didn't seem to work, but then I realized that I made a mistake in making the test. (I checked the wrong variable)

but yeah, turns out that works...

ok, so now we need to repeatedly divide the number by two to get the remainders.

and we need to store these remainders in a list or something.


Tags
11 years ago
Learn ~ATH Turned 1 Today!

Learn ~ATH turned 1 today!

yaaaay!

So I think that my explanations of things hasn't been very clear so far, and while I can't promise that they will improve, I do intend to teach programming in person at my school, which might help me to know how to make this tutorial better. Maybe, maybe not. 

May things go well!


Tags
Loading...
End of content
No more pages to load
learn-tilde-ath - Learn ~ATH
Learn ~ATH

News and tutorials on drocta ~ATH by drocta. interpreter here A brief summary of how to write code in the language (but also see the table of contents)

38 posts

Explore Tumblr Blog
Search Through Tumblr Tags