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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:30:34+00:00 2026-05-10T13:30:34+00:00

What is the best method for comparing IEEE floats and doubles for equality? I

  • 0

What is the best method for comparing IEEE floats and doubles for equality? I have heard of several methods, but I wanted to see what the community thought.

  • 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. 2026-05-10T13:30:35+00:00Added an answer on May 10, 2026 at 1:30 pm

    The best approach I think is to compare ULPs.

    bool is_nan(float f) {     return (*reinterpret_cast<unsigned __int32*>(&f) & 0x7f800000) == 0x7f800000 && (*reinterpret_cast<unsigned __int32*>(&f) & 0x007fffff) != 0; }  bool is_finite(float f) {     return (*reinterpret_cast<unsigned __int32*>(&f) & 0x7f800000) != 0x7f800000; }  // if this symbol is defined, NaNs are never equal to anything (as is normal in IEEE floating point) // if this symbol is not defined, NaNs are hugely different from regular numbers, but might be equal to each other #define UNEQUAL_NANS 1 // if this symbol is defined, infinites are never equal to finite numbers (as they're unimaginably greater) // if this symbol is not defined, infinities are 1 ULP away from +/- FLT_MAX #define INFINITE_INFINITIES 1  // test whether two IEEE floats are within a specified number of representable values of each other // This depends on the fact that IEEE floats are properly ordered when treated as signed magnitude integers bool equal_float(float lhs, float rhs, unsigned __int32 max_ulp_difference) { #ifdef UNEQUAL_NANS     if(is_nan(lhs) || is_nan(rhs))     {         return false;     } #endif #ifdef INFINITE_INFINITIES     if((is_finite(lhs) && !is_finite(rhs)) || (!is_finite(lhs) && is_finite(rhs)))     {         return false;     } #endif     signed __int32 left(*reinterpret_cast<signed __int32*>(&lhs));     // transform signed magnitude ints into 2s complement signed ints     if(left < 0)     {         left = 0x80000000 - left;     }     signed __int32 right(*reinterpret_cast<signed __int32*>(&rhs));     // transform signed magnitude ints into 2s complement signed ints     if(right < 0)     {         right = 0x80000000 - right;     }     if(static_cast<unsigned __int32>(std::abs(left - right)) <= max_ulp_difference)     {         return true;     }     return false; } 

    A similar technique can be used for doubles. The trick is to convert the floats so that they’re ordered (as if integers) and then just see how different they are.

    I have no idea why this damn thing is screwing up my underscores. Edit: Oh, perhaps that is just an artefact of the preview. That’s OK then.

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

Sidebar

Ask A Question

Stats

  • Questions 156k
  • Answers 156k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In ListView parlance, these are not separate items. It sounds… May 12, 2026 at 10:49 am
  • Editorial Team
    Editorial Team added an answer You need to perform this cast a lot. There isn't… May 12, 2026 at 10:49 am
  • Editorial Team
    Editorial Team added an answer Using the C# language constructs, you cannot explicitly call the… May 12, 2026 at 10:49 am

Related Questions

I came across this recently, up until now I have been happily overriding the
I have an app written in C# that does a lot of string comparison.
After finishing two cs classes, I've started working on a personal project in Java.
I am currently developing an approval routing WCF service that will allow an user

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.