2

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.

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

what do you mean by change the precision 1e-9 accordingly? In my case, I'm not sure of the sign so I would be using something if(fabs(v[0]-point.v[0]) < 1e-9).
I meant, you need to change 1e-9 according to the number of digits you need to be precise.
3

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.

1 Comment

Be sure to write std::abs instead of just abs or you may accidentally get the C function that only does integers. Happy debugging then...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.