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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:00:59+00:00 2026-05-20T12:00:59+00:00

I have aproblem with long sums of floats or doubles in Java. If I

  • 0

I have aproblem with long sums of floats or doubles in Java.

If I execute:

for ( float value = 0.0f; value < 1.0f; value += 0.1f )
    System.out.println( value );

I get:

0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.70000005
0.8000001
0.9000001

How do I get rid of the accumulation of the floating precision error?

I tried using doubles to halve the error, but the result is the same.

  • 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-20T12:01:00+00:00Added an answer on May 20, 2026 at 12:01 pm

    There is a no exact representation of 0.1 as a float or double. Because of this representation error the results are slightly different from what you expected.

    A couple of approaches you can use:

    • When using the double type, only display as many digits as you need. When checking for equality allow for a small tolerance either way.
    • Alternatively use a type that allows you to store the numbers you are trying to represent exactly, for example BigDecimal can represent 0.1 exactly.

    Example code for BigDecimal:

    BigDecimal step = new BigDecimal("0.1");
    for (BigDecimal value = BigDecimal.ZERO;
         value.compareTo(BigDecimal.ONE) < 0;
         value = value.add(step)) {
        System.out.println(value);
    }
    

    See it online: ideone

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

Sidebar

Related Questions

I have a problem where Device.Dispose() is taking a long time to execute when
I have now used way too long time, trying to figure out a problem,
I have a problem for a long time, I don't figure out where the
I have a problem to manage long paths. How can I get quickly to
We have a long standing issue in our bug tracking system about the dreaded
I have a problem with dividing a long value by 1000 and round it
I have a problem with long urls in the context of video streaming via
I have a problem, I can't get the result of my code. When I
i have a problem that i have two lables, 1) will contain a long
Early warning - code sample a little long... I have a singleton NSMutableArray that

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.