相关疑难解决方法(0)

获得π值的最快方法是什么?

我正在寻找获得π值的最快方法,作为个人挑战.更具体地说,我使用的方法不涉及使用#define常量M_PI,或者对数字进行硬编码.

下面的程序测试了我所知道的各种方式.从理论上讲,内联汇编版本是最快的选择,但显然不便于携带.我已将其作为基线与其他版本进行比较.在我的测试中,使用内置4 * atan(1)函数,在GCC 4.2上版本最快,因为它会自动将其折叠atan(1)为常量.根据-fno-builtin指定,atan2(0, -1)版本最快.

这是主要的测试程序(pitimes.c):

#include <math.h>
#include <stdio.h>
#include <time.h>

#define ITERS 10000000
#define TESTWITH(x) {                                                       \
    diff = 0.0;                                                             \
    time1 = clock();                                                        \
    for (i = 0; i < ITERS; ++i)                                             \
        diff += (x) - M_PI;                                                 \
    time2 = clock();                                                        \
    printf("%s\t=> %e, time => %f\n", #x, diff, diffclock(time2, time1));   \
}

static inline double
diffclock(clock_t time1, clock_t time0)
{ …
Run Code Online (Sandbox Code Playgroud)

language-agnostic unix algorithm performance pi

315
推荐指数
21
解决办法
5万
查看次数

运行时异常,递归太深

我转换的伪代码这里到C#,并让它递归地重复10000次.但是我StackOverflow Exception经历了一次C#运行时错误9217.我怎么能阻止这个?

编辑如果它对任何人有帮助,这里是代码:

    private double CalculatePi(int maxRecursion)
    {
        return 2 * CalculatePi(maxRecursion, 1);
    }

    private double CalculatePi(int maxRecursion, int i)
    {
        if (i >= maxRecursion)
            return 1;
        return 1 + i / (2.0 * i + 1) * CalculatePi(maxRecursion, i + 1);
    }

    double pi = CalculatePi(10000); // 10,000 recursions
Run Code Online (Sandbox Code Playgroud)

EDIT2所以每个人似乎都同意我需要将其转换为迭代...任何人都可以提供一些代码吗?我似乎无法编写任何有效的迭代代码......

编辑感谢Paul Rieck的这个答案,我测试了它,它的工作原理如下:

    private static double CalculatePi(int maxRecursion)
    {
        double result = 1;
        for (int i = maxRecursion; i >= 1; …
Run Code Online (Sandbox Code Playgroud)

c# recursion exception

8
推荐指数
2
解决办法
4675
查看次数

以下Java代码如何计算Pi的数字?

以下代码使用哪种算法/公式?

    /**
 * Computes the nth digit of Pi in base-16.
 * 
 * If n < 0, return -1.
 * 
 * @param n The digit of Pi to retrieve in base-16.
 * @return The nth digit of Pi in base-16.
 */
public static int piDigit(int n) {
    if (n < 0) return -1;

    n -= 1;
    double x = 4 * piTerm(1, n) - 2 * piTerm(4, n) -
               piTerm(5, n) - piTerm(6, n);
    x = x - Math.floor(x);

    return …
Run Code Online (Sandbox Code Playgroud)

java pi

4
推荐指数
1
解决办法
4895
查看次数

如何在 Pi 的 C# 程序中获得小数到 n 位精度

用 reg 这个问题 Pi in C#

我编写了下面的代码并给出了最后 6 位为 0 的输出。所以我想通过将所有内容转换为十进制来改进程序。我以前从未在 C# 中使用过十进制而不是双精度数,而且我只在常规使用中对双精度数感到满意。

所以请帮助我进行十进制转换,我尝试在开始时将所有双精度替换为十进制,但效果不佳:(。

 using System;

class Program
{
    static void Main()
    {
    Console.WriteLine(" Get PI from methods shown here");
    double d = PI();
    Console.WriteLine("{0:N20}",
        d);

    Console.WriteLine(" Get PI from the .NET Math class constant");
    double d2 = Math.PI;
    Console.WriteLine("{0:N20}",
        d2);
    }

    static double PI()
    {
    // Returns PI
    return 2 * F(1);
    }

    static double F(int i)
    {
    // Receives the call number
   //To avoid so error
    if (i …
Run Code Online (Sandbox Code Playgroud)

c# precision pi decimal

3
推荐指数
1
解决办法
2481
查看次数