繁体   English   中英

math.sqrt返回0

[英]math.sqrt returns 0

我有一个ac#函数,其中包含一些公式来计算某些点的欧式距离。 我得到了由R(rx,ry)和L(lx,ly)定义的点的位置。

首先,我尝试编写如下代码:

double dRightLeft = Math.Sqrt((Math.Pow(rx - lx, 2) + Math.Pow(ry - ly, 2)));

它返回0.0。

然后我尝试拆分变量以检查我在哪里做错了,像这样:

 double rl = (Math.Pow(rx - lx, 2) + Math.Pow(ry - ly, 2));
 double dRightLeft = Math.Sqrt(rl);

rl变量返回其操作的有效值。 但是,当我尝试从中取平方根时,dRighLeft变量仍返回0.0。 我像这样尝试分配和未分配的dRightLeft:

//assigned
dRightLeft = 0;
//unassigned
dRightLeft;

它们都仍然返回0.0值。

这是我简短但完整的程序,其中我得到了rx,ry,lx和ly值:

public Bitmap getDetectedImage()
{
int rx, rx, lx, ly, ...;
double dRightLeft = 0;
...
//righteyeloop
for (int x = fo.rightEye.X; x < (fo.rightEye.X + fo.rightEye.Width); x++)
{
    for (int y = fo.rightEye.Y; y < (fo.rightEye.Y + fo.rightEye.Height); y++)
    {    //segmentation...//   
         rPixel++;
         result.byteImage[x, y].R = 0;
         result.byteImage[x, y].G = 255;
         result.byteImage[x, y].B = 0;
//to get the the first pixel detected//
         if (rPixel == 1)
         {
             result.byteImage[x, y].R = 255;
             result.byteImage[x, y].G = 0;
             result.byteImage[x, y].B = 0;

rx = x + (fo.rightEye.Width / setting.featureWidth * setting.eyeHeight / setting.eyeWidth);
ry = y + (fo.rightEye.Height / setting.featureWidth * setting.eyeHeight / setting.eyeWidth);
         }
    }
}
//lefteyeloop basically the same type as righteyeloop//
.....
//this to count the distance of righteye and lefteye
    double rl = ((rx - lx) * (rx - lx) + (ry - ly) * (ry - ly));
    double dRightLeft = Math.Pow(rl, 0.5);
}

我怀疑问题在于Math.Pow处理精度较低的double (请参阅此SO问题以获取更多讨论) 直觉是通过写出乘法来替换Math.Pow:

double rl = ((rx - lx) * (rx - lx) + (ry - ly) * (ry - ly));
double dRightLeft = Math.Sqrt(rl);

Math.Pow参考中 ,看来,如果指数为2,则返回0的唯一方法是您的基数(即rx - lxry - ly )也为0。

暂无
暂无

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

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