Why Does Adding 0.1 Multiple Times Result in Lossless Values in Java?

Question

Why do multiple additions of 0.1 sometimes yield accurate results in Java despite precision issues?

double sum = 0, d = 0.1;
for (int i = 0; i < 5; i++)
    sum += d;
System.out.println(sum == 0.5); // Prints true

Answer

In Java, the representation of decimal numbers in binary can lead to precision issues. However, certain results may appear accurate due to the nature of specific numbers being added together.

double sum = 0, d = 0.1;
for (int i = 0; i < 5; i++)
    sum += d;
System.out.println(sum == 0.5); // Prints true

Causes

  • 0.1 is represented as a binary floating-point number, which is inherently imprecise.
  • The binary system cannot exactly represent certain fractions, like 0.1, but can represent exact multiples of powers of two, like 0.5.

Solutions

  • Adding 0.1 three times results in a floating-point number that is very close to, but not exactly, 0.3 due to rounding errors.
  • When adding 0.1 five times, the result aggregates more accurately to 0.5, which has a precise binary representation, hence returning true when compared.

Common Mistakes

Mistake: Assuming all decimal additions will produce accurate results in floating-point arithmetic.

Solution: Always test floating-point values for equality with a tolerance (epsilon) rather than exact matching.

Mistake: Neglecting that not all decimal fractions can be exactly represented in binary format.

Solution: Be aware of which decimal fractions can and cannot be accurately represented in floating-point.

Helpers

  • Java floating-point precision
  • adding decimal numbers in Java
  • floating-point arithmetic
  • binary representation of decimals

Related Questions

⦿How to Set Multiple Values in an IN Clause of a PreparedStatement in JDBC

Learn how to use PreparedStatement to set multiple values in an IN clause for executing SQL queries with JDBC.

⦿Why Are Static Methods Not Allowed in Interfaces?

Learn why static methods cant be declared in interfaces common misconceptions and how to properly use interfaces in Java.

⦿How to Fix javax.crypto.BadPaddingException: Given Final Block Not Properly Padded in Java Encryption?

Learn how to resolve javax.crypto.BadPaddingException in Java passwordbased encryption with detailed explanations and code examples.

⦿How to Calculate the Time Difference Between Two Joda-Time DateTime Objects in Minutes

Learn how to find the difference between two JodaTime DateTime instances in minutes with this stepbystep guide and code example.

⦿Why Is My Overridden equals() Method in Java Not Working as Expected?

Learn why your equals method might not function correctly in Java and how to properly override it for effective object comparison.

⦿How to Identify Duplicates in a List of Integers in Python

Learn how to identify duplicate integers in a list with this expert guide. Includes code snippets explanations and common mistakes to avoid.

⦿Can You Assign Numeric Values to Enums in Java?

Discover how to assign numeric values to enum elements in Java and explore code examples featuring custom enum definitions.

⦿Understanding the Difference Between Dynamic and Static Polymorphism in Java

Explore the differences between dynamic and static polymorphism in Java with clear examples and explanations for better understanding.

⦿How to Fix the Eclipse Warning: Build Path Specifies Execution Environment J2SE-1.4?

Learn how to resolve the Eclipse warning about J2SE1.4 execution environment and restore project compiling and debugging.

⦿Handling Lombok's @Data Annotation Warning with Inheritance

Learn how to effectively manage Lomboks Data annotation warning related to equalshashCode in an inheritance context.

© Copyright 2025 - CodingTechRoom.com