【问题标题】:Distance between two points without using the square root不使用平方根的两点之间的距离
【发布时间】:2013-05-03 19:03:25
【问题描述】:

是否可以计算两点之间的距离而不必使用 math.h 库?我知道,使用 math.h 库,它必须在这些行中(欧几里得距离公式):

int Distance(int x1, int y1, int x2, int y2)

    {
    int dx = x2 - x1;
    int dy = y2 - y1;
    return sqrt(dx*dx + dy*dy);
    }

但是,有没有办法在不使用平方根(需要 math.h 库)的情况下做同样的事情?

编辑:每当我尝试以下代码时,都会出现浮点异常(核心转储):

float sqrt(int x) {
        int i;
        float s;
        s=((x/2)+x/(x/2)) / 2; /*first guess*/
        for(i=1;i<=4;i++) { /*average of guesses*/
            s=(s+x/s)/2;
        }
        return s;
    }

float Distance(float x1, float y1, float x2, float y2) {
    float dx = x2 - x1;
    float dy = y2 - y1;
    return sqrt(dx*dx + dy*dy);
}

int main() {
  printf("%f", Distance(1, 2, 2, 1));
  return 0;
}

【问题讨论】:

  • 当然有,但你基本上是从数学库中复制代码。
  • 请注意,math.h 是头文件,而不是库,并且数学函数是标准 C 库的一部分。 (很多年前,当系统更小,内存是一个真正可以用完的东西时,数学函数被分解成一个单独的库,如果你不需要它可以不链接。)跨度>

标签: c distance points square-root


【解决方案1】:
int int_sqrt(int x){
    int s, t;

    s = 1;  t = x;
    while (s < t) {
        s <<= 1;
        t >>= 1;
    }//decide the value of the first tentative

    do {
        t = s;
        s = (x / s + s) >> 1;//x1=(N / x0 + x0)/2 : recurrence formula
    } while (s < t);

    return t;
}

【讨论】:

  • N、x1、x0从何而来?
【解决方案2】:

这应该可行。试试看。

float sqrt(int x) {
    int i;
    float s;
    s=((x/2)+x/(x/2)) / 2; /*first guess*/
    for(i=1;i<=4;i++) { /*average of guesses*/
        s=(s+x/s)/2;
    }
    return s;
}

【讨论】:

  • 感谢您的准确回答!但是,每当我尝试以下代码时,它都会给我浮动异常(核心转储):(编辑后)
  • 嗯,我不确定。你的调试器有提示吗?
  • x=0x=1调用函数会导致s=((x/2)+x/(x/2))被0除。
【解决方案3】:

您可以使用巴比伦方法计算平方根。该方法使用逐次逼近来计算平方根。

这是它的工作原理

假设您要计算 1234 的 sqrt。

令 S = 1234,

D 是 S 中的位数,即 = 4。

如果 D 是偶数,我们将其表示为 D = 2n+2 否则如果 D 是奇数 D = 2n + 1;

这里的 D 甚至是 4 = 2*1+2,所以 n=1。

Sapprox 的近似平方根 = D * 10^n = 4 * 10^1 = 40

我们称它为 X0 = Sapprox = 40。

X0 是第 0 个近似值。

由于 S 有 4 位数字,因此您必须再计算 3 个近似值,而 X3 将是 S 的正确平方根。

所以 X1 = 0.5(X0 + S/X0);
X1 = 0.5(40 + 1234/40) = 35.425

X2 = 0.5(X1 + S/X1); X2 = 0.5(35.42 + 1234/35.42) = 35.129

X3 = 0.5(X2 + S/X2); X3 = 0.5(35.129 + 1234/35.129) = 35.128

sqrt(1234) = 35.128

【讨论】:

    【解决方案4】:

    网格上的距离​​计算通常使用涉及平方根计算的公式。实际上,在不调用作为标准 C 库一部分的 sqrt() 函数的情况下计算平方根的唯一方法是重新实现它,很糟糕。

    你为什么要这样做? (或者你是在问,“我怎么能不计算平方根呢?”这不再是一个编程问题。)

    【讨论】:

      【解决方案5】:

      看妈妈!没有&lt;math.h&gt;(但仍需要与libm链接)

      #include <complex.h>
      #include <stdio.h>
      
      double distance(double x0, double y0, double x1, double y1) {
          return cabs((x0 + I*y0) - (x1 + I*y1));
      }
      
      int main(void) {
          printf("==> %7.2f\n", distance(1, 2, 2, 1));
          printf("==> %7.2f\n", distance(1, 0, 4, 0));
          printf("==> %7.2f\n", distance(1, 1, 4, 4));
      }
      

      【讨论】:

      • 当然,cabs() 将使用sqrt() 的一些变体来实现...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      • 2012-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多