在空闲时间,我喜欢编写定理程序。
const PI = 3.141592653589793; // Math.PI
function CalculatePi(total = 0, x = 1, addSubBool = false) {
if (addSubBool) {
total -= (4 / x)
console.log(`${total}\tDifference: ${PI - total}`);
} else {
total += (4 / x)
console.log(`${total}\tDifference: ${total - PI}`);
}
if (total !== PI) {
setTimeout(function() {
CalculatePi(total, x + 2, !addSubBool);
}, 100);
}
}
CalculatePi();
这是一个计算圆周率的递归调用。我以this link为基础
我的问题是,我如何在编程中计算极限?这个调用将会是无限的。
那么,当x接近无穷大时,计算器或其他编程语言如何计算极限呢?我会为x设置一个最大值吗?
发布于 2019-01-22 00:28:06
下面我们使用loop和recur来让你的函数无限循环。我没有使用setTimeout,而是尽可能快地递归,而是每隔1000 x输出一个结果-
const recur = (...values) =>
({ recur, values })
const loop = f =>
{ let acc = f ()
while (acc && acc.recur === recur)
acc = f (...acc.values)
return acc
}
const calculatePi = (limit = Infinity) =>
loop // loop our function ...
( (total = 0, x = 1, addSubBool = false) =>
{ if (x > limit) // stop condition
return total
if (x % 1e3 === 1) // display progress as we go
console.log(x, total, total - Math.PI)
if (total === Math.PI) // solution found
return total
if (addSubBool)
return recur // recur with ...
( total - 4 / x // next total
, x + 2 // next x
, false // next addSubBool
)
else
return recur // recur with ...
( total + 4 / x // next total
, x + 2 // next x
, true // next addSubBool
)
}
)
console.log(calculatePi(1e7))
正如您所看到的,这种方法需要很长时间才能收敛到答案。即使在1000万(10M) x之后,我们仍然只计算了6个点的精度-
x total diff
...
9997001 3.1415924535297624 -2.0006003076389334e-7
9998001 3.1415924535497695 -2.0004002365681117e-7
9999001 3.141592453569776 -2.0002001699381822e-7另一种方法是将precision作为calculatePi的输入。我们将继续计算,直到达到特定的精度,而不是受到某些任意x的限制。出于演示目的,此函数还返回x,因此我们可以看到在达到所需的精度之前,x必须达到多大的精度-
const calculatePi = (precision = 1e5) =>
loop
( (total = 0, x = 1, addSubBool = false) =>
{ if (total * precision >> 0 === Math.PI * precision >> 0)
return [ total, x ]
if (addSubBool)
return recur
( total - 4 / x
, x + 2
, false
)
else
return recur
( total + 4 / x
, x + 2
, true
)
}
)如你所见,x超过了3700万,达到了7位小数精度-
console .log
( calculatePi (1e2)
// [ 3.14999586659347, 239 ]
, calculatePi (1e3)
// [ 3.141000236580159, 3377 ]
, calculatePi (1e4)
// [ 3.1415000095284658, 21589 ]
, calculatePi (1e5)
// [ 3.141599999994786, 272243 ]
, calculatePi (1e7)
// [ 3.1415926000000005, 37320609 ]
)展开下面的代码片段,在浏览器中验证结果-
const recur = (...values) =>
({ recur, values })
const loop = f =>
{ let acc = f ()
while (acc && acc.recur === recur)
acc = f (...acc.values)
return acc
}
const calculatePi = (precision = 1e5) =>
loop
( (total = 0, x = 1, addSubBool = false) =>
{ if (total * precision >> 0 === Math.PI * precision >> 0)
return [ total, x ]
if (addSubBool)
return recur
( total - 4 / x
, x + 2
, false
)
else
return recur
( total + 4 / x
, x + 2
, true
)
}
)
console .log
( calculatePi (1e2)
// [ 3.14999586659347, 239 ]
, calculatePi (1e3)
// [ 3.141000236580159, 3377 ]
, calculatePi (1e4)
// [ 3.1415000095284658, 21589 ]
, calculatePi (1e5)
// [ 3.141599999994786, 272243 ]
, calculatePi (1e7)
// [ 3.1415926000000005, 37320609 ]
)
最后,在计算pi时检查Math.PI没有多大意义;我认为整个目标是计算一个我们假装不知道的数字。为此,我们从一些guess开始,然后测量它与total之间的差异。如果猜测在指定的容差内,则返回猜测-
const calculatePi = (precision = 1e5) =>
loop
// guess starts at 1
( (guess = 1, total = 0, x = 1, addSubBool = false) =>
{ if (Math .abs (guess - total) * precision < 1)
return [ guess, x ]
if (addSubBool)
return recur // recur with ...
( total // next guess
, total - 4 / x // next total
, x + 2 // next x
, false // next addSubBool
)
else
return recur // recur with ...
( total // next guess
, total + 4 / x // next total
, x + 2 // next x
, true // next addSubBool
)
}
)我们可以看到它像预期的那样工作。诚然,我对输入精度和计算它所需的x之间的相关性感到惊讶-
console .log
( calculatePi (1e2)
// [ 3.136592684838816, 403 ]
, calculatePi (1e3)
// [ 3.1410926536210413, 4003 ]
, calculatePi (1e4)
// [ 3.1415426535898248, 40003 ]
, calculatePi (1e5)
// [ 3.1415876535897618, 400003 ]
, calculatePi (1e7)
// [ 3.141592603589817, 40000003 ]
)展开下面的代码片段,在浏览器中验证结果-
const recur = (...values) =>
({ recur, values })
const loop = f =>
{ let acc = f ()
while (acc && acc.recur === recur)
acc = f (...acc.values)
return acc
}
const calculatePi = (precision = 1e5) =>
loop
// guess starts at 1
( (guess = 1, total = 0, x = 1, addSubBool = false) =>
{ if (Math .abs (guess - total) * precision < 1)
return [ guess, x ]
if (addSubBool)
return recur // recur with ...
( total // next guess
, total - 4 / x // next total
, x + 2 // next x
, false // next addSubBool
)
else
return recur // recur with ...
( total // next guess
, total + 4 / x // next total
, x + 2 // next x
, true // next addSubBool
)
}
)
console .log
( calculatePi (1e2)
// [ 3.136592684838816, 403 ]
, calculatePi (1e3)
// [ 3.1410926536210413, 4003 ]
, calculatePi (1e4)
// [ 3.1415426535898248, 40003 ]
, calculatePi (1e5)
// [ 3.1415876535897618, 400003 ]
, calculatePi (1e7)
// [ 3.141592603589817, 40000003 ]
)
发布于 2019-01-22 00:16:03
就我个人而言,我会定义一个合适的容差,然后比较当前值和上一个值之间的差异。如果差值低于容差,停止计算,您就知道结果是准确的正负容差。
您也可以继续计算,直到得到两个相同的值,这可能意味着您已经达到了存储结果的数据类型的精度限制,任何进一步的计算都是没有意义的。
https://stackoverflow.com/questions/54293708
复制相似问题