简体   繁体   English

JavaScript 数学表达式产生不同的结果

[英]JavaScript Math Expression Yields Different Results

The following is a line I have in my Javascript code.以下是我的 Javascript 代码中的一行。 It outputs -5108024 and some change when sqftVal = 2828 and bathsVal = 3.5 .sqftVal = 2828bathsVal = 3.5时,它输出 -5108024 和一些变化。

out.value = -6932000 + 221400 * Math.log(sqftVal) + 637.2*Math.exp(bathsVal) + 51640; 

However, when I manually type this in my calculator, I get roughly -5099721 and some change.但是,当我在计算器中手动输入时,我得到大约 -5099721 和一些变化。 I get the same result in R .我在R得到相同的结果。 Why does JavaScript mess up the math, and what can I do to fix this?为什么 JavaScript 搞砸了数学,我该怎么做才能解决这个问题?

Calculator/R input: -6932000 + 221400 * ln(2828) + 637.2 * e^(3.5) + 51640 = -5099721.073计算器/R 输入: -6932000 + 221400 * ln(2828) + 637.2 * e^(3.5) + 51640 = -5099721.073

I don't believe this is a floating point error because as I add more terms, the difference becomes fairly large.我不相信这是一个浮点错误,因为当我添加更多项时,差异变得相当大。

Plus, everything was matching up until I added the fourth term ( +51640 ) which made no sense to me.另外,在我添加第四个术语( +51640 )之前,一切都匹配,这对我来说毫无意义。

There must be some other code that is interfering with your values or something, because the code shown does not produce the value you report.一定有其他代码干扰了您的值或其他内容,因为显示的代码不会产生您报告的值。

 var sqftVal = 2828; var bathsVal = 3.5; var value = -6932000 + 221400 * Math.log(sqftVal) + 637.2*Math.exp(bathsVal) + 51640; console.log(value);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM