• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# What am I doing wrong?

## 3 posts in this topic

This code is to test two functions I need, RotatePoint, and MultplyMat. As far as I acn tell they are correct, but when the prog is run, the results show the new rotated point correctly except one part which is like -0.000007, it SHOULD be 0. Take a look at my code and see... #define PI 3.1416f void MultiplyMat(float m1[3][3], float m2[3][3], float dest[3][3]) { int down; int across; for (down = 0; down < 3; down++) { for (across = 0; across < 3; across++) { dest[across][down] = m1[0][down] * m2[across][0] + m1[1][down] * m2[across][1] + m1[2][down] * m2[across][2]; } } } void RotatePoint(float point[3], float rot[3], float dest[3]) { float rx = rot[0] * PI / 180; float ry = rot[1] * PI / 180; float rz = rot[2] * PI / 180; float XRmat[3][3]; float YRmat[3][3]; float ZRmat[3][3]; // Set up XRmat XRmat[0][0] = 1; XRmat[1][0] = 0; XRmat[2][0] = 0; XRmat[0][1] = 0; XRmat[1][1] = cos(rx); XRmat[2][1] = sin(rx); XRmat[0][2] = 0; XRmat[1][2] = -sin(rx); XRmat[2][2] = cos(rx); // Set up YRmat YRmat[0][0] = cos(ry); YRmat[1][0] = 0; YRmat[2][0] = sin(ry); YRmat[0][1] = 0; YRmat[1][1] = 1; YRmat[2][1] = 0; YRmat[0][2] = -sin(ry); YRmat[1][2] = 0; YRmat[2][2] = cos(ry); // Set up ZRmat ZRmat[0][0] = cos(rz); ZRmat[1][0] = sin(rz); ZRmat[2][0] = 0; ZRmat[0][1] = -sin(rz); ZRmat[1][1] = cos(rz); ZRmat[2][1] = 0; ZRmat[0][2] = 0; ZRmat[1][2] = 0; ZRmat[2][2] = 1; float tempmat[3][3]; MultiplyMat(XRmat, YRmat, tempmat); float finalmat[3][3]; MultiplyMat(tempmat, ZRmat, finalmat); dest[0] = point[0] * finalmat[0][0] + point[1] * finalmat[0][1] + point[2] * finalmat[0][2]; dest[1] = point[0] * finalmat[1][0] + point[1] * finalmat[1][1] + point[2] * finalmat[1][2]; dest[2] = point[0] * finalmat[2][0] + point[1] * finalmat[2][1] + point[2] * finalmat[2][2]; } int main(int argc, char* argv[]) { float p[3] = { 0, 0, 1}; float rot[3] = { 0, 90, 0 }; float dest[3] = { 0, 0, 0 }; RotatePoint(p, rot, dest); cout << p[0] << "\t" << p[1] << "\t" << p[2] << endl << endl; cout << dest[0] << "\t" << dest[1] << "\t" << dest[2] << endl << endl; return 0; } Thanks for any help.
0

##### Share on other sites
Getting small errors when working with floats is very common. The precision of floats is not very good, something about 6 digits. It is something you will just have to live with.

You can lessen the errors a bit by using doubles instead, but there will still be errors.
0

##### Share on other sites
The problem is that if I rotate point (1000000, 0, 0) that "small error" suddenly amounts to 7.000000 instead of the acceptable 0.000007. But I guess this is acceptable to in the grand scheme of things.
0

##### Share on other sites
Try increasing the accuracy of your PI. You should at least go out to 3.14159, but probably much farther, if you want to reduce that error.
0