What sin is this?

About Truespace Archives

These pages are a copy of the official truespace forums prior to their removal somewhere around 2011.

They are retained here for archive purposes only.

What sin is this? // Introduction to Scripting

1  |  

Post by mbulman // Oct 29, 2006, 4:45pm

mbulman
Total Posts: 10
pic
Hi,I too enjoy having the scripting course!

This may be a very dumb question and i understand most math but, why is it the Math.sin() values (in the LE) don't match my calculator?

Math.sin(1) comes out -0.544 I though sin was based on the 360 degree thing


am i not understanding something?


Thanks again.

Mark

Post by mbulman // Oct 29, 2006, 5:16pm

mbulman
Total Posts: 10
pic
This is Mark again.

I just wanted to also say that not only Math.sin(1) (through a script variable) but also: sin(4)= 0.745 and sin(5)= -0.262?????

Is it because i'm going through a variable in the attributes part?

I am using a value passed to it by the offsetter.

Post by Norm // Oct 30, 2006, 2:41pm

Norm
Total Posts: 862
pic
You may wish to use a search engine and research what the Math.sin means in terms of jscript or vbscript.

Post by iBrent // Oct 30, 2006, 6:45pm

iBrent
Total Posts: 102
pic
As I understand it, JavaScript computes Math.sin() in radians. To calculate with degrees you need to convert to radians first.


For example:


If I want to know the sin of a 30 degree angle I must convert 30 degrees into radians in order to get the correct answer. By mistake I may type:


Math.sin(30) which gives the wrong answer of -0.988031624 (I was thinking 30 degrees, but the computer saw it as 30 radians)


To return the proper sin ratio we need to covert degrees to radians by using the formula degrees * pi /180 or in this case enter the following:


Math.sin(30*Math.PI/180) which returns 0.5


I only know this because I'm a Flash programmer by day and Actionscript uses the same math functions. I don't know much trueSpace yet, but I think that is what you are seeing in the LE...


HTH,


iBrent

Post by TomG // Oct 31, 2006, 3:19am

TomG
Total Posts: 3397
Hi all,


I get all the radians part, but this is what I don't get. If I convert 5 to radians and take the sin, I do not get -0.262. Same if I take 4, convert to radians, take the sin, I do not get 0.745.


Also, if I just take the sin of 5 radians, I do not get -0.262. And if I take the sin of 4 radians, I do not get 0.745.


In fact, for a sin of 0.745, I get 48.159 as the asin if I use degrees, or 0.840 as the asin if using radians - neither of which was the input value in this case.


So I share the original author's confusion as to the numbers they are getting - they don't match what I would expect either.


My guess is that it relates to the fact a variable is being used - I am thinking maybe something like a conversion is happening which is causing truncation on the bits, or causing rounding errors, or something similar.


I'd ask what happens if you don't use a variable, but instead feed the numbers in directly, do you get these same numbers?


Anyone able to see a pattern to these values that might give a clue as to what is happening?


Thanks!

Tom

Post by Norm // Oct 31, 2006, 5:42am

Norm
Total Posts: 862
pic
As I understand it, ...Math.sin(30*Math.PI/180) which returns 0.5


iBrent


Most excellent post iBrent. You hit the proverbial nail on the head.


http://msdn2.microsoft.com/en-us/library/system.math.sin.aspx , down in the remarks for parameter a, Microsoft specifies that the angle "a" in Math.sin(a) must be in radians. If you have angle degrees as only number to work with, it first must be converted to radians.


http://math.rice.edu/~pcmi/sphere/drg_txt.html is another good reference place

Post by TomG // Oct 31, 2006, 7:51am

TomG
Total Posts: 3397
But I still dont get it - I still can never get the value 0.745 from sin with an input of 4, whether my input is radians or degrees or anything else....


So I am still confused - I know about radians quite well, having dealt with programming for many years. But I still don't get how 4 turns into 0.745 by any method using sin!


EDIT - please note, sin(4) using radians should give the answer -0.7568, which is not what Mark reports getting. sin(4) in degrees is 0.0697, but Mark is not complaining about not getting 0.0697 (ie that he has expected a sin of 4 degrees and not allowed for it being in radians), the answer he gets for a sin(4) is 0.745, which isn't right for radians either!


HTH,

Tom

Post by Norm // Oct 31, 2006, 1:18pm

Norm
Total Posts: 862
pic
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html might hold a clue for you. Difference between Math. libraries and strictMath. libraries is considered precision. Where stictMath does not round numbers like Math. does. They suggest if you require accuracy, you have to start with accuracy I guess.


Peter might be able to go into it deeper but if you research the scenario you should be able to come up with a page that makes sense to you and you understand.


In the course, time value is used as input for Math.sin/cos/tan etc. All I required was numbers .. nothing accurate in subatomic level stuff :)

Post by mbulman // Nov 3, 2006, 5:15am

mbulman
Total Posts: 10
pic
Thank's you guys

It looks like the B=Math.sin(A*Math.PI/180) was what I was looking for.
Awportals.com is a privately held community resource website dedicated to Active Worlds.
Copyright (c) Mark Randall 2006 - 2020. All Rights Reserved.
Awportals.com   ·   ProLibraries Live   ·   Twitter   ·   LinkedIn