Luka
Luka

Reputation: 341

JavaScript trigonometric

I'm working on some JavaScript and I've run across a problem with the math:

var p1 = {x:0,y:0}, p2 = {x:1,y:1};
return Math.sin(45) * Math.sqrt((Math.pow(p2.x-p1.x,2) + Math.pow(p2.y-p1.y,2)));

Returns 1.203359304667218

But when I do the same calculation on my calculator, it returns 1 which is what I expect this calculation to return. Can anyone explain this?

Upvotes: 2

Views: 1076

Answers (3)

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38052

These are always useful and will likely avoid future confusion:

function sind(d) {
  return Math.sin(Math.PI*d/180.0); 
}

function cosd(d) {
  return Math.cos(Math.PI*d/180.0); 
}

function tand(d) {
  return Math.tan(Math.PI*d/180.0); 
}

Upvotes: 3

Danil Speransky
Danil Speransky

Reputation: 30473

var degreesToRadians = function (d) {
  return Math.PI * d / 180; 
}

Upvotes: 2

Stefan
Stefan

Reputation: 2613

What mode did you work on with calculator?

Radians or Degrees? Guess that could be your "problem" which may be solved easily :-)

Upvotes: 3

Related Questions