Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Cos-1
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Insectoid




PostPosted: Mon Nov 17, 2008 1:03 pm   Post subject: Cos-1

Hi,

I'm working on a program (obviously) which requires a cos-1 function. I looked through the reference and didn't find a pre-defined function, so I decided to make my own. I created an array 1..360 and filled it with cos (index). I have created an equation to give me the cos of an angle, but to retrieve the angle I need to convert that cos using cos-1. Of course, there are far more than 360 possible cosines, and the odds of arriving exactly at one of the 360 points is extremely low. So I need to know how to compare a number to each element of an array, and find out which number is closest to it. Basically, I need it to 'almost equal', to 4 decimal places, I believe.

Ex.

cos x = 0.1234

cos n = 0.1236
cos m = 0.5974

Obviously, cos x is closest to cos n, but how do I determine that with code, among 360 different possibilities?
Sponsor
Sponsor
Sponsor
sponsor
HellblazerX




PostPosted: Mon Nov 17, 2008 1:16 pm   Post subject: Re: Cos-1

Arccos

but, if you still want to make that function of yours, most likely you'll just have to check the absolute difference between each element, and go with the one with the lowest.
Insectoid




PostPosted: Mon Nov 17, 2008 1:37 pm   Post subject: RE:Cos-1

Absolute difference, is there a function for that? (Not that I need one for absolute difference)

Absolute difference is just the larger number minus the smaller number, right?
HellblazerX




PostPosted: Mon Nov 17, 2008 1:41 pm   Post subject: Re: Cos-1

Sorry, I guess I wasn't clear enough, but arccos is the function you're looking for. However, if you still want to do it yourself, take the difference of the two values (doesn't matter which you use to subtract with), and use Math.abs to get the absolute value of it.
Insectoid




PostPosted: Mon Nov 17, 2008 1:53 pm   Post subject: RE:Cos-1

You definitely were clear enough. I can't do a problem for this long, then find a pre-existing answer and use it. I want to do my own function.

Exactly what is the 'absolute difference'? (going to wiki now...)
Vermette




PostPosted: Mon Nov 17, 2008 2:04 pm   Post subject: RE:Cos-1

It simply means dropping the sign from the number. |-3| == |3| == 3
A.J




PostPosted: Mon Nov 17, 2008 4:12 pm   Post subject: Re: Cos-1

in turing, abs(x) is sufficient enough for the absolute value function.

and turing already has an inbuilt function for cos and arccos :
code:

var theta : real := 30.0
put cosd (theta) % outputs cos(theta) in degrees
put cos (theta) % outputs cos(theta) in radians
var gamma : real := 0.5
put arccosd (gamma) % outputs arccos(gamma) in degrees
put arccos (gamma) % outputs arccos(gamma) in radians
Insectoid




PostPosted: Mon Nov 17, 2008 6:10 pm   Post subject: RE:Cos-1

Well, it doesn't really matter. I have gotten an assignment today from my teacher: Learn Perl, and write a program in it that will generate random sentences. Yes, it will be incredibly hard, but I have some ideas. (by the way, does anyone know if Perl has a built-in dictionary of words including type(noun, verb, etc), plural, etc. or where I can find one?)
Sponsor
Sponsor
Sponsor
sponsor
gitoxa




PostPosted: Mon Nov 17, 2008 10:43 pm   Post subject: RE:Cos-1

Make your own wordlist. Something simple like:
The <adjective> <noun> <verb>ed the <adjective> <noun>
The jealous dog kicked the emo rabbit
DemonWasp




PostPosted: Tue Nov 18, 2008 2:54 pm   Post subject: RE:Cos-1

gitoxa's idea would work, but here's a more entertaining one:

Start with a large (huge!) body of text. Something on the order of several megabytes, preferably. Take the text of a few books and append them together.

Have the program do the following:
1. Choose a random starting location in the text.
2. Read the next L (length, try 5-7) characters from the input (I) into S (working-string).
3. Output the first character of S, then remove it from the string, so that you have the last L-1 characters remaining.
4. Starting at a random location in I, search for S. When you find it, read the next character AFTER the instance of S found and add it to S.
5. GOTO 3.

Ex.
Input Text (partial lyrics to Through the Fire and the Flames, available online easily):
On a cold winter morning, in the time before the light,
In flames of death's eternal reign, we ride towards the fight.
When the darkness has fallen down, and the times are tough all right.
The sound of evil laughter falls around the world tonight.

Fighting hard, fighting on for the steel, through the wastelands evermore,
The scattered souls will feel the hell, bodies wasted on the shore.
On the blackest plains in Hell's domain, we watch them as they go,
Through fire and pain, and once again we know...

So now we'll fly ever free, we're free before the thunderstorm,
On towards the wilderness, our quest carries on.
Far beyond the sundown, far beyond the moonlight,
Deep inside our hearts and all our souls...

So far away we wait for the day,
For the lives all so wasted and gone;
We feel the pain of a lifetime lost in a thousand days-
Through the fire and the flames we carry on!

L = 2 since I'm lazy and that's simple.

1. So we start at a random location, let's say we start part-way through "eternal".
2. Then S = "er"
3. We output e, leaving S = "r"
4. then start looking from a random location for S = "r", which we quickly find in "wilderness". We read in the next letter (n), so S = "rn".
5. We go back to 3 and do it again.

This method produces some very strange text. I wrote such a program years ago, here's some sample output:

L = 4, Input was some DnD books
Hawksguardiance them herse a this farm and animal lovely previous Stone. Adon said."A summoned antic to clarate antime beside of the fight to Midnight, and all Keep her magic-user his end mist. How castave to find be might a true made. There although Lord Bane for Bane!"Suddenly estions, but spenses, strange evening, and command. The headened fight gentlety assault of battled when in who much this had as men...

L = 7, similar DnD-book input
They didn't care to everything beyond the altar; a gigantic black pit known collective passage of a man named Cyric made him and poised in that his hands surrounded from ear to lower levels almost beyond Gnoil Pass.A caravan, which he held his actions seemed to cross the storm, but he knew the man's cheek.Kelemvor rolled one of the attention on Adon didn't evaporate in sometimes," Kelemvor having that gave me, you with the idea of taking from the true nature than any of the clear signs that reached the chests and looked away and stared at the insects crawling away and of her?"Cyric laughed, and he fell back and spoke endless series of green eyes, and yet it seems."The bald man sprang into the warning, Kelemvor was a miracle you from existence."You will bring me what happened long ago. Whatever Elminster called the price of the angry fighter as she felt a slightly, but he worried expression vanished.Safely hidden, although the castle.
andrew.




PostPosted: Tue Nov 18, 2008 6:20 pm   Post subject: RE:Cos-1

LOL. Kind of like mad libs. That's cool.
r691175002




PostPosted: Tue Nov 18, 2008 6:53 pm   Post subject: Re: RE:Cos-1

insectoid @ Mon Nov 17, 2008 1:53 pm wrote:
You definitely were clear enough. I can't do a problem for this long, then find a pre-existing answer and use it. I want to do my own function.

Ideally one would perform a linear interpolation between the two closest values. I will point out that there are simpler, faster and more accurate methods of approximating arccos.

Generating text is really much simpler than you might expect. There are many ways of producing believable text, and of them Markov Chains are what I would recommend.
http://en.wikipedia.org/wiki/Markov_chain

A quick explanation and samples of the technique can be found here:
http://www.codinghorror.com/blog/archives/001132.html
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: