[英]Decimal.js sin() function returns incorrect result
I have a javascript program that looks like this:我有一个 javascript 程序,如下所示:
function dosine(){
var num = new Decimal(document.getElementById('num').value);
if(document.getElementById('degrad').value == "degrees"){
num = (num*Math.PI)/180;
console.log(num);
}
num = Decimal.sin(num);
console.log(num.toString());
numinverse = Decimal.asin(num);
if(document.getElementById('degrad').value == "degrees"){
num = num * (180/Math.PI);
numinverse = numinverse * (180/Math.PI);
}
document.getElementById('resultsine').innerHTML = "Sine: " + num.toString();
document.getElementById('resultinverse').innerHTML = "Inverse Sine: " + numinverse.toString();
}
In my program, I am now using Degrees.sin and Degrees.asin because of floating-point weirdness with the Math library, but when I get the sin output for 64 I get 51.49710550442818, but on my physical calculator I get 0.920026038197 0.8987940463.在我的程序中,由于数学库的浮点怪异,我现在使用 Degrees.sin 和 Degrees.asin,但是当我得到 64 的 sin output 时,我得到 51.49710550442818,但在我的物理计算器上我得到
0.920026038197 0.8987940463。 Am I using this library wrong, or is my code just not good?我是不是用错了这个库,还是我的代码不好? I am pretty new to javascript so advice would be very much appreciated.
我是 javascript 的新手,所以非常感谢您的建议。 Thanks!
谢谢!
This has nothing to do with Decimal
.这与
Decimal
无关。 You convert num
to radians, then you take a sine of it.您将
num
转换为弧度,然后取其正弦值。 Finally you convert the result of the sine (which should be a proportion from 0 to 1, not an angle,) from radians to degrees.最后,将正弦的结果(应该是从 0 到 1 的比例,而不是角度)从弧度转换为度。 which makes no sense - it's like converting weight from feet into metres.
这没有任何意义——这就像将重量从英尺转换为米。
This could be avoided by using better naming conventions, using variable names that make sure you know what the variable contains.这可以通过使用更好的命名约定,使用确保您知道变量包含什么的变量名称来避免。
num
is not very semantic - all it tells you is that it has a number in it. num
不是很语义化——它只告诉你它有一个数字。 Consider angle_in_degrees
, angle_in_radians
and sine
.考虑
angle_in_degrees
、 angle_in_radians
和sine
。 Then it would be immediately obvious that this is not what you want:那么很明显这不是你想要的:
angle_in_radians = Decimal.sin(angle_in_radians) // result is a sine ratio, not an angle!
angle_in_degrees = angle_in_radians * (180 / Math.PI); // good operation on bad data
Another big point is that your code does not stay in Decimal
.另一个重点是您的代码不会保留在
Decimal
中。 JavaScript cannot override the default operations, so you have to use Decimal
methods to calculate, not +
, *
and /
. JavaScript 无法覆盖默认操作,因此您必须使用
Decimal
方法计算,而不是+
、 *
和/
。 Note this:请注意:
Decimal(1) + 3 // incorrect
// => "13"
Decimal(1).add(3).toNumber() // correct
// => 4
Finally, unless you are dealing with financial systems, the floating point error is usually negligible;最后,除非您处理的是金融系统,否则浮点误差通常可以忽略不计; moreover, the result of sine function is irrational, so it can't be represented in
Decimal
any more correctly than in floating point anyway.此外,正弦 function 的结果是无理数,因此它不能用
Decimal
表示比用浮点数更正确。 Unless you have a use case that makes Decimal.js
necessary, just use the normal numbers.除非您有需要
Decimal.js
的用例,否则只需使用普通数字即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.