This is an archived, read-only copy of the United-TI subforum , including posts and topic from May 2003 to April 2012. If you would like to discuss any of the topics in this forum, you can visit Cemetech's
TI-BASIC subforum. Some of these topics may also be directly-linked to active Cemetech topics. If you are a Cemetech member with a linked United-TI account, you can link United-TI topics here with your current Cemetech topics.
TI-Basic Brain Teasers =>
TI-BASIC
Author |
Message |
|
thornahawk μολών λαβέ
Active Member
Joined: 27 Mar 2005 Posts: 569
|
Posted: 25 Sep 2007 06:59:27 am Post subject: |
|
|
I have a proposal: seeing that we have so many different brain teasers coming from so many different sources... might it be time to make a new TI-Basic subforum? ;)
thornahawk |
|
Back to top |
|
|
Recursive Acronym
Advanced Member
Joined: 11 Dec 2006 Posts: 499
|
Posted: 25 Sep 2007 11:16:42 pm Post subject: |
|
|
A new subforum would be okay... this topic is on the small side; but since we have several problems going on at once in this thread, a new forum might be nice. It would definitely have more activity than the 68k Basic forum, though .
My program does cosine, accepting angles from 0 to 90 degrees. It also asks for a number of iterations. It works by dividing breaking the angle input into parts, each part being .5 the size of the previous. 90 would be broken up into 45+22.5+11.25+5.625+..., which approaches 90 as the number of terms increases. 58 would be broken up as 45+11.25+1.40625+.17578125..., which approaches 58. A greater number of iterations will increase the number of terms in this sequence, the related accuracy, and also the processing time. It also calculates the correct answer at the beginning and displays it alongside program's calculated value for comparison purposes. Once again, it is a proof of concept, and isn't optimized for size or speed. Code: Prompt θ,I
Degree
cos(θ→A
1/√(2→C
1→R
45→M
For(X,1,I
If θ≥M
Then
RC-√(1-R^2)√(1-C^2→R
θ-M→θ
End
M/2→M
√((C+1)/2→C
End
Disp "RESULT",R,"ACTUAL",A
Last edited by Guest on 25 Sep 2007 11:17:24 pm; edited 1 time in total |
|
Back to top |
|
|
thornahawk μολών λαβέ
Active Member
Joined: 27 Mar 2005 Posts: 569
|
Posted: 26 Sep 2007 11:43:41 am Post subject: |
|
|
I see recognizable portions of CORDIC in RA's program that he posted above.
You know, of course, that you don't have to switch to Degree mode to do this, right? ;)
thornahawk
P.S. It's a common theme, as always: efficiency, accuracy, generality, and code size can become conflicting goals when writing a program such as this. |
|
Back to top |
|
|
Recursive Acronym
Advanced Member
Joined: 11 Dec 2006 Posts: 499
|
Posted: 26 Sep 2007 03:07:08 pm Post subject: |
|
|
The switch to Degree mode is so that the actual answer can be stored to A and displayed when execution is finished. |
|
Back to top |
|
|
Weregoose Authentic INTJ
Super Elite (Last Title)
Joined: 25 Nov 2004 Posts: 3976
|
Posted: 26 Sep 2007 05:32:11 pm Post subject: |
|
|
It might be useful to know that the degree and radians symbols usually override the mode settings (but not when using the inverse trig functions). It's a good way to save a byte or two while leaving the user settings alone. |
|
Back to top |
|
|
DarkerLine ceci n'est pas une |
Super Elite (Last Title)
Joined: 04 Nov 2003 Posts: 8328
|
Posted: 26 Sep 2007 11:34:02 pm Post subject: |
|
|
You can kind of get around the inverse trig function problem by dividing by the appropriate unit instead... e.g. sin-1(1/2)/1o will return 30 regardless of the mode setting.
Last edited by Guest on 24 Sep 2010 03:09:56 pm; edited 1 time in total |
|
Back to top |
|
|
thornahawk μολών λαβέ
Active Member
Joined: 27 Mar 2005 Posts: 569
|
Posted: 27 Sep 2007 08:10:05 am Post subject: |
|
|
The "45" in RA's program can be changed to "π/4", and the program would now work in Radian mode. ;)
thornahawk |
|
Back to top |
|
|
DarkerLine ceci n'est pas une |
Super Elite (Last Title)
Joined: 04 Nov 2003 Posts: 8328
|
Posted: 27 Sep 2007 02:07:00 pm Post subject: |
|
|
And if you write it 45o it will work in both modes.
Last edited by Guest on 24 Sep 2010 03:09:35 pm; edited 1 time in total |
|
Back to top |
|
|
thornahawk μολών λαβέ
Active Member
Joined: 27 Mar 2005 Posts: 569
|
Posted: 01 Oct 2007 02:56:15 am Post subject: |
|
|
There, I've added to and edited the solution I've posted. That indeed was an embarrassing bug. :blush:
I'm posting another solution that uses an approximant with better convergence properties. Here it is, with explanatory comments:
[highlight below]
[font="courier new"]
1→Y
If abs(X)>e-6:Then \\ e-6 : 10^-6
X→Z:0→K
While abs(Z)>π/4 \\ scale X accordingly if needed
K+1→K:Z/2→Z
End
3→B:Z²/4→A:1→Y \\ evaluate the continued fraction for tan(Z/2)...
Y→C:0→D
Repeat abs(H–1)=0 \\ ...using Lentz's algorithm
(B–AD)ֿ¹→D
B–A/C→C
CD→H:HY→Y
B+2→B
End
(4Y²–Z²)/(4Y²+Z²)→Y \\ use the identity relating tan(Z/2) and cos(Z)
For(J,1,K)
2Y²–1→Y \\ apply the double-angle formula K times
End
End
If imag(Y)=0 \\ ensures real results are returned as real numbers
real(Y)→Y
Y
There's a second way to evaluate the approximant I used in the above entry; I will post the code here in a while...
thornahawk
Last edited by Guest on 02 Oct 2007 10:04:39 pm; edited 1 time in total |
|
Back to top |
|
|
|
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