Trigonometric functions

I am currently working on a project that may make use of complex trigonometric functions. I am getting increasingly frustrated, and I wonder if it’s because I am repeatedly using phrases like ‘tanal is cosine of r degrees * tangent of latitude degrees’, etc. Do I only need to use the word ‘degrees’ once to get the program to understand values in degrees(as opposed to radians)? And if so, do I need to use the word ‘degrees’ in each equation at the first occurrence of an angle?

I am (thankfully) developing this part separately from the main program, until I get a working subroutine.

Thanks

It’s probably not you; the order of operations for trigonometric functions is messed up: bug report. That bug report applies to equations, but is even more likely to apply to things that aren’t in equations. If you do this:

[code]Lab is a room.

R is a real number that varies. R is initially 90.
Latitude is a real number that varies. R is initially 90.
tanal is a real number that varies.

When play begins:
now tanal is cosine of r degrees * tangent of latitude degrees;
say “Tanal is [tanal]. Cosine of r degrees is [cosine of r degrees]. Tangent of latitude degrees is [tangent of latitude degrees].”[/code]

you get

which I think means that it’s taking (90 degrees * tangent of 90 degrees), which is 0, and taking the cosine of that, which is 1.

Put in parentheses:

[code]Lab is a room.

R is a real number that varies. R is initially 90.
Latitude is a real number that varies. R is initially 90.
tanal is a real number that varies.

When play begins:
now tanal is (cosine of r degrees) * (tangent of latitude degrees);
say “Tanal is [tanal]. Cosine of r degrees is [cosine of r degrees]. Tangent of latitude degrees is [tangent of latitude degrees].”
[/quote]
and it works (approximately):

[code]Lab is a room.

R is a real number that varies. R is initially 90.
Latitude is a real number that varies. R is initially 90.
tanal is a real number that varies.

When play begins:
now tanal is (cosine of r degrees) * (tangent of latitude degrees);
say “Tanal is [tanal]. Cosine of r degrees is [cosine of r degrees]. Tangent of latitude degrees is [tangent of latitude degrees].”[/code]

though note that the numbers aren’t exact (I suspect that the conversion to radians rounds the numbers off very slightly, leading to a tiny negative value for the cosine).

So, if something is going wrong, wrap everything in parentheses and see if that helps. If that doesn’t help, wrap it in more parentheses. (And for stuff like this it might be a good idea to use equations rather than set the values in line, because it’s likely to get very ugly-looking very quickly. Which, if I read your post already, you’re already doing.)

Thanks, Matt–the thing was that it kept throwing problem messages at me during the compile, saying that it juggled around my equation and kept trying to look at it in different ways, and none of them worked(and you were right, I padded it with parentheses in every which way I could imagine). I realized why it didn’t understand the equation. Instead of using the correct variable, I instead typed the name of the thing it applied to–in this case, I typed ‘latitude’(which is part of the name of a noun in my project) instead of ‘lat’, the name of the variable applying to it. Once I saw this, I retyped the whole equation the way I originally wanted it, this time with ‘lat’, the correct variable, and it compiled smoothly. Next, apparently, I need to see if the calculation is correct–thank you for mentioning the bug report, this may prevent me from tearing my hair out further.

Next question, before I try this out…

the manual says that initially, the program sees angles in terms of radians, unless you specify degrees, such as ‘lat degrees’. In my project, I will need to use the inverse functions, such as arctan x. The manual makes no mention of whether this function will render a response in degrees or radians. Will I need to convert the response if I want degrees?

Thanks.

I’d be surprised if you didn’t–this may be somewhat implicit when it describes arctan as the inverse of the tangent function, since if the tangent function takes an angle in radians to a real number, the inverse should take a real number to an angle in radians. You could always write a tiny project that prints out arctan(1) and see what it says. (I always keep at least one dummy project open to try out little bits of code when I need to see what their effects are without putting them in the main code.)

After a bit of lugubrious experimentation, using a dummy command which I filled with a number of preset parameters for latitude, etc(to use until I get correct results), I found out I have to put parentheses around every trig function, and another set around pairs of numbers that I have to multiply or divide(apparently the program does the addition/subtraction before these). My formula turned out to look like this–

now tana is (0 minus cosine of ramc degrees) / (((sine of obliq degrees) * (tangent of lat degrees)) + ((cosine of obliq degrees) * (sine of ramc degrees)))

Rather obtuse, but it works.

And yes, I have to alter the result of taking the arctangent of the above, like so–

now atana is (arctangent of tana) / (pi / 180)

I had to grit my teeth, but I’m glad I did it, now I know how to do trig and algebra in Inform.