简体   繁体   English

将数学公式转换为 C++

[英]Translate math formula to C++

I need to write a formula to determine the distance between two points in a plane in C++.我需要编写一个公式来确定 C++ 平面中两点之间的距离。 ### Here is the formula: ###这是公式:在此处输入图像描述

I wrote such a code for this, but where is my mistake and how should I write the code correctly?:我为此编写了这样的代码,但是我的错误在哪里,我应该如何正确编写代码?:

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    double x1, x2, y1, y2, distance;
    distance = sqrt((pow(x2-x1), 2) + pow((y2-y1), 2));
    return 0;
}

Under IEEE754 (a floating point standard ubiquitious on desktop personal computers), std::sqrt is required to return the best representable result possible, but std::pow is not.在 IEEE754(桌面个人计算机上普遍存在的浮点标准)下, std::sqrt需要返回可能的最佳可表示结果,但std::pow不是。

So所以

distance = std::sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));

is a better way.是更好的方法。 Make sure that all the domain variables are initialised prior to the evaluation else the behaviour of the program is undefined.确保在评估之前初始化所有域变量,否则程序的行为是未定义的。

In order that your program has observable output, write为了使您的程序具有可观察的 output,请编写

std::cout << distance;

having supplied appropriate values for x1 , x2 , y1 , and y2 .x1x2y1y2提供了适当的值。

The specific example in your question has a several serious errors - among other things you have a syntax error (a parentheses at the wrong place causing the first pow to only have one parameter) and you're using variables that you never initialized - and then not even printing or returning the result...您问题中的具体示例有几个严重的错误-除其他外,您有语法错误(错误位置的括号导致第一个pow只有一个参数)并且您使用的是从未初始化过的变量 - 然后甚至没有打印或返回结果......

With a slight correction, the formula you used is correct:稍作修正,您使用的公式是正确的:

sqrt(pow((x2-x1), 2) + pow((y2-y1), 2))

As Bathsheba noted, if you want to square a number a faster and more accurate alternative to calling pow() is just to do multiplication:正如 Bathsheba 所指出的,如果您想对一个数字求平方,那么调用 pow() 的更快、更准确的替代方法就是进行乘法运算:

sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))

Another alternative is to replace the sqrt and two multiplications by one math function, hypot , designed to do exactly what you need:另一种选择是将sqrt和两个乘法替换为一个数学 function, hypot ,旨在完全满足您的需求:

hypot(x2-x1, y2-y1)

The jury is still out which of the above two options is better.陪审团仍然没有上述两种选择中的哪一种更好。 hypot has the potential of being more accurate and also be able to survive overflows (when the distance squared overflows the floating point limits but the distance itself doesn't), but some report that it is slower than the sqrt-and-multiplication version (see When to use `std::hypot(x,y)` over `std::sqrt(x*x + y*y)` ). hypot具有更准确的潜力并且还能够在溢出中幸存(当距离平方溢出浮点限制但距离本身没有时),但有人报告说它比 sqrt-and-multiplication 版本(请参阅何时使用 `std::hypot(x,y)` 而不是 `std::sqrt(x*x + y*y)` )。

I tested this.我对此进行了测试。 Below code is working fine下面的代码工作正常

#include <iostream>

#include <math.h>

using namespace std;

double calculateDistance(double x1, double x2, double y1, double y2) {
  return sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2));
}

int main() {
  double x1 = 5, x2 = 3, y1 = 5, y2 = 4;
  double distance = calculateDistance(x1, x2, y1, y2);
  cout << distance;
  return 0;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM