一个接近无限或0的极限将如何计算?

时间:2019-01-21 16:06:21

标签: javascript recursion tail-recursion calculus

在业余时间,我喜欢编写定理。

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

这是一个计算pi的递归调用。我基于this link

我的问题是,如何计算编程极限?此通话将转到无穷大。

那么当x接近无穷大时,计算器或其他编程语言如何计算极限?我会为x设置最大值吗?

2 个答案:

答案 0 :(得分:2)

下面,我们使用looprecur来使您的函数无限重复。而且,我不再使用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))

如您所见,此方法需要很长时间才能收敛到答案。即使经过一千万(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超过3,700万,达到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 ]
  ) 

答案 1 :(得分:1)

就个人而言,我将定义一个合适的公差,然后比较当前值和最后一个值之间的差异。如果差异低于公差,请停止计算,并且您知道结果可以精确到正负公差。

您也可以继续进行计算,直到获得与可能相同的两个值为止,这意味着您将要存储结果的数据类型的精度已达到极限,并且任何进一步的计算都是没有意义的。