Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Integral division result cast to double #12

Open
rjbohinski opened this issue Mar 19, 2016 · 0 comments
Open

Integral division result cast to double #12

rjbohinski opened this issue Mar 19, 2016 · 0 comments

Comments

@rjbohinski
Copy link
Member

Found in VisionSystem.java.computeCrosshairHorizontalAngle(Image image, double targetHorizontalAngle, int crosshairNum)

return ((crosshairLeft[crosshairNum].x + (crosshairRight[crosshairNum].x - crosshairLeft[crosshairNum].x)/2)/size.width * VIEW_ANGLE - 30) - targetHorizontalAngle;

This code casts the result of an integral division (e.g., int or long division) operation to double or float. Doing division on integers truncates the result to the integer value closest to zero. The fact that the result was cast to double suggests that this precision should have been retained. What was probably meant was to cast one or both of the operands to double before performing the division. Here is an example:

int x = 2;
int y = 5;
// Wrong: yields result 0.0
double value1 =  x / y;
// Right: yields result 0.4
double value2 =  x / (double) y;
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant