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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:16:14+00:00 2026-06-13T16:16:14+00:00

I want to understand how the Java double type will store its value in

  • 0

I want to understand how the Java double type will store its value in memory in Java.
When I run the following code I get unexpected output:

public static void main(String[] args)  {

    float a = 1.5f;
    float b= 0.5f;
    double c= 1.5;
    double d = 0.5;

    float a1 = 1.4f;
    float b1= 0.5f;
    double c1= 1.4;
    double d1 = 0.5;

    System.out.println(" a- b is " + (a-b));
    System.out.println(" c- d is " + (c-d));
    System.out.println("a1-b1 is " + (a1-b1));
    System.out.println("c1-d1 is " + (c1-d1));

}

Output:

 a- b is 1.0
 c- d is 1.0
a1-b1 is 0.9
c1-d1 is 0.8999999999999999

Why is c1-d1 not equal to 0.9?

I also tried other different values but some time it return expected result and some time not.

  • 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-13T16:16:15+00:00Added an answer on June 13, 2026 at 4:16 pm

    While you might have heard about rounding errors, you might be wondering why you have a rounding error here.

    float a1 = 1.4f;
    float b1 = 0.5f;
    double c1 = 1.4;
    double d1 = 0.5;
    
    System.out.println(new BigDecimal(a1) + " - " + new BigDecimal(b1) + " is " +
            new BigDecimal(a1).subtract(new BigDecimal(b1)) + " or as a float is " + (a1 - b1));
    System.out.println(new BigDecimal(c1) + " - " + new BigDecimal(d1) + " is " +
            new BigDecimal(c1).subtract(new BigDecimal(d1)) + " or as a double is " + (c1 - d1));
    

    prints

    1.39999997615814208984375 - 0.5 is 0.89999997615814208984375 or as a float is 0.9
    1.399999999999999911182158029987476766109466552734375 - 0.5 is
         0.899999999999999911182158029987476766109466552734375 
         or as a double is 0.8999999999999999
    

    As you can see, neither float nor double can represent these values exactly, and when the float or double is printed, some rounding occurs to hide this from you. In this case of float, the rounding to 7 decimal places yields the number you expected. In the case of double which has 16 digits of precision, the rounding error is visible.

    As @Eric Postpischil, notes whether the float or double operation has a rounding error depends entirely on the values used. In this situation, it was the float which appeared to be more accurate even through the represented value was further from 0.9 than the double value.

    In short: if you are going to use float or double you should use a sensible rounding strategy. If you can’t do this, use BigDecimal.

    System.out.printf("a1 - b1 is %.2f%n", (a1 - b1));
    System.out.printf("c1 - d1 is %.2f%n", (c1 - d1));
    

    prints

    a1 - b1 is 0.90
    c1 - d1 is 0.90
    

    When you print a float or double, it assumes that the nearest short decimal value is the one you really want. i.e. within 0.5 ulp.

    E.g.

    double d = 1.4 - 0.5;
    float f = d;
    System.out.println("d = " + d + " f = " + f);
    

    prints

    d = 0.8999999999999999 f = 0.9
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My goal is to understand every single bit of the following example: http://code.google.com/p/google-api-java-client/wiki/AndroidAccountManager I
I want to understand if code snippets are what I am looking for here.
I want to understand the use of xargs man in Rampion's code : screen
I want to understand why the following is happening. My guess is that a
In the below code, I want to understand what does the Collections.sort function is
I am still a learner of core java. I want to understand the polymorphism
I need to develop an application with following features and want to understand if
Where does the application logic run in a Java Web Start deployment? I want
I want to understand some things that take place in JAVA application while application
I want to understand this class better. (java.util.logging.Handler) Can anybody help me with some

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.