JavaScript sin函数问题 [英] Javascript sin function issue

查看:90
本文介绍了JavaScript sin函数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Math.sin有问题.我认为它将输出给定整数的正弦曲线.所以我尝试了Math.sin(30),我的输出是 -0.9880316240928618 ,然后我用计算器进行了检查,结果是0.5

I have a problem with Math.sin. I thought it would output the sinus of the given integer. So I tried Math.sin(30) and my output was -0.9880316240928618 and then I checked with my calculator and it was 0.5.

推荐答案

假定参数以弧度而不是度为单位.

Parameters are assumed to be in radians, not degrees.

尝试

Math.sin(Math.PI * (30/180));

以下评论指出,预先计算比值π/180是个好主意.可以向Math.sin添加一个可以以这种方式在度上工作的同伴:

A comment below notes that pre-computing the ratio π/180 is a good idea. One could add a companion to Math.sin that works on degrees this way:

Math.dsin = function() {
  var piRatio = Math.PI / 180;
  return function dsin(degrees) {
    return Math.sin(degrees * piRatio);
  };
}();

(有些人不喜欢扩展内置对象,但由于至少没有实例化Math实例-m,所以我并不–这似乎并不令人反感.)

(Some people don't like extending built-in objects, but since one doesn't instantiate Math instances — at least, I don't — this doesn't seem terribly offensive.)

这篇关于JavaScript sin函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆