I have a program which divides a variable with another variable. But the answer im getting is incorrect:!! Please check image below :
Code Below:
iter_defects++;
df1[iter_defects].blob_id = b.Id;
decimal convertX = Convert.ToDecimal(txtmmtopixX.Text);
decimal convertY = Convert.ToDecimal(txtmmtopixY.Text);
df1[iter_defects].count = iter_defects;
df1[iter_defects].cog_X = Math.Round( Convert.ToDecimal( b.Centroid.X)) / convertX;
df1[iter_defects].cog_Y = Math.Round(Convert.ToDecimal(b.Centroid.Y ))/ convertY;
if (df1[iter_defects].cog_X > 10 && df1[iter_defects].cog_X < 30)
{
if (df1[iter_defects].BY > 1 && df1[iter_defects].BY < 74)
{
df1[iter_defects].cog_X = df1[iter_defects].cog_X - 20;
}
if (df1[iter_defects].BY > 64 && df1[iter_defects].BY < 129)
{
df1[iter_defects].cog_X = df1[iter_defects].cog_X - 20;
}
if (df1[iter_defects].BY > 118 && df1[iter_defects].BY < 183)
{
df1[iter_defects].cog_X = df1[iter_defects].cog_X - 20;
}
}
b.centroid.X and b.centroid.Y are double by default, so im converting it to decimal.
If I divide 147 by 9.3 i should get approx 16, but im getting 7.9!
Earlier I was using double instead of decimal, but even decimal is giving same issues. Have pinned the variable values on the image on the right.
What could be wrong?
UPDATE:
In my nested if statement I so some more arithmetic operation based on few conditions. When I removed those conditions the calculation is happening just fine. More confused now. The code below works, but without those conditions, my code after this section wont work:
iter_defects++;
df1[iter_defects].blob_id = b.Id;
decimal convertX = Convert.ToDecimal(txtmmtopixX.Text);
decimal convertY = Convert.ToDecimal(txtmmtopixY.Text);
df1[iter_defects].count = iter_defects;
df1[iter_defects].cog_X = Math.Round( Convert.ToDecimal( b.Centroid.X)) / convertX;
df1[iter_defects].cog_Y = Math.Round(Convert.ToDecimal(b.Centroid.Y ))/ convertY;
if (df1[iter_defects].cog_X > 10 && df1[iter_defects].cog_X < 30)
{
df1[iter_defects].cog_X = df1[iter_defects].cog_X - 20;
}
It's not entirely clear how your program works, but I assume the problem is probably because you are in a loop of some kind and by the time you are checking your results, more than one iteration has occurred and the base values (ex cog_X
) have changed, so your comparison math won't match.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.