2

我正在研究一些 JavaScript,但遇到了数学问题:

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)));

返回 1.203359304667218

但是当我在我的计算器上进行相同的计算时,它会返回 1,这是我期望这个计算返回的结果。谁能解释一下?

4

3 回答 3

3

你用计算器在什么模式下工作?

弧度还是度数?猜猜这可能是您的“问题”,可以轻松解决:-)

于 2012-08-21T05:46:57.957 回答
3

这些总是有用的,并且可能会避免将来的混淆:

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); 
}
于 2012-08-21T06:33:31.467 回答
2
var degreesToRadians = function (d) {
  return Math.PI * d / 180; 
}
于 2012-08-21T05:49:26.433 回答