c++ round floating numbers to set precision
I wish to round a floating point number to set precision and return the result from a function. For example, I currently have the following function:
inline bool R3Point::
operator==(const R3Point& point) const
{
// Return whether point is equal
return ((v[0] == point.v[0]) && (v[1] == point.v[1]) && (v[2] == point.v[2]));
}
What I wish to do is instead of doing a direct v[i] == point.v[i]
comparison, I wish to compare only digits to a certain set precision, so that if v[i] = 0.33349999999999996
and point.v[i] = 0.33350000000000002
, my equal comparison will result in TRUE.
I am aware that there's a c++ smanip setprecision ( int n );
function and I've seen it used a lot when displaying 开发者_运维问答output on screen using cout
. However, I'm not sure if this can be used within the function like I described.
Thanks.
Generally, ==
should not be used to compare doubles, you should do something like :
if(v[0] - point.v[0] < 1e-9) { }
You can use abs
or fabs
if you are not sure of the sign and change the precision 1e-9 accordingly.
Comparing 2 floating point numbers (say a and b), is best done using the following: abs(a-b) < precision. Where abs(x) is the absolute value function, and precision is some small positive number. Often you want to set the precision as a function of the absolute value of the numbers being compared themselves.
精彩评论