I'm trying a really simple program and I don't undersand an apparent loss of precision when an int is converted into a double.
void collinear(int x1, int y1, int x2,
int y2, int x3, int y3)
{
int a = (x1 * (y2 - y3)) +
(x2 * (y3 - y1)) +
(x3 * (y1 - y2));
if (a == 0)
cout << "Yes";
else
cout << "No";
}
This is the version with a double
void collinear(double x1, double y1, double x2,
double y2, double x3, double y3)
{
double a = (x1 * (y2 - y3)) +
(x2 * (y3 - y1)) +
(x3 * (y1 - y2));
if (a == 0.0)
cout << "Yes";
else
cout << "No";
}
Given this input the two programs behave diffeently and I don't understand why
collinear(0,0,94911150,94911151,94911151,94911152);
Program with doubles: Yes
Program with ints: No
The answer when worked out with pure math is also No
. Is the program with the doubles losing precision?