Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Math.log错误结果,Visual Studio_Javascript_Visual Studio - Fatal编程技术网

Javascript Math.log错误结果,Visual Studio

Javascript Math.log错误结果,Visual Studio,javascript,visual-studio,Javascript,Visual Studio,我试图让下面的公式起作用,但是我得到了错误的结果,尽管我不认为它有什么问题 用户键入4个数字作为示例: 输入1=100 输入2=75 输入3=30 输入4=170 公式: var results = (Math.log(input1 + input2 - input3) / Math.log(10)) - (Math.log(input4) / Math.log(10)); 结果是:2.6,这是错误的 如果我改变下面的公式,它会起作用 var results = (Math.log(145

我试图让下面的公式起作用,但是我得到了错误的结果,尽管我不认为它有什么问题

用户键入4个数字作为示例:

  • 输入1=100
  • 输入2=75
  • 输入3=30
  • 输入4=170
公式:

var results = (Math.log(input1 + input2 - input3) / Math.log(10)) - (Math.log(input4) / Math.log(10));
结果是:2.6,这是错误的

如果我改变下面的公式,它会起作用

var results = (Math.log(145) / Math.log(10)) - (Math.log(170) / Math.log(10));
结果是:-0.1


为什么呢?我遗漏了什么吗?

听起来你可能有其他原因导致这种不正确的行为。两个代码块产生相同的结果。请参阅链接


没有什么问题

var results = (Math.log(input1 + input2 - input3) / Math.log(10)) - (Math.log(input4) / Math.log(10));
问题就在这里:

var input1 = document.getElementById("one").value;
var input2 = document.getElementById("two").value;
var input3 = document.getElementById("three").value;
var input4 = document.getElementById("four").value;
用户给出的数字被视为字符串,因此当程序运行上述公式时,将产生以下结果:

var results = (Math.log(10075 - 30) / Math.log(10)) - (Math.log(170) / Math.log(10));
因为当您在Javascript中添加两个字符串时,例如1+2,它将为您提供1+2

因此,100+75=10075

为了修复它,我在
document.getElementById
前面添加了
+

var input1 = +document.getElementById("one").value;
var input2 = +document.getElementById("two").value;
var input3 = +document.getElementById("three").value;
var input4 = +document.getElementById("four").value;
var input1 = +document.getElementById("one").value;
var input2 = +document.getElementById("two").value;
var input3 = +document.getElementById("three").value;
var input4 = +document.getElementById("four").value;