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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:05:50+00:00 2026-06-02T17:05:50+00:00

I have seen a very weird behaviour in Java’s double variable, as I’m trying

  • 0

I have seen a very weird behaviour in Java’s double variable, as I’m trying to simply add small fractions to a double and I see a completely bizarre results.

double test = 0;
test += 0.71;
test += 0.2;

Now I’d expect the result to be:

test = 0.91

Right? Wrong!

In reality, this is the number I get in my test double:

test = 0.9099999999999999

Now while this is very close, it’s a very bizarre fraction loss, and in the long run it causes serious bugs in my program.

With a float I’ve gotten even a weirder result.

Any help would be greatly appreciated.

Thanks

  • 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-02T17:05:53+00:00Added an answer on June 2, 2026 at 5:05 pm

    That’s the magic of binary encoding of floating point values (look for IEEE754 : http://en.wikipedia.org/wiki/IEEE_754-2008 ). If you want to be sure to never have this kind of things, you’re maybe looking for BigDecimal :

    http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html

    Basic rules :

    • don’t use equality tests when dealing with floating point numbers (you must test gaps)
    • round numbers you’re displaying (usually using DecimalFormat)
    • don’t use floating point numbers for financial applications
    • the float is generally the way to go for scientific or industrial operations, as long as you understand IEEE754
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen some very weird for loops when reading other people's code. I
In software companies I have seen it's really hard to work on very large
I've seen jint in Codeplex. It looks very interesting. Have you used it? Is
Great site! I have a question I haven't seen answered. I am very new
i have seen [] such brackets in c# very very rarely but when i
Today I have seen this weird magic NTFS system supports: each file can have
I have seen some web pages have a very nice scroll bar when a
I try to keep it very simple. All I have seen in .Net 3.5fx
I have seen very similar if not same questions on here but my trials
I have encountered a very weird behavior while using SimpleDateFormat for parsing a string

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.