Jump to content
  • Advertisement
Sign in to follow this  
Grain

Is this a good idea?

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In dealing with round off errors floats that should equal each other often don’t and Boolean comparisons that should pass could fail. Is the following an acceptable way of dealing with the problem, could I run into trouble somehow doing this?
const float OrderOfMagnitude = 8;

bool operator ==(const float &x, const float &y)
{
    float epsilon = x / pow(10, OrderOfMagnitude);

    if(abs(x-y) < epsilon)
        return true;
    return false;
}

Share this post


Link to post
Share on other sites
Advertisement
You can't override the behaviour of built-in operations afaik. Nor is there any point in making the parameters const references as opposed to just plain old floats.

Just make it a regular function with a name like "equals".

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Heh, you can run into trouble with float precision no matter what you do. No epsilon is univerally better than some other. Relative error may or may not be a better idea than absolute error.

Oh, and if you don't want your function to have a slow-ass performance, take that constant pow-expression out of there. And what iMalc said..

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Anonymous Poster
Oh, and if you don't want your function to have a slow-ass performance, take that constant pow-expression out of there.


Does MSVC compiler have compile-time caculation of values of some functions if their parameters are constant (something like gcc builtin functions)?

Share this post


Link to post
Share on other sites
If the function is inline, and it actually inlines it, then the compiler might do some local optimizations with const values.

Share this post


Link to post
Share on other sites
I'm fairly certain MSVC won't evaluate 'constant' floating point expressions at compile time simply because one different cpus with different fpu settings, the results won't be the same.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!