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

  • Home
  • SEARCH
  • 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 8139551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:51:46+00:00 2026-06-06T11:51:46+00:00

Background Consider the validation of three doubles low , width and high such that

  • 0

Background

Consider the validation of three doubles low, width and high such that the following three rules hold true:

  1. low < high;
  2. width > 0; and
  3. width fits into (high - low) “exactly” a whole number of times.

Essentially, the three values should specify a range that is to be split up into a certain number of bins, each of “exactly” equal width, with no part of the range unnaccounted for.

For example:

(A) low = -0.5, width = 0.005 and high = 0.5

would specify a range with a valid bin width, since “exactly” 200 full bins can be created, whereas

(B) low = -0.5, width = 0.275 and high = 0.5

would specify a range with an invalid bin width, since 3 full bins could be created but part of the range is not covered by those bins.

The Problem

What is the best way approach the third validation rule, given the floating-point nature of doubles?

My first naive attempt consisted of:

fmod( high - low, width ) == 0.0

but unfortunately fmod returns 0.005 for example (A) – my debugger tells me that the double of 0.005 actually holds the value of 0.0050000000000000001.

Should I be home-brewing my own solution to include tolerances, or is there a more elegant solution to this problem?

This is what I have currently:

bool valid(double range, double width, double tolerance = 0.000000001)
{
  assert(width > 0 && range > 0);

  while( range > 0 && range > tolerance )
  {
    range -= width;
  }

  return abs(range) <= tolerance;
}

Note the complete and utter arbitrariness of the default value of tolerance …

  • 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-06-06T11:51:48+00:00Added an answer on June 6, 2026 at 11:51 am

    Your approach using a tolerance for double comparisons is a reasonable one. The only thing you need now is to do the rest of your requirements analysis to determine how haccurate you really need to be 🙂

    If you know your maximum precision, multiply up to integer values for the comparison.

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

Sidebar

Related Questions

Background Consider the following: template <unsigned N> struct Fibonacci { enum { value =
Background Consider the following code: #include <utility> namespace ns { struct foo { foo()
Background Consider the following input: <Foo Bar=bar Baz=1 Bax=bax > After processing, I need
Background To replace invalid zip codes. Sample Data Consider the following data set: Typo
Consider the following code, which takes place in a background thread (thread B): List<T>
Background Distinguish between model values and predicted values. Problem Consider the following code: library(
Consider the following CSS: .my_class { background-image: url(/images/sprites.png); } Sometimes I change sprites.png by
This question requires some hypothetical background. Let's consider an employee table that has columns
Background: We're building an application that allows our customers to supply data in a
Background: I would like to dismiss a modalView that I have presented earlier and

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.