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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:55:47+00:00 2026-06-12T10:55:47+00:00

For the issue of the floating precision, I defined my custom compare function for

  • 0

For the issue of the floating precision, I defined my custom compare function for floating numbers:

bool cmp(double a, double b)
{
    if(abs(a - b) <= eps) return false;
    return a < b;
}

Then I call sort on some array of floating numbers. I’ve heard that some bad compare function will cause the sort to segment fault. I just wondering will cmp work correctly for sort? On one hand, cmp satisfied the associating rule. But on the other hand, cmp(x - eps, x) == false && cmp(x, x + eps) == false doesn’t mean cmp(x - eps, x + eps) == false.

I didn’t use sort directly on floating numbers because what I want to sort is pair<double, double>.
For example:

(1,2), (2,1), (2.000000001, 0)

I’d like to consider 2 and 2.000000001 as the same and expect the result to be:

(1,2), (2.000000001, 0), (2,1)
  • 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-12T10:55:49+00:00Added an answer on June 12, 2026 at 10:55 am

    std::sort requires a comparer that defines a strict weak ordering. This means, among other things, that the following condition must be met:

    • We define two items, a and b, to be equivalent (a === b) if !cmp(a, b) && !cmp(b, a)
    • Equivalence is transitive: a === b && b === c => a === c

    As you already say in your question, your function cmp() does not meet these conditions, so you cannot use your function in std::sort(). Not only the result of the algorithm will be unpredictable, which is bad unless you are actually looking for this unpredictability (cf. randomize): if you have a few values that are very close to each other, such that any of them compare true with some, but false with some others, the algorithm might enter an infinite loop.

    So the answer is no, you cannot use your function cmp() in std::sort() unless you want to risk your program freezing.

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

Sidebar

Related Questions

Possible Duplicate: Precision of Floating Point Can you compare floating point values exactly to
Possible Duplicate: Floating point issue in C #include<stdio.h> main() { int a,b; float f;
I ran into an issue by introducing floating point columns in the MySQL database
Its the standard float issue. You have a bunch of floating elements inside a
My algorithm is calculating the epsilon for single precision floating point arithmetic. It is
I just read this nice answer given on how to compare floating-point values for
I've got an issue with floating divs in IE6. There's one navigation div on
Possible Duplicate: Floating point equality in python I have a small issue about my
Possible Duplicate: Floating point inaccuracy examples PHP rounding issue - Is this a bug?
Further to the discussion in Floating div issue in IE i added Floating div

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.