x.x That's totally correct... I thinking... idk what I was thinking Razz

But that's correct Smile I would rather do
[cpde]repeat Ans=2[/code]
simply because it seems more accurate to do so... idk. I'm going to edit my code to make it correct
I'm with Mufin; just start the timer, run a massive number of iterations, and hope it all evens out.
I dunno... I have done something like:

Code:
Starttimer->K
For(A,0,1000
<code>
End
checktimer(K

and it has returned between +/- 2 seconds, which for me is too much. Where as my routine, it always returns the same thing, else it will more often than not return the right thing Smile
I understand why you're doing that, so I encourage you to keep using that method, especially when you want to evaluate exactly how much faster a method is than another. For my purposes, usually discovering qualitatively which one is faster is good enough.

And Mufin, nice avatar!
*bump* I'm not sure if this has previously been posted.

Modulus Operator

A%B, A mod B, or mod(A,B) (three different ways to represent the modulus of A base B) can be calculated as follows:


Code:
int(BfPart(A/B))


Note that A and B should ideally be integers, and that the outermost int() is necessary to remove some corner cases where the result may appear to be an integer but may have a very small floating point component (specifically, < 10^-10) in the calculator's memory.

Edit: Thanks to PT_ for catching a typo in this post.
KermMartian wrote:
*bump* I'm not sure if this has previously been posted.

Modulus Operator

A%B, A mod B, or mod(A,B) (three different ways to represent the modulus of A base B) can be calculated as follows:


Code:
int(AfPart(A/B))


Note that A and B should ideally be integers, and that the outermost int() is necessary to remove some corner cases where the result may appear to be an integer but may have a very small floating point component (specifically, < 10^-10) in the calculator's memory.


It's been done, except the other guy's method made use of the round( command.
Ah, cheers, thanks for looking. I posted this because Sonlen needed a modulo operation for displaying HH:MM:SS from seconds.
Here's a small (3 line) base-N to decimal converter.

Code:
Input N
Input Str1
sum(seq(inString(sub("123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",1,N),sub(Str1,A,1)),A,1,length(Str1))seq(N^A,A,length(Str1)-1,0,~1->B

I'm still working on a small decimal to base-N converter.
Very nice use of Seq()! I did something very similar with a pair of For() loop for my base converter, but your seq tricks reduce my seven-line implementation to one. Smile Impressive.
Here's my new base-N to base-M converter (total of 16 lines long):

Code:
Input "FROM BASE:",N
Input "TO BASE:",M
Input "",Str1
sum(seq(N^A,A,length(Str1)-1,0,~1)seq(inString(sub("123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",1,N),sub(Str1,A,1)),A,1,length(Str1->B
Pause B
seq(0,A,1,int(log(B)/log(M->L1
While B
int(log(B)/log(M->C
int(BM^~Ans->L1(dim(L1)-Ans+1
B-AnsM^C->B
End
" ->Str1
For(A,1,dim(L1
Str1+sub("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",1+L1(A),1->Str1
End
DelVar L1sub(Str1,2,length(Str1)-1->Str1

The decimal to base-M converter was unfortunately longer than I had hoped for. The last 5 lines I knew would be needed, but I was hoping to get the 6 lines that make the list that stores all the number data to work just fine in 1 or 2 lines. Either way, it's still a huge improvement on my previous one.
How about
Code:
int(BM^~Ans->L1(dim(L1)-Ans+1
to
Code:
int(BM^~Ans->L1(1-Ans+dim(L1
? Saves a byte. Smile
I think one of your statements might be subject to rounding error:

Code:
int(log(B)/log(M->C


That will fail in some cases, for example, int(log(16)/log(2))=3 instead of 4 because log(16)/log(2)=3.9999999999999

This code, which I believe is only one byte extra, should fix this problem:

Code:
int(round(log(B)/log(M->C
Ah yes, that failsome problem. An excellent point, that. I was going to suggest a log reduction until I remembered that it's log(a/b) -> log(a) - log(b), not log(a)/log(b) -> log(a-b).
KermMartian wrote:
How about
Code:
int(BM^~Ans->L1(dim(L1)-Ans+1
to
Code:
int(BM^~Ans->L1(1-Ans+dim(L1
? Saves a byte. :)


:P I wasn't much looking for line-by-line optimizations. I literally made this code in about 30 minutes this morning, after a kid who was asking for help with his binary-decimal converter gave me an idea.

calc84maniac wrote:
I think one of your statements might be subject to rounding error:

Code:
int(log(B)/log(M->C


That will fail in some cases, for example, int(log(16)/log(2))=3 instead of 4 because log(16)/log(2)=3.9999999999999

This code, which I believe is only one byte extra, should fix this problem:

Code:
int(round(log(B)/log(M->C


Noted and updated.

KermMartian wrote:
Ah yes, that failsome problem. An excellent point, that. I was going to suggest a log reduction until I remembered that it's log(a/b) -> log(a) - log(b), not log(a)/log(b) -> log(a-b).


:P I didn't get a 5 on my AP Calc exam to be corrected by a "log reduction" shortcut.
No offense intended, ignore my attempts at optimization. Smile When I see a piece of code that looks extremely well-made my reflex is to try to optimize. As it is, it's very tight code.
KermMartian wrote:
No offense intended, ignore my attempts at optimization. Smile When I see a piece of code that looks extremely well-made my reflex is to try to optimize. As it is, it's very tight code.
Haha, no worries. Thanks for the compliment. Good to see that the countless hours I spent learning to optimize my code hasn't been wasted. Smile
MufinMcFlufin wrote:
KermMartian wrote:
No offense intended, ignore my attempts at optimization. Smile When I see a piece of code that looks extremely well-made my reflex is to try to optimize. As it is, it's very tight code.
Haha, no worries. Thanks for the compliment. Good to see that the countless hours I spent learning to optimize my code hasn't been wasted. Smile
Not at all; you write much better code than many of your peers.
I edited the first post with a list of routines I found in this topic. Parts where people helped others or optimized code, then I added you to the "Author" list (which is the part after the code). If you find any parts where I messed up (I know there are a lot, I went into a new method as I got through all of them, so I need to edit the top parts.) I went along the convention of
Code:
[b][size=18]Title[/size][/b]
[i]Inputs:
A = A number
Str1 = A string
Outputs:
A = Another number[/i]
[code]Code here!
Wee!1!!
Moar code!!![/code]
[i]Author(s)[/i]
If I found any parts that were combined outputs (AKA, Ans and Str1 equal a string), then I did "Ans & Str1 = A string" If I didn't get a name right, or there was better code posted that I missed, tell me Smile
Oh, I should add this to the list before I forget about it entirely.

Compact Triangle Edge-Length Solver (Pythagorean Theorem)

Given
1) side-lengths A, B, and C, where C is the hypotenuse, and
2) the one side of unknown length being set to "0"
The length of the unknown side is


Code:
:C²-B²-A²
:√(Ans-2Ansnot(C


Fairly trivial, but (imho) fairly well-optimized Smile
Tanner, you are most awesome! I have an idea: Do you guys have an interest in working together to make a compendium of useful BASIC routines and release it here and on ticalc.org.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
» Goto page Previous  1, 2, 3 ... 5, 6, 7 ... 13, 14, 15  Next
» View previous topic :: View next topic  
Page 6 of 15
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement