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 8971317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:55:31+00:00 2026-06-15T17:55:31+00:00

Let’s say I want to check whether two numbers a and b are equal.

  • 0

Let’s say I want to check whether two numbers a and b are equal. Because of imprecision with floating points, I know that instead of simply checking a == b, I usually want to pick some small number eps and check instead that abs(a - b) < eps.

But what do I do if I want to take into account floating point errors when checking that a > b? I’m guessing that instead of simply

if (a > b) {
  ...
}

I want to do something like:

if ((a > b) || abs(a - b) < eps) {
  ...
}

Is this correct? How do I check that a is “approximately greater than” b?

  • 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-15T17:55:32+00:00Added an answer on June 15, 2026 at 5:55 pm

    You are asking how to calculate a correct result (whether one value is greater than another value) from incorrect input (some values that have errors in them). Obviously, this is impossible in general: Incorrect input produces incorrect output. However, in some specific situations, we can salvage something. The following discusses one situation.

    Let’s suppose you have calculated some a and b that approximate the ideal values a and b, where a and b are the results you would have if the calculations were done with exact mathematics. Also suppose that we know error bounds ea and eb such that a – ea ≤ a ≤ a + ea and a – eb ≤ b ≤ b + eb. In other words, the calculated a and b lie within some intervals around a and b, respectively. (Depending on the operations performed, it is possible that errors could cause a or b to lie in some unconnected intervals, possibly not even containing a or b. But we will suppose you have “well behaved” errors.)

    In that case, if a – ea > b + eb, then you can be certain that a > b.

    However, suppose you test for this condition and return true if it holds. Then, whenever this returns true, you will know that a > b. However, when it returns false, you will not be sure that a > b is false. So, this test is good if you want to perform some action only when you are certain that a > b. But this causes you to miss performing the action in some cases when a > b.

    Suppose you do not want to miss any of those cases. Then consider the condition a + ea > b – eb. If a > b, then this condition must be true. So, if you test for this condition and perform the desired action when it holds, then the action will always be performed when a > b. However, the action may also be performed some times when it is not true that a > b.

    This shows that you have choices to make. If you have errors in your calculations, sometimes your application will do the wrong thing. You must choose:

    • How acceptable it is for your application to perform the action when it is false that a > b. Is it always acceptable/unacceptable, or does it depend on how close a is to b?
    • How acceptable it is for your application to not perform the action when it is true that a > b. Is it always acceptable/unacceptable, or does it depend on how close a is to b?

    If you can find some satisfactory compromise, then you set your condition to some intermediate level, and you test for the condition a-b > e, for some e that lies between – ea – eb and + ea + eb, inclusive. If you cannot find a satisfactory compromise, then you need to improve the calculations of a and b to reduce the errors, or you need to redesign your program in some way.

    Note: The final test in this scenario is a-b > e rather than a > b+e because there may be a small rounding error calculating b+e. There may also be a rounding error calculating a-b, but only if a and b are not near each other, in which case the difference, even with rounding, is much larger than e (unless your error interval is atrocious). In the cases where we care about precision, when a is near b, the calculation of a-b is exact.

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

Sidebar

Related Questions

Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say I have thousands of users and I want to make the passwords
Let's say I have multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,

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.