i have matrix of type cv_32fc1
, , want limit precision. instance, has value this:
[0.00021743798, 0.000174778698, 0.00011652464, 5.826234e-005, 1.561136e-005]
but want limit precision (like in matlab):
[0.0002, 0.0002, 0.0001, 0.0001, 0.0000]
i'm not looking std::setprecision
solution rather limit matrix value precision inside, because affect multiplication other matrix later in case. changing matrix type didn't (at least). should do? thank you.
may these should work (do initial checks)
float customprecision(float in) { char buffer[15]; int buflen = sprintf(buffer, "%.4f", in); return atof(buffer); }
i afraid on choosing buffer size, float can give 23 bits of significand, 8 bits of exponent, , 1 sign bit. can out on choosing right buffer size.
as suggested adi,
float roundtoprecision(float val,int precision) { // initial checks float output = roundf(val * pow(10,precision))/pow(10,precision); return output; } int main() { // code goes here float a[] = {0.00021743798, 0.000174778698, 0.00011652464, 5.826234e-005, 1.561136e-005}; (int index = 0; index < 5; index++) { float val = roundtoprecision(a[index], 4); cout << val << endl; } return 0; }
Comments
Post a Comment