简体   繁体   中英

JavaScript Math Expression Yields Different Results

The following is a line I have in my Javascript code. It outputs -5108024 and some change when sqftVal = 2828 and bathsVal = 3.5 .

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. I get the same result in R . Why does JavaScript mess up the math, and what can I do to fix this?

Calculator/R input: -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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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