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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:18:02+00:00 2026-06-14T20:18:02+00:00

I am having a problem with this code! I am trying to run the

  • 0

I am having a problem with this code!
I am trying to run the code in java but the answer seems pretty weird.

float a=0.1F;
float b=0.2F;
if((a+b)==0.3){
System.out.println("True");
}
else{
System.out.println("False");
}

The answer is : False
But theoretically it should be return True.
The function returns True if we use values of a and b like 0.15 and 0.15 or 0.05 and 0.25.
I am confused.
I have read somewhere that languages like Java/JavaScript implements IEEE-754 number formatting! If so, then what is this formatting and what is wrong with the code? Is there anyway to change the number format?

  • 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-14T20:18:04+00:00Added an answer on June 14, 2026 at 8:18 pm

    The precision difference between floats (a+b) and doubles (0.3) is causing the condition to be false. You could instead use (a+b)==0.3F. i.e.

    float a = 0.1F;
    float b = 0.2F;
    if ((a + b) == 0.3F) {  // notice the "F"
        System.out.println("True");
    } else {
        System.out.println("False");
    }
    
    True
    

    EDIT: In this case, even if you use doubles the condition will be false:

    double a = 0.1;
    double b = 0.2;
    if ((a + b) == 0.3) {
        System.out.println("True");
    } else {
        System.out.println("False");
    }
    
    False
    

    Printing 0.1 + 0.2 will reveal why (the number cannot be represented perfectly):

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

Sidebar

Related Questions

I am having problem getting this piece of code to run. The class is
I'm having a problem with getting this code to print out to file due
I'm having a problem with this code: #include <iostream> using namespace std; class A
I am having a problem getting this code to work. Basically I Go from
hi i'm having a problem with this code. my problem is that the first
I've having this problem with some JavaScript code. I've simplified it in the error
I am having a problem with bit of code. This is the code $test
I am having some problem with jsonp and jquery. This is my code -
I'm having some trouble with this code. I am trying to write a function
I've run into a very annoying problem while trying to update some code. The

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.