Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6613005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:09:57+00:00 2026-05-25T20:09:57+00:00

Is there a general best practice strategy for dealing with floating point inaccuracy? The

  • 0

Is there a general best practice strategy for dealing with floating point inaccuracy?

The project that I’m working on tried to solve them by wrapping everything in a Unit class which holds the floating point value and overloads the operators. Numbers are considered equal if they “close enough,” comparisons like > or < are done by comparing with a slightly lower or higher value.

I understand the desire to encapsulate the logic of handling such floating point errors. But given that this project has had two different implementations (one based on the ratio of the numbers being compared and one based on the absolute difference) and I’ve been asked to look at the code because its not doing the right, the strategy seems to be a bad one.

So what is best the strategy for try to make sure you handle all of the floating point inaccuracy in a program?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:09:57+00:00Added an answer on May 25, 2026 at 8:09 pm

    You want to keep data as dumb as possible, generally. Behavior and the data are two concerns that should be kept separate.

    The best way is to not have unit classes at all, in my opinion. If you have to have them, then avoid overloading operators unless it has to work one way all the time. Usually it doesn’t, even if you think it does. As mentioned in the comments, it breaks strict weak ordering for instance.

    I believe the sane way to handle it is to create some concrete comparators that aren’t tied to anything else.

    struct RatioCompare {
      bool operator()(float lhs, float rhs) const;
    };
    
    struct EpsilonCompare {
      bool operator()(float lhs, float rhs) const;
    };
    

    People writing algorithms can then use these in their containers or algorithms. This allows code reuse without demanding that anyone uses a specific strategy.

    std::sort(prices.begin(), prices.end(), EpsilonCompare());
    std::sort(prices.begin(), prices.end(), RatioCompare());
    

    Usually people trying to overload operators to avoid these things will offer complaints about “good defaults”, etc. If the compiler tells you immediately that there isn’t a default, it’s easy to fix. If a customer tells you that something isn’t right somewhere in your million lines of price calculations, that is a little harder to track down. This can be especially dangerous if someone changed the default behavior at some point.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Properties vs Methods Is there any rule or general best practice as
Is there a general best practice for creating somewhat complex HTML elements in jQuery?
Is there a generally-accepted best practice for creating an event handler that unsubscribes itself?
There is a general rule of OO design that you should model is-a relationships
I am following the general best practice principles of restoring my persistent and none
I was hoping for some guidelines on the general best practice for storing data
Is there a best practice around lazily loading modes when encountering a relevant file
I am just wondering what general best practice is for saving data in Ajax
Is there a best-practice for scalable http session management? Problem space: Shopping cart kind
Is there a general procedure for programming extensibility capability into your code? I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.