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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:45:03+00:00 2026-06-11T08:45:03+00:00

I implemented the AlmostEqual2sComplement as proposed by Bruce Dawson , but for comparing double

  • 0

I implemented the AlmostEqual2sComplement as proposed by Bruce Dawson, but for comparing double instead of float values.

Similar implementations can be found in many places.

bool AlmostEqual2sComplement(double A, double B, int maxUlps= 10)
{
    // Make sure maxUlps is non-negative and small enough that the
    // default NAN won't compare as equal to anything.
    // assert maxUlps > 0 && maxUlps < 4 * 1024 * 1024;

    long long aInt = *(long long*)&A;
    // Make aInt lexicographically ordered as a twos-complement int
    if (aInt < 0)
        aInt = 0x8000000000000000 - aInt;

    // Make bInt lexicographically ordered as a twos-complement int
    long long bInt = *(long long*)&B;
    if (bInt < 0)
        bInt = 0x8000000000000000 - bInt;

    long long intDiff = aInt - bInt;
    if (intDiff < 0)
        intDiff= intDiff*-1;

    if (intDiff <= maxUlps)
        return true;
    return false;
}

Unfortunately when comparing the double value -1.0 and 4.0 the function returns true.
This is because the intDiff results in this case to be equal 0x8000000000000000 and
the absolute value of 0x8000000000000000 is again 0x8000000000000000

My current solution to this problem is not to take the absolute value of intDiff but rather change the comparision of intDiff and maxUlps to:

if (intDiff <= maxUlps && -maxUlps <= intDiff)
    return true;

There must be more (maybe not so evident) cases where intDiff results in 0x8000000000000000.

I’m wondering if other implementations of AlmostEqual2sComplement are just not aware of this problem, or if I made a mistake in my original implementation?

  • 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-11T08:45:04+00:00Added an answer on June 11, 2026 at 8:45 am

    Here http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition is the corrected version, where there is an additional check whether the two floats have opposite sign.

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

Sidebar

Related Questions

I implemented some code using backbone.js in Asp.NEt MVC3 and found backbone.js very helpful.
I implemented sherlockactionbar in my project and it works great, but I don't know
I implemented a quick search in my application. But when I turned the screen,
I implemented the delegate-callback pattern between two classes without retaining the delegate. But in
I implemented in my app MFMessageComposeViewController for send sms. It works well, but I
I implemented RestSharp succesfully in my WP7 application, but one issue remains: When I
I implemented an rpc call. It works on local machine but does not work
I implemented the cookies jar as shown in the web, and I can receive
Having implemented CLogClass to make decent logging I also defined macro, but it works
I implemented iCarousel method in my ViewController and all is working properly but there

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.