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.

This forum is locked: you cannot post, reply to, or edit topics. TI-Basic Brain Teasers => TI-BASIC
Author Message
Recursive Acronym


Advanced Member


Joined: 11 Dec 2006
Posts: 499

Posted: 23 Sep 2007 06:03:41 pm    Post subject:

Wow, I started this topic and I haven't posted for ages...
Anyway, here is a small challenge:
Create a program that approximately calculates the cosine (or sine, it doesn't matter) of a number given in degrees or radians without using trigonometric functions.


Last edited by Guest on 29 Sep 2007 10:52:44 am; edited 1 time in total
Back to top
Weregoose
Authentic INTJ


Super Elite (Last Title)


Joined: 25 Nov 2004
Posts: 3976

Posted: 23 Sep 2007 06:19:40 pm    Post subject:

I have 21 bytes (within a program, minus the title).
Back to top
simplethinker
snjwffl


Active Member


Joined: 25 Jul 2006
Posts: 700

Posted: 23 Sep 2007 06:54:52 pm    Post subject:

How exact do you want it?

[edit] I can only get 26 bytes...

[another edit] 7 Bytes (in mem menu prog named A is 17 bytes, so 17-9-1=7). The method itself is 4 tokens


Last edited by Guest on 23 Sep 2007 07:17:17 pm; edited 1 time in total
Back to top
Weregoose
Authentic INTJ


Super Elite (Last Title)


Joined: 25 Nov 2004
Posts: 3976

Posted: 23 Sep 2007 07:30:04 pm    Post subject:

After another trial, I believe I've matched simplethinker's (four tokens).
Back to top
simplethinker
snjwffl


Active Member


Joined: 25 Jul 2006
Posts: 700

Posted: 23 Sep 2007 07:37:04 pm    Post subject:

Weregoose wrote:
After another trial, I believe I've matched simplethinker's (four tokens).
[post="113088"]<{POST_SNAPBACK}>[/post]

Thanks, I figured it out by trying to think of how you got it, and was worried it might be considered cheating since it was smaller than yours... :biggrin:

One question: When you say '21 bytes within a program, minus the title', do you mean taking 1 from the size in the mem menu? or the actual size of only the tokens (plus the newlines)?


Last edited by Guest on 23 Sep 2007 07:39:10 pm; edited 1 time in total
Back to top
Weregoose
Authentic INTJ


Super Elite (Last Title)


Joined: 25 Nov 2004
Posts: 3976

Posted: 23 Sep 2007 08:48:51 pm    Post subject:

I read off the number of bytes as it appears in the [2nd] [+] [2] [7] menu, then subtract the number of characters in the title of the program (just one if it is prgmA).

Last edited by Guest on 24 Sep 2010 03:19:36 pm; edited 1 time in total
Back to top
DarkerLine
ceci n'est pas une |


Super Elite (Last Title)


Joined: 04 Nov 2003
Posts: 8328

Posted: 23 Sep 2007 09:24:34 pm    Post subject:

How approximately are we approximating the sine or cosine here? And is something like real(e^(iAns "approximating" or just cheating?

Last edited by Guest on 24 Sep 2010 03:19:58 pm; edited 1 time in total
Back to top
Weregoose
Authentic INTJ


Super Elite (Last Title)


Joined: 25 Nov 2004
Posts: 3976

Posted: 23 Sep 2007 09:29:54 pm    Post subject:

To me, that's less cheating than what I think simplethinker and I came up with (no offense):

cos(θ) = [font="courier new"]P►Rx(1,θ

sin(θ) = [font="courier new"]P►Ry(1,θ

Last edited by Guest on 24 Sep 2010 03:20:36 pm; edited 1 time in total
Back to top
Recursive Acronym


Advanced Member


Joined: 11 Dec 2006
Posts: 499

Posted: 24 Sep 2007 03:57:23 am    Post subject:

Sorry, I should have been more specific. I don't really care about the size for this challenge. I hadn't thought of the polar conversions, but I'll say now that they are off limits. Mine only does the first quadrant, but it doesn't matter because of the symmetries of the trig functions. It gives results with an error of about 10^(-6) and takes about 3 seconds. I think there are polynomials that approximate trig functions, so that would be one way, but I used a different method.
Back to top
thornahawk
μολών λαβέ


Active Member


Joined: 27 Mar 2005
Posts: 569

Posted: 24 Sep 2007 07:08:44 am    Post subject:

Would using inverse trigonometric functions or complex exponentials e^(i*something) be disallowed as well, just to clear things up? ;)

thornahawk

P.S. It must be added as a reminder for the respondents to this challenge to note: once you can compute sine, it merely takes some modifications for your program to compute cosine (and vice-versa). Very Happy


Last edited by Guest on 24 Sep 2007 07:12:39 am; edited 1 time in total
Back to top
simplethinker
snjwffl


Active Member


Joined: 25 Jul 2006
Posts: 700

Posted: 24 Sep 2007 08:44:34 am    Post subject:

I have a slight modification to this. Have a program that approximates sin(x) without the use of: any form of i or e (including logs), no trig function (including hyperbolic and inverse), not complex, angle or polar functions, and has to work in Real mode. I have 34 bytes
Back to top
alexrudd
pm me if you read this


Bandwidth Hog


Joined: 06 Oct 2004
Posts: 2335

Posted: 24 Sep 2007 10:14:49 am    Post subject:

You should specify accuracy, too. My program below has ± 1 x 100 accuracy calculating sine or cosine in any quadrant.

Code:
:0
Back to top
DarkerLine
ceci n'est pas une |


Super Elite (Last Title)


Joined: 04 Nov 2003
Posts: 8328

Posted: 24 Sep 2007 10:32:23 am    Post subject:

simplethinker wrote:
I have a slight modification to this. Have a program that approximates sin(x) without the use of: any form of i or e (including logs), no trig function (including hyperbolic and inverse), not complex, angle or polar functions, and has to work in Real mode. I have 34 bytes
[post="113210"]<{POST_SNAPBACK}>[/post]
But I can imagine someone using all of these legitimately. How about the simple condition that it has to actually approximate sine, rather than tricking the calculator into computing it using various identities?

As for accuracy, I think that a reasonable requirement would be that the program be more accurate (on, say, the first quadrant) than the longest Maclaurin series that you could fit in the same size.

(note: a sum(seq( of the first 15 terms fits into 36 bytes (and could probably be slightly optimized), and gives as good accuracy as the calculator can handle on the first quadrant)


Last edited by Guest on 24 Sep 2010 03:20:55 pm; edited 1 time in total
Back to top
thornahawk
μολών λαβέ


Active Member


Joined: 27 Mar 2005
Posts: 569

Posted: 24 Sep 2007 11:18:11 am    Post subject:

I have a cosine routine here I wrote with more emphasis on efficiency and as wide a range as possible (works even for, say X>1000 with relatively good accuracy) rather than size... unfortunately I had to use a logarithm, but with no complex number trickery, it must be added.

[highlight below]
[font="courier new"]
1→Y:X→Z
If real(X)<0
-X→Z
If abs(Z)>e-6:Then \\ e-6 : 10^-6
max(0,int(log(abs(Z))/log(2)))→K \\ estimate the size (or do nothing if Z is small)...
Z/2^K→Z \\ ...then scale accordingly by halving K times
1→T:0→J
Repeat abs(Y–O)=0 \\ use the Maclaurin series on the scaled argument
Y→O:J+2→J
-Z²T/J/(J–1)→T
Y+T→Y
End
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


I did not lob off the ending parentheses as is customarily done, so there's room for further size reduction. :P

(edit 9/27/2007: The original program can only handle real numbers; I have now incorporated the necessary modifications for it to work on complex numbers as well. I have also added a few explanatory comments within the program.

edit 10/1/2007: removed a grievous bug that made the routine inaccurate for small arguments.)

thornahawk

Last edited by Guest on 01 Oct 2007 02:51:12 am; edited 1 time in total
Back to top
Weregoose
Authentic INTJ


Super Elite (Last Title)


Joined: 25 Nov 2004
Posts: 3976

Posted: 24 Sep 2007 11:40:03 am    Post subject:

Couldn't one take the input (mod 2[font="times new roman"]π) at the beginning instead of having to worry about its size?

I'll have a routine up here in a bit.
Back to top
thornahawk
μολών λαβέ


Active Member


Joined: 27 Mar 2005
Posts: 569

Posted: 24 Sep 2007 12:00:15 pm    Post subject:

@Goose: Here is a paper you might want to look at regarding your question.

thornahawk
Back to top
Weregoose
Authentic INTJ


Super Elite (Last Title)


Joined: 25 Nov 2004
Posts: 3976

Posted: 24 Sep 2007 12:11:05 pm    Post subject:

Oh, so the very build of the number in the input is essential to the algorithm. :)

[EDIT]

Okay, while some may not agree with this...

[s]2[font="times new roman"]π/79fPart(.5X/[font="times new roman"]π


(With X being treated as a number of degrees.)

My argument is that, with a maximum error of about ±.078 for any input, it's good enough for plotting on a graph screen that's not zoomed too far in. (Cop-Out Award 2007)[/s]

No, no, thats all wrong. Yikes.

Alright, this is shameful. I'm trying again, I promise. :lol:

[EDIT×2]

X-[font="times new roman"]πint(X/[font="times new roman"]π
.5Ans/[font="times new roman"]π²(Ans-3[font="times new roman"]π)(Ans-[font="times new roman"]π)(1-2([font="times new roman"]π<X-2[font="times new roman"]πint(.5X/[font="times new roman"]π

I averaged the sides of this inequality:



...and patched it to give it cyclic behavior. The maximum error is about ±.1182.

Still terrible. :D

Back to products/summations, I suppose.

Last edited by Guest on 24 Sep 2010 03:22:04 pm; edited 1 time in total
Back to top
simplethinker
snjwffl


Active Member


Joined: 25 Jul 2006
Posts: 700

Posted: 24 Sep 2007 02:52:27 pm    Post subject:

DarkerLine wrote:
simplethinker wrote:
I have a slight modification to this. Have a program that approximates sin(x) without the use of: any form of i or e (including logs), no trig function (including hyperbolic and inverse), not complex, angle or polar functions, and has to work in Real mode. I have 34 bytes
[post="113210"]<{POST_SNAPBACK}>[/post]
But I can imagine someone using all of these legitimately. How about the simple condition that it has to actually approximate sine, rather than tricking the calculator into computing it using various identities?

As for accuracy, I think that a reasonable requirement would be that the program be more accurate (on, say, the first quadrant) than the longest Maclaurin series that you could fit in the same size.

(note: a sum(seq( of the first 15 terms fits into 36 bytes (and could probably be slightly optimized), and gives as good accuracy as the calculator can handle on the first quadrant)
[post="113212"]<{POST_SNAPBACK}>[/post]


I know that they can be used legitimately. I just want(ed) to see how efficient you guys can get a maclaurin polynomial.

Okay, here's new restrictions. You can use i and complex functions (still not using polar or e or trig), but you have to be accurate to 10 decimal places (with any input, not just simple things like π/6) from zero to 2π.

[edit] DarkerLine: In my program I've found that anything past the 32nd maclaurin polynomial makes a negligable difference (less than 10^-10)

[another edit] Weregoose, where'd you get that inequality? It looks familiar but I can't put my finger on it... (but don't tell me if it's so simple that I'll feel like and idiot... Neutral )

[last edit] Recursive Acronym, what method did you use? Or at least give a hint so I can see what you meant for the answer to be.
Also, what I meant by no logs was no natural logs, because then you would still be able to use the euler identity, so thornahawk's method wasn't cheating


Last edited by Guest on 24 Sep 2010 03:18:57 pm; edited 1 time in total
Back to top
thornahawk
μολών λαβέ


Active Member


Joined: 27 Mar 2005
Posts: 569

Posted: 24 Sep 2007 07:32:03 pm    Post subject:

I'll be doing a bit of explaining with the cosine program I've written, so here it goes as hidden text:


First, as it was mentioned in the paper, you have two possible choices for range reduction: you can either repeatedly subtract multiples of the period from the argument (mod 2π reduction) or scale the argument by halving repeatedly (which is the method I used). The importance of this, again, is that Maclaurin series (or for those with more sophistication, Padé approximants or economized rational approximations) are only worthwhile near their point of expansion, which is commonly taken to be 0. The further away the argument is from 0, the worse these approximations become.

Now that we've gotten the importance of reduction out of the way, I now justify why I chose halving over subtraction:

1. Subtractive cancellation - at large enough arguments, trying to subtract a multiple of 2π of comparable magnitude will leave you with much less significant digits than original. Because of this, any trig function you will be computing with this now not-so-accurate argument will be inaccurate, too. Special tricks like those mentioned in the paper must then be employed.

2. Generality - mod 2π reduction will definitely not be helpful if your argument has large imaginary part (e.g. cos(100i) and cos(40π+100i) would of course have the same value), and since large absolute values are bad for the underlying approximants, scaling by halving will be a much simpler solution for handling arguments of large absolute value.

Besides, I again scoped in advance: apart from complex numbers, you can modify the algorithm easily and it will compute a matrix cosine.



simplethinker: One can still cheat to use common logarithms instead of natural ones; log(X)/log(e)=ln(X) is valid for all complex values. ;)

Weregoose, it seems, got that cute Wink inequality from the Wolfram Functions Site, which also allows us to compute the cosine and a bunch of other functions to arbitrary accuracy, so we all now have a standard to check against. :D

If everybody wants to stress-test their trig routines, here are a few choice arguments to test against:

π/4 (customary Wink )
i (should give, with some factors, the hyperbolic equivalent)
arguments with absolute value >10^3, including complex ones (e.g. 4+1000i)

thornahawk
Back to top
simplethinker
snjwffl


Active Member


Joined: 25 Jul 2006
Posts: 700

Posted: 24 Sep 2007 08:53:23 pm    Post subject:

About the log, natural log thing. I was hoping that no natural logs would rule out people's thoughts of using logs to get it, because it is a form of e/natual log, which I had said no forms of. I'm probably making this even more confusing, since I was hoping for people to figure out what I meant without having to go into detail of what I meant. So, after the corrections to what I used for restrictions, I'm even more confused of what I meant, so disregard everything I said. Cool
Back to top
Display posts from previous:   
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 1, 2  Next
» View previous topic :: View next topic  
Page 1 of 2 » All times are UTC - 5 Hours

 

Advertisement