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

  • Home
  • SEARCH
  • 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 5942033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:11:40+00:00 2026-05-22T16:11:40+00:00

Possible Duplicate: Java floating point arithmetic Hi All, I was just reading a book

  • 0

Possible Duplicate:
Java floating point arithmetic

Hi All,
I was just reading a book and i came across this example which gave different outputs. Can somebody explain to me why.

 public class Test {

    public static void main(String[] args) {
        double x = 0.3 - 0.2;
        double y = 0.2 - 0.1;
        System.out.println(x);
        System.out.println(y);

        System.out.println(y == x);
    }
}

Output is :
0.09999999999999998

0.1

false

  • 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-22T16:11:41+00:00Added an answer on May 22, 2026 at 4:11 pm

    Computers represent floating-point numbers as an integer times a power of 2. 0.1 is not exactly representable as a floating point number (in the same way you can’t write 1/3 in decimal) and you therefore get rounding errors. The best way to avoid this is to use integer types when you can and to use a tolerant comparison function when you can’t. Do not compare floating point numbers for exact equality unless you know what you are doing.

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

Sidebar

Related Questions

Possible Duplicate: Strange floating-point behaviour in a Java program I came across this weird
Possible Duplicate: Java floating point arithmetic What is special about this double math in
Possible Duplicate: Floating point inaccuracy examples double arithmetic and equality in Java I caught
Possible Duplicate: Choosing a Java Web Framework now? Hi All, I'm thinking about which
Possible Duplicates: Is JavaScript's Math broken? Java floating point arithmetic I have the current
Possible Duplicate: Java - Regex problem I have list of URLs of types: http://www.example.com/pk/etc
Possible Duplicate: Plugging in to Java compilers Edit - this appears to be a
Possible Duplicate: How do you find all subclasses of a given class in Java?
Possible Duplicate: java String concatenation How to Improve performance of this chunk of code
Possible Duplicate: Best Java obfuscator ? For my example I know that eclipse offers

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.