Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;数学问题,如果数学都在同一行上,则不返回值_C++_Opencv - Fatal编程技术网

C++ c++;数学问题,如果数学都在同一行上,则不返回值

C++ c++;数学问题,如果数学都在同一行上,则不返回值,c++,opencv,C++,Opencv,首先,感谢您在本网站上提供的所有精彩问答!这个网站是一个很好的资源,可以解决我过去遇到的很多问题,这些问题已经得到了解答。然而,我现在遇到了一个问题,我似乎找不到解决办法 我正和OpenCV合作,尝试制作我的第一个C++程序,我正经历一个奇怪的数学问题。我猜这很简单,我没有看到,我希望有人能解释我做错了什么 问题在下面的行中: 双L1_斜率=(L1P2.y-L1P1.y)/(L1P2.x-L1P1.x) 如果我在同一行上计算,它返回0,但是如果我把它分成3行,它会给我需要的输出 我见过类似的问题

首先,感谢您在本网站上提供的所有精彩问答!这个网站是一个很好的资源,可以解决我过去遇到的很多问题,这些问题已经得到了解答。然而,我现在遇到了一个问题,我似乎找不到解决办法

我正和OpenCV合作,尝试制作我的第一个C++程序,我正经历一个奇怪的数学问题。我猜这很简单,我没有看到,我希望有人能解释我做错了什么

问题在下面的行中: 双L1_斜率=(L1P2.y-L1P1.y)/(L1P2.x-L1P1.x) 如果我在同一行上计算,它返回0,但是如果我把它分成3行,它会给我需要的输出

我见过类似的问题,但它们都涉及整数。 L1P1、L1P2、L2P1、L2P2实际上都是cv::Point–都是整数。。。 但既然我将L1_斜率声明为双精度,我不明白为什么会出现这种情况

有什么想法吗?我知道我可以把数学分解,它会起作用,但我无法想象不能在一行上做这个数学

cv::Point calcIntersection(cv::Point L1P1, cv::Point L1P2, cv::Point L2P1, cv::Point L2P2) {
//calculates the intersection of two lines

std::cout << "\n\nL1P1.x=" << L1P1.x << "\n";
std::cout << "L1P1.y=" << L1P1.y << "\n";
std::cout << "L1P2.x=" << L1P2.x << "\n";
std::cout << "L1P2.y=" << L1P2.y << "\n\n";

double test1 = (L1P2.y - L1P1.y);
double test2 = (L1P2.x - L1P1.x);
double test3 = test1/test2;
std::cout << "test1=" << test1 << "\n";
std::cout << "test2=" << test2 << "\n";
std::cout << "test3=" << test3 << "\n\n";

double L1_Slope = (L1P2.y-L1P1.y) / (L1P2.x-L1P1.x);
std::cout << "L1_Slope=" << L1_Slope << "\n\n";

double L1_Intersect = L1P1.y - L1_Slope * L1P1.x;
double L2_Slope = (L2P2.y - L2P1.y) / (L2P2.x - L2P1.x);
std::cout << "L2_Slope=" << L2_Slope << "\n";

double L2_Intersect = L2P2.y - L2_Slope * L2P2.x;
double intersectionX = (L2_Intersect - L1_Intersect) / (L1_Slope - L2_Slope);
double intersectionY = (L1_Slope * intersectionX + L1_Intersect);
cv::Point intersection = { static_cast<int>(intersectionX), static_cast<int>(intersectionY) };
return intersection;}
这里,
x
y
坐标是整数。表达式的计算结果为:

(345 - 62) / (578 - 111)

<>你会惊讶地发现,C++中,<代码> 283/467 >代码>为0。这是整数除法,以整数形式执行,而不是小数部分。即使最终结果被分配给一个
double
,也为时已晚。首先对部门进行评估。小数部分已被截断,因此最终结果为0

double test1 = (L1P2.y - L1P1.y);
double test2 = (L1P2.x - L1P1.x);
double test3 = test1/test2;
这里,将分子和分母分别存储到
double
变量中,然后除法将两个
double
值分开:

283.0 / 467.0

现在这是一个浮点除法,其结果为.605996

表达式是根据其类型而不是将其结果指定给的变量类型进行计算的

当您单独进行计算并将每个结果分配给
double
变量时,您正在隐式地将子表达式转换为
double
。所以实际上,你得到了

double L1_Slope = static_cast<double>(L1P2.y-L1P1.y) / static_cast<double>(L1P2.x-L1P1.x);
在这种情况下,如果x差值超过y差值,舍入将意味着小于1的分数将变成整数0


要强制数学为双精度,需要确保每个操作中至少有一个操作数是双精度(如有必要,另一个操作数将隐式向上转换)。如您所见,您可以通过将一个或多个子表达式的结果赋给
double
变量,或者通过显式转换为
double
,如本答案的第一段所示。

Change
double L1_Slope=(L1P2.y-L1P1.y)/(L1P2.x-L1P1.x)到<代码>双L1_斜率=(L1P2.y-L1P1.y)/静态_转换(L1P2.x-L1P1.x)所有的数学运算都会正确!下面的Sam Varshavchik是正确的。我做了另一个测试,使用的坐标都声明为double,数学工作在一行中,然后声明为整数,返回0。即使最终结果声明为双精度,这仍然是一个整数数学错误。请不要忘记检查分母是否为0。如果您建议使用
静态\u cast
而不是
(双精度)
double test1 = (L1P2.y - L1P1.y);
double test2 = (L1P2.x - L1P1.x);
double test3 = test1/test2;
283.0 / 467.0
double L1_Slope = static_cast<double>(L1P2.y-L1P1.y) / static_cast<double>(L1P2.x-L1P1.x);
int L1_Slope_int = (L1P2.y-L1P1.y) / (L1P2.x-L1P1.x);
double L1_Slope = L1_Slope_int;