简体   繁体   中英

explicit math operation gives incorrect result in opencv

I've got this bug from time to time. And I have to uncover it this time.

Mat tRo012 = trans(165, 135, 85)*rotz(-11/180*CV_PI)*rotx((-90-15)/180*CV_PI);

trans, rotz, rotx are functions receiving double type numbers.

I wonder for what causes everytime I stack more operations like this, the return value, Mat tRo012 becomes wrong?

I remember experiencing a bug like similar to this:

double num = (-90-15)/180*CV_PI;

num got incorrect value. What I did for a way-around was to use a calculator and copy the explicit result for num. But that is not a good way, isn't it?

Would you have any comments on this?

Thank you!

If you're using C or C++, your compiler may be interpreting your constants as type int and doing integer arithmetic on them, then only converting the final result to double after completion. Try the following:

double num = (-90.0-15.0)/180.0*CV_PI;

On a side note, if you know the explicit number (such as your num value) it IS good practice to manually enter the number. This will save the program from having to calculate it at runtime. Many compilers will do this for you anyway as part of the optimization process, but they're not required to.

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.

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