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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:35:30+00:00 2026-05-30T02:35:30+00:00

Possible Duplicate: java for-loop problem Why is the output of following code: for (float

  • 0

Possible Duplicate:
java for-loop problem

Why is the output of following code:

for (float j2 = 0.0f; j2 < 10.0f; j2+=0.1f) {
    System.out.println(j2);
}

this:

0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.70000005
0.8000001
0.9000001
1.0000001
1.1000001
1.2000002
1.3000002
1.4000002
1.5000002
1.6000003
1.7000003
1.8000003
1.9000003
2.0000002
2.1000001
2.2
2.3
2.3999999
2.4999998
2.5999997
2.6999996
2.7999995
2.8999994
2.9999993
3.0999992
3.199999
3.299999
3.399999
3.4999988
3.5999987
3.6999986
3.7999985
3.8999984
3.9999983
4.0999985
4.1999984
4.2999983
4.399998
4.499998
4.599998
4.699998
4.799998
4.8999977
4.9999976
5.0999975
5.1999974
5.2999973
5.399997
5.499997
5.599997
5.699997
5.799997
5.8999968
5.9999967
6.0999966
6.1999965
6.2999964
6.3999963
6.499996
6.599996
6.699996
6.799996
6.899996
6.9999957
7.0999956
7.1999955
7.2999954
7.3999953
7.499995
7.599995
7.699995
7.799995
7.899995
7.9999948
8.099995
8.199995
8.299995
8.399996
8.499996
8.599997
8.699997
8.799997
8.899998
8.999998
9.099998
9.199999
9.299999
9.4
9.5
9.6
9.700001
9.800001
9.900002

Also one more question: even if I change condition of for loop to j2<=10.0f the output is the same. Why so? Shouldn’t it include 10.0 in output?

  • 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-30T02:35:30+00:00Added an answer on May 30, 2026 at 2:35 am

    As others have mentioned it is about the precision of the floating point numbers and the fact that each step of manipulation could potentially increase the error.

    This is why you never use floats to represent money. Check out BigDecimal for exact decimal handling.

    The reason that 10.0 is not included is that the sum has floated and no longer is <= 10.0 since the precision/rounding error has drifted above +0.0. 9.900002+0.1 will probably yield something like 10.000002 which is > 10.0.

    Floats are not represented as exact numbers but rather as a coefficient and an exponent such as (−1)^s × c × b^q and adding to floats will combine uncertainty from both numbers. This might translate to 0.70000005 instead of 0.7 due to loss of precision.

    Check out http://en.wikipedia.org/wiki/IEEE_754-2008

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

Sidebar

Related Questions

Possible Duplicate: Java GUI repaint() problem? I write a Java code, but I have
Possible Duplicate: java String concatenation How to Improve performance of this chunk of code
Possible Duplicate: Java string comparison? I have encounter the following problem, I have an
Possible Duplicate: java: "final" System.out, System.in and System.err? It's just occurred to me although
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: Create Excel file in Java How to save output in excel format
Possible Duplicate: Java Class.cast() vs. cast operator I am unsuccessfully trying to find out
Possible Duplicate: Can one do a for each loop in java in reverse order?
Possible Duplicate: Java - Leaking this in constructor In the NetBeans I have a

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.