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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:56:14+00:00 2026-05-25T23:56:14+00:00

I have to check to see whether two double values are equal including magnitude

  • 0

I have to check to see whether two double values are equal including magnitude and precision. I encounter a weird scenario where primitive double equals check is not consistent and depend on magnitude of value.

Java version i’ve used:

java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) Client VM (build 20.1-b02, mixed mode, sharing)

My code:

public class Test{
     public static void main(String args[]) throws Exception{
         String val1 = "15.999999999999999";
         String val2 = "16";
         String val3 = "16.999999999999999";
         String val4 = "17";
         double d1 = Double.parseDouble(val1);
         double d2 = Double.parseDouble(val2);
         double d3 = Double.parseDouble(val3);
         double d4 = Double.parseDouble(val4);
         System.out.println(val1 + "=" + val2 + "? ===>" + (d1==d2));
         System.out.println(val3 + "=" + val4 + "? ===>" + (d3==d4));
     }
}

Output:

15.999999999999999=16? ===>false
16.999999999999999=17? ===>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-25T23:56:14+00:00Added an answer on May 25, 2026 at 11:56 pm

    What you’re trying to do won’t work for various boring and complicated reasons that you can read about here: http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html

    Unless you’re going to compare the bit-representation of the doubles (which may or may not be insightful), you will always need some sort of epsilon value, i.e. margin of error when dealing with floating-point representations of numbers. Something like:

    boolean doublesAreEqual(double d1, double d2)
    {
        double d = d1 / d2;
        return (Math.abs(d - 1.0) < 0.00001 /* epsilon */);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I have a scenario where I want to check whether writing a given
I have just seen this // Check to see if the request is a
Many languages have a facility to check to see if an Object is of
I have a login script that when successful, should check to see if the
I have been trying to check my arraylist to see if an object already
Basically I have some code to check a specific directory to see if an
I have this control (see picture). I like when check one option in this
I need to check specific positions in an NSArray to see if they have
So, I'm using Jquery and have two arrays both with multiple values and I
I have two tables that I need to compare and update values in one

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.