DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Convert Files to Thumbnail Images in Java
  • A Complete Guide on How to Convert InputStream to String In Java
  • The Generic Way To Convert Between Java and PostgreSQL Enums
  • How To Convert Image Files Into GIF or WebP Format Using Java

Trending

  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • Artificial Intelligence, Real Consequences: Balancing Good vs Evil AI [Infographic]
  • Automatic Code Transformation With OpenRewrite
  • How to Convert XLS to XLSX in Java
  1. DZone
  2. Coding
  3. Java
  4. Understanding Floating-Point Precision Issues in Java

Understanding Floating-Point Precision Issues in Java

In Java, there are two types of floating-point numbers: float and double. Follow an explanation about why floating numbers in Java is not what you might expect.

By 
Dmitry Egorov user avatar
Dmitry Egorov
DZone Core CORE ·
Sep. 12, 24 · Tutorial
Likes (14)
Comment
Save
Tweet
Share
7.7K Views

Join the DZone community and get the full member experience.

Join For Free

Java Floating Numbers Look Familiar

In Java, we have two types of floating-point numbers: float and double. All Java developers know them but can't answer a simple question described in the following meme:
Prove you are robot

Are you robot enough?

What Do You Already Know about Float and Double?

float and double represent floating-point numbers. float uses 32 bits, while double uses 64 bits, which can be utilized for the decimal or fractional part. But what does that actually mean? To understand, let's review the following examples:

Example: float and double utilized for the decimal or fractional part

Intuitive results are completely contradictory and seem to contain mistakes:

Results showing mistakesBut how is this possible? Where do the 4 and 2 at the end of the numbers come from? To understand, let's review how these numbers are actually created.  

Don’t Trust Your Eyes: Reviewing How 0.1 Converted to IEEE Standard

float and double follow the IEEE standard, which defines how to use 32/64 bits to represent a floating-point number. But how is a number like 0.1 converted to a bit array? Without diving too much into the details, the logic is similar to the following:

Converting Floating 0.1 to Arrays of Bits First

In the first stage, we need to convert the decimal representation of 0.1 to binary using the following steps:

  1. Multiply 0.1 by 2 and write down the decimal part.
  2. Take the fractional part, multiply it by 2, and note the decimal part.  
  3. Repeat the first step with the fraction from the second step.  

So for 0.1, we get the following results:

Step
Operation
Integer Part
Fraction
1
0.1 * 2
0
0.2
2
0.2 * 2
0
0.4
3
0.4 * 2
0
0.8
4
0.8 * 2
1
0.6
5
0.6 * 2
1
0.2
6
0.2 * 2
0
0.4

After repeating these steps, we get a binary sequence like 0.0001100110011 (in fact, it’s a repeating infinite sequence).

Converting Binary Array to IEEE Standard

Inside float/double, we don't keep the binary array as it is. float/double follow the IEEE 754 standard. This standard splits the number into three parts:

  1. Sign (0 for positive and 1 for negative)
  2. Exponent (defines the position of the floating point, with an offset of 127 for float or 1023 for double)
  3. Mantissa (the part that comes after the floating point, but is limited by the number of remaining bits)

So now converting 0.0001100110011... to IEEE, we get:

  • Sign 0 for positive
  • Exponent : Considering first four zeros  0.0001100110011 = -4 + 127 = 123 (or 01111011)
  • Mantissa 1100110011 (Mantissa ignores first 1), so we get 100110011

So the final representation is:

Final representation

So What? How Do These Numbers Explain Weird Results?

After all these conversions, we lose precision due to two factors:

  • We lose precision when converting from the infinite binary representation (which has repeating values like 1100110011).
  • We lose precision when converting to IEEE format because we consider only the first 32 or 64 bits.

This means that the value we have in float or double doesn't represent exactly 0.1. If we convert the IEEE bit array from float to a "real float," we get a different value. More precisely, instead of 0.1, we get 0.100000001490116119384765625.  

How can we verify this? There are a couple of ways. Take a look at the following code:

Verifying code

And as we expect, we get the following results:

Results

But if we want to go deeper, we can write reverse engineering code:

Reverse engineering code

As expected, it confirms our ideas:

Results confirming ideas

Answering the Question and Drawing Conclusions

Now that we know the value we see at initialization is different from what is actually stored in float/double, we expect the value on the left (0.1) but instead, we initialize with the value on the right (0.100000001490116119384765625):

Picture of two cats representing what we initialized versus what we actually get

So, knowing this, it's clear that when we perform actions such as adding or multiplying values, this difference becomes more pronounced, until it becomes visible during printing.

Conclusions

Here are the conclusions we can draw:

  1. Don’t use floating-point values for precise calculations, such as in finance, medicine, or complex science.
  2. Don’t compare two double values for equality directly; instead, check the difference between them with a small delta. For example: boolean isEqual = Math.abs(a - b) < 0.0000001;
  3. Use BigDecimal or similar classes for precise calculations.

I hope you now understand why 0.1 + 0.2 returns 0.300000000000004. Thanks for reading!

Convert (command) Java (programming language) Precision (computer science) Data Types

Opinions expressed by DZone contributors are their own.

Related

  • How to Convert Files to Thumbnail Images in Java
  • A Complete Guide on How to Convert InputStream to String In Java
  • The Generic Way To Convert Between Java and PostgreSQL Enums
  • How To Convert Image Files Into GIF or WebP Format Using Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!