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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:08:52+00:00 2026-05-17T02:08:52+00:00

I want to make ‘==’ operator use approximate comparison in my program: float values

  • 0

I want to make ‘==’ operator use approximate comparison in my program: float values x and y are equal (==) if

abs(x-y)/(0.5(x+y)) < 0.001

What’s a good way to do that? Given that float is a built-in type, I don’t think I can redefine the == operator, can I?

Note that I would like to use other features of float, the only thing that I’d like to change is the equality operator.

EDIT:

Thank you for the answers, and I understand your arguments about readability and other issues.

That said, I really would prefer, if possible, to actually keep using the regular float type, instead of having a new class or a new comparison function. Is it even possible to redefine == operator for regular floats?

My reasons are::

(a) everyone using the program I’m writing wants the floats to be compared this way

(b) there’s no way in the world anyone would ever want to use the default == for floats.. Why is it even in the language???

(c) I dislike extra words in the code; obviously using the existing float results in no changes in the code whatsoever

EDIT 2.

Now that I know I can’t overload the == operator for float, I have to change my question. It will become so different that I’ll make a new one at custom comparison for built-in containers

  • 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-05-17T02:08:53+00:00Added an answer on May 17, 2026 at 2:08 am

    Your definition has two problems:

    1. Missing an *

    2. Will attempt to divide by zero if x + y == 0.0 (which covers a possibly frequent case x == y == 0.0)

    Try this instead:

    define approx_Equal(x, y, tolerance=0.001):
        return abs(x-y) <= 0.5 * tolerance * (x + y)
    

    Edit: Note the use of <= instead of < … needed to make the x == y == 0.0 case work properly.

    I wouldn’t try to override ==

    Edit 2: You wrote:

    there’s no way in the world anyone would ever want to use the default
    == for floats.. Why is it even in the language???

    No way? Suppose you have a function that returns a float, and you have a brainwave about an algorithm that would produce the same answers faster and/or more elegantly; how do you test it?

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

Sidebar

Related Questions

I want make sql query which will insert values from one table to another
I want make simple call app. I use this code. Intent intent = new
I use codeigniter, i want make valid data for captcha, if captcha is mixed
I use codeigniter and i want make uniqid code with php as following code(by
I want make datetimepicker in my project. Using jquery how it is possible? I
I want make a bash script which returns the position of an element from
I want make interactive application where user launches it and can do various task
Let's say I want make some of my sources publicly available via my blog
I am new android app developer i want make app for tablets and phone
I have style sheet with a class name changebackgroundcolor i want make change in

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.