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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:05:32+00:00 2026-05-24T01:05:32+00:00

Is it safe if I use comparision like this (a is int, b and

  • 0

Is it safe if I use comparision like this (a is int, b and c is float/double):

a == b
b == c

It may hear ridiculous, but in my old programing language, sometimes 1 + 2 == 3 is false (because left side returns 2.99999999999…). And, what about this:

Math.sqrt(b) == Math.sqrt(c)
b / 3 == 10 / 3 //In case b = 10, does it return true?
  • 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-24T01:05:33+00:00Added an answer on May 24, 2026 at 1:05 am

    In general, no it is not safe due to the fact that so many decimal numbers cannot be precisely represented as float or double values. The often stated solution is test if the difference between the numbers is less than some “small” value (often denoted by a greek ‘epsilon’ character in the maths literature).

    However – you need to be a bit careful how you do the test. For instance, if you write:

    if (Math.abs(a - b) < 0.000001) {
        System.err.println("equal");
    }
    

    where a and b are supposed to be “the same”, you are testing the absolute error. If you do this, you can get into trouble if a and b are (say_ 1,999,999.99 and 2,000,000.00 respectively. The difference between these two numbers is less than the smallest representable value at that scale for a float, and yet it is much bigger than our chosen epsilon.

    Arguably, a better approach is to use the relative error; e.g. coded (defensively) as

    if (a == b ||
        Math.abs(a - b) / Math.max(Math.abs(a), Math.abs(b)) < 0.000001) {
        System.err.println("close enough to be equal");
    }
    

    But even this is not the complete answer, because it does not take account of the way that certain calculations cause the errors to build up to unmanageable proportions. Take a look at this Wikipedia link for more details.

    The bottom line is that dealing with errors in floating point calculations is a lot more difficult than it appears at first glance.


    The other point to note is (as others have explained) integer arithmetic behaves very differently to floating point arithmetic in a couple of respects:

    • integer division will truncate if the result is not integral
    • integer addition subtraction and multiplication will overflow.

    Both of these happen without any warning, either at compile time or at runtime.

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

Sidebar

Related Questions

Is it safe to use something like this to ensure that a directory isn't
I run all my integers through a (int)Integer to make them safe to use
Software will use memory, no big suprise, but how do you keep this usage
Is this portable or at least safe to use with g++? #include <iostream> #include
I just wonder if this line of code is safe to use to avoid
Is it safe to use the 'raw' helper in a Rails app that may
Possible Duplicate: Using GetHashCode for getting Enum int value Is it safe to use
Is it safe to use MS SQL's WITH (NOLOCK) option for select statements and
My office has a central Source Safe 2005 install that we use for source
Assuming we always use a Sun JVM (say, 1.5+), is it always safe to

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.