简体   繁体   English

为什么以“0”和“3”开头大约有区别

[英]Why is there a difference between starting with a "0" and "3" for approx

I was attempting an approximated value of pi through the formula of pi= 3 + (4/(2*3*4)) - (4/(4*5*6)) + (4/(6*7*8)) - … (and so on).我试图通过pi= 3 + (4/(2*3*4)) - (4/(4*5*6)) + (4/(6*7*8)) - … (等等)。 However, my code (shown below) had 2 separate answers (3.1415926535900383 and 3.141592653590042) when:但是,我的代码(如下所示)在以下情况下有 2 个单独的答案(3.1415926535900383 和 3.141592653590042):

  1. approx variable started with "0" and "3" respectively approx 变量分别以“0”和“3”开头
  2. n=10000 n=10000

Does anyone know why?有谁知道为什么?

def approximate_pi(n):
    approx=0
    deno=2
    if n == 1:
        return 3
    for x in range(n-1):
        if x%2:
            approx -= 4/((deno)*(deno+1)*(deno+2))
        else:
            approx += 4/((deno)*(deno+1)*(deno+2))
        deno+=2
    return approx+3

and

def approximate_pi(n):
    approx=3
    deno=2
    if n == 1:
        return 3
    for x in range(n-1):
        if x%2:
            approx -= 4/((deno)*(deno+1)*(deno+2))
        else:
            approx += 4/((deno)*(deno+1)*(deno+2))
        deno+=2
    return approx

I think is because you can't have exact float numbers in pc.我认为是因为在 pc 中你不能有精确的浮点数。 More info you can get here: Why can't decimal numbers be represented exactly in binary?您可以在此处获得更多信息: 为什么十进制数不能用二进制精确表示?

It is because of the way float is in python.这是因为 float 在 python 中的方式。 If you do not have the digit before the decimal it gives 3 extra precision digits(from my trial).如果您没有小数点前的数字,它会提供 3 个额外的精度数字(来自我的试验)。 This changes the answer because when you start with 0, you get a different calculation altogether.这会改变答案,因为当您从 0 开始时,您会得到完全不同的计算。

An approximation algorithm approximates.近似算法近似。 Neither number is the true value of π.这两个数字都不是 π 的真实值。 Your two versions start at different starting points, so why are you surprised that they give you slightly different approximations?您的两个版本从不同的起点开始,那么为什么您对它们给出的近似值略有不同感到惊讶? What matters is that the longer you run them, the further they will both converge to the true value.重要的是,你运行它们的时间越长,它们就会越接近真实值。

Note that this is not an artifact of the finite-precision representation of floats.请注意,不是浮点数的有限精度表示的人工制品。 While floating point rounding will affect your results, you would see differences even with unlimited precision arithmetic.虽然浮点舍入会影响您的结果,但即使使用无限精度算术,您也会看到差异。

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

相关问题 spark函数中approxCountDsitinct和approx_count_distinct的区别 - Difference between approxCountDsitinct and approx_count_distinct in spark functions 为什么`0--3//2`和`--3//2`之间有区别? - Why is there a difference between `0--3//2` and `--3//2`? gunicorn和以编程方式启动wsgi服务器之间有什么区别? - What's the difference between gunicorn and starting an wsgi server programmatically? 通过`firebase`和`gcloud`启动firestore模拟器的区别? - Difference between starting firestore emulator through `firebase` and `gcloud`? 为什么以下两行代码之间存在差异? - Why is there a difference between the following two lines of code? 为什么嵌套循环的顺序之间存在性能差异? - Why is there a performance difference between the order of a nested loop? 为什么多线程和单线程之间没有执行时间差异 - Why is there no execution time difference between multithreading and singlethreading numpy:为什么(x,1)和(x,)维度之间存在差异 - numpy: Why is there a difference between (x,1) and (x, ) dimensionality 为什么 `|` 和 `or` 之间的性能差异对于不同的值不同? - Why the performance difference between `|` and `or` different for different values? 为什么 n1 和 n2 之间存在差异? - Why there is a difference between n1 and n2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM