Are there two sine and cosine ratios?

krahny

New Member
Messages
25
Reaction score
0
Points
0
When I enter sin(0) in a calculator, it gives me 0, and sin(90) gives me 1, just like I'd expect it to. But, when I use javascripts Math.sin() and Math.cos() functions, they give me completely different results. I think they might have something to do with pi, instead of an angle.

Could someone please help me?

Thanks.
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
In Javascript (and PHP) the input for the sin functions are in radians rather than degrees. A radian is just a different way of measuring angles and is linked to circular calculations. There are 2pi radians in a complete circle, so 360˚ = 2pi radians and 180˚ = pi radians. The simplest way to convert one to the other is like so:
Code:
If you intend to do this:
Math.sin(number);
Change it to this:
Math.sin((Math.PI * number)/180);
Also remember that doing inverse sin functions will give you an answer in radians that you may then want to convert to degrees.
 
Last edited:

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
lemon-tree: This is actually true for calculators too. Their default setting is Radian, however most schools make kids change the setting to Degree so they don't run into this problem. :p
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
Wasn't the default on my lump of a calculator, at least I don't think it was. Don't fancy resetting it to test and lose the data on it.
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
My first calculator:

hp35.jpg
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
Retro :)
Anyway, krahny is this working for you now? If you need any further explanation of the the implementation or the theory then one of us will be happy to help.
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
@descalzo: What's that, a 45 maybe? I have no idea how old you are, but I remember paying more for my first HP than I paid for my first (used) car. Never could get the hang of RPN -- I became a TI man by '77 (the TI 59 was another half-a-grand machine when I bought it). And, to get back to the question at hand, it defaulted to degree mode as well.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
HP 35. Originally ('72) cost $395 (about $2000 in today's dollars). Was at U.C. Berzerkly.

Considering the laptop/notepad you can get today for $395 it is mind boggling.
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
Ah, a fellow fogie. Good to know I'm not the only one. (And I'd bet we're the only ones here who remember Al Capp's characters. Schmoo's cool, but I was into Joe Btfsplk -- he kinda reminded me of me. And as a Canadian, often standing waist-deep or deeper in the snow, I had a deep feeling of kinship with the good people of Lower Slobovia.)
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
Use Math.asin(), Math.acos() and Math.atan(). The returned values will be in radians and will have the following ranges:
Math.asin: -π/2 to π/2
Math.acos: 0 to π
Math.atan: -π/2 to π/2
Also remember that for Math.asin() and Math.acos() the domain is from -1 to 1, so inputting any number outside of this will return an error or NaN.
 
Last edited:

krahny

New Member
Messages
25
Reaction score
0
Points
0
Thanks, i had tried that, but something's screwy.

I have some actionscript code(which is almost exactly the same as javascript) that is supposed to use two values(determined by the position of the mouse) to make an arrow pointing to a direction and extend to a certain length, but I think there's an error in the section that changes its direction.

Code:
//this part determins the length of the arrow, it works

arrowLength=Math.round(Math.sqrt(StartVForce*StartVForce+StartHForce*StartHForce))

//this makes sure it doesn't get too long, also working

if(arrowLength>50){
arrowLength=50
}

//this is the part that's screwy, i don't know why

arrowAngle=Math.asin((Math.PI*(StartVForce/arrowLength))/180)

//these change the length and rotation of the arrow

DArrow.gotoAndStop(arrowLength+1)
DArrow._rotation=arrowAngle
 
Last edited:

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
You've got the radians conversion in the wrong place, that's why. This following line will return in degrees, but as I said above you may get a negative number that you'll have to code for if (StartVForce/arrowLength) is ever negative.
Code:
arrowAngle = (180 * Math.asin(StartVForce/arrowLength))/Math.PI; //Returns in degrees
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
A design note: breaking the overall task down in terms of subtasks and defining a few helper functions should help prevent logic errors, as it can make things more readable and certainly makes them more testable (you can unit test the subtasks to ensure they do what they're supposed to).
Code:
function dot(u, v) {
    return Math.sqrt(u.x*v.x + u.y*v.y);
}
function length(v) {
    return dot(v, v);
}
function rad2deg(theta) {
    return 180 * theta / Math.PI;
}
function deg2rad(theta) {
    return Math.PI * theta / 180;
}
function angleInRadians(v) {
    // tan(theta) == y / x, which is why atan2 takes (y,x)
    return Math.atan2(v.y, v.x);
}
function angleInDegrees(v) {
    return rad2deg(angleInRadians(v));
}


var StartForce = {x: StartHForce, y: StartVForce};
var arrow = {
  length: Math.min(length(StartForce), 50),
  angle: angleInDegrees(StartForce)
};

DArrow.gotoAndStop(arrow.length+1);
DArrow._rotation=arrow.angle;

To increase cohesion, the operations and construction of should be made part of a class. The simplest way of getting one is to extend the Point class.
Code:
class Vector2D extends flash.geom.Point {
    public function Point(x:Number = 0, y:Number = 0) {
        super(x, y);
    }
    public function get radians() {
        return Math.atan2(this.y, this.x);
    }
    public function get degrees() {
        return this.radians * 180 / Math.Pi;
    }
}


var force = new Vector2D(StartHForce, StartVForce);
var arrowLength = Math.min(force.length, 50);

DArrow.gotoAndStop(arrowLength+1);
// or skip arrowLength and instead use the less readable:
//DArrow.gotoAndStop(Math.min(force.length, 50)+1);
DArrow._rotation=force.degrees;

If you want to be able to set the length or angle, add a few setters. The code to manipulate the arrow then becomes even more straightforward and simple:
Code:
var force = new Vector2D(StartHForce, StartVForce);

force.length = Math.min(force.length, 50)+1;
DArrow.gotoAndStop(force.length);
DArrow._rotation=force.degrees;
 
Last edited:
Top