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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:08:00+00:00 2026-05-29T04:08:00+00:00

Possible Duplicate: Retain precision with Doubles in java Floating point inaccuracy examples I know

  • 0

Possible Duplicate:
Retain precision with Doubles in java
Floating point inaccuracy examples

I know that there is a good reason pertaining to objects and the way doubles are stored in Java that makes this if statement false. My understanding is for some reason, it’s really 0.29999999 instead of .3. Could some one explain to me further and intuitively why this conditional returns false?

public class hello {
    public static void main(String[] args) {

        double a = 0.1;
        double b = 0.1;
        double c = 0.3;

        if( a+b+a == c )
            System.out.println("if is 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-29T04:08:01+00:00Added an answer on May 29, 2026 at 4:08 am

    You can use BigDecimal to see what the actual values are.

    double a = 0.1;
    double c = 0.3;
    
    System.out.println(a + " is actually " + new BigDecimal(a));
    System.out.println(c + " is actually " + new BigDecimal(c));
    double a3 = a * 3;
    System.out.println("0.1 * 3 or " + a3 + " is actually " + new BigDecimal(a3));
    double ac = a + c;
    System.out.println("0.1 + 0.3 or " + ac + " is actually " + new BigDecimal(ac));
    

    prints

    0.1 is actually 0.1000000000000000055511151231257827021181583404541015625
    0.3 is actually 0.299999999999999988897769753748434595763683319091796875
    0.1 * 3 or 0.30000000000000004 is actually 0.3000000000000000444089209850062616169452667236328125
    0.1 + 0.3 or 0.4 is actually 0.40000000000000002220446049250313080847263336181640625
    

    As you can see, double cannot represent 0.1 precisely. When you print the value for 0.1 it does a small amount of rounding which hides the representation error. This is looks fine except when you perform an un-rounded calculation and you can see the representation error has accumulated.

    You can see that 0.1 * 3 results in an error which is large enough to be noticeable. However 0.1 + 0.3 works out to be the same as 0.4 (This is because the error for 0.1 is slightly too large and for 0.3 its slightly too small)

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

Sidebar

Related Questions

Possible Duplicate: Retain precision with Doubles in java import static java.lang.System.out; public class q2{
Possible Duplicate: What describes @property(…) best? What's that actually good for? If I declare
Possible Duplicate: Qt equivalent of PathAppend? Is there a class that handles file paths
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: Pre & post increment operator behavior in C, C++, Java, & C#
Possible Duplicate: NSNumber retain count issue Hello, I have the following code: NSNumber *number
Possible Duplicate: Atomic vs nonatomic properties I just want to know what is the
Possible Duplicate: How is release handled for @synthesized retain properties? I saw someone mention
Possible Duplicate: Django dynamic model fields Good Morning guys! Scenario is the following. For
Possible Duplicate: NSString retain Count I am the beginner for iPhone programming. I am

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.