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

  • Home
  • SEARCH
  • 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 139631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:28:11+00:00 2026-05-11T07:28:11+00:00

Consider the following code: 0.1 + 0.2 == 0.3 -> false 0.1 + 0.2

  • 0

Consider the following code:

0.1 + 0.2 == 0.3  ->  false 
0.1 + 0.2         ->  0.30000000000000004 

Why do these inaccuracies happen?

  • 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. 2026-05-11T07:28:12+00:00Added an answer on May 11, 2026 at 7:28 am

    Binary floating point math is like this. In most programming languages, it is based on the IEEE 754 standard. The crux of the problem is that numbers are represented in this format as a whole number times a power of two; rational numbers (such as 0.1, which is 1/10) whose denominator is not a power of two cannot be exactly represented.

    For 0.1 in the standard binary64 format, the representation can be written exactly as

    • 0.1000000000000000055511151231257827021181583404541015625 in decimal, or
    • 0x1.999999999999ap-4 in C99 hexfloat notation.

    In contrast, the rational number 0.1, which is 1/10, can be written exactly as

    • 0.1 in decimal, or
    • 0x1.99999999999999...p-4 in an analogue of C99 hexfloat notation, where the ... represents an unending sequence of 9’s.

    The constants 0.2 and 0.3 in your program will also be approximations to their true values. It happens that the closest double to 0.2 is larger than the rational number 0.2 but that the closest double to 0.3 is smaller than the rational number 0.3. The sum of 0.1 and 0.2 winds up being larger than the rational number 0.3 and hence disagreeing with the constant in your code.

    A fairly comprehensive treatment of floating-point arithmetic issues is What Every Computer Scientist Should Know About Floating-Point Arithmetic. For an easier-to-digest explanation, see floating-point-gui.de.

    Side Note: All positional (base-N) number systems share this problem with precision

    Plain old decimal (base 10) numbers have the same issues, which is why numbers like 1/3 end up as 0.333333333…

    You’ve just stumbled on a number (3/10) that happens to be easy to represent with the decimal system, but doesn’t fit the binary system. It goes both ways (to some small degree) as well: 1/16 is an ugly number in decimal (0.0625), but in binary it looks as neat as a 10,000th does in decimal (0.0001)** – if we were in the habit of using a base-2 number system in our daily lives, you’d even look at that number and instinctively understand you could arrive there by halving something, halving it again, and again and again.

    Of course, that’s not exactly how floating-point numbers are stored in memory (they use a form of scientific notation). However, it does illustrate the point that binary floating-point precision errors tend to crop up because the "real world" numbers we are usually interested in working with are so often powers of ten – but only because we use a decimal number system day-to-day. This is also why we’ll say things like 71% instead of "5 out of every 7" (71% is an approximation, since 5/7 can’t be represented exactly with any decimal number).

    So no: binary floating point numbers are not broken, they just happen to be as imperfect as every other base-N number system 🙂

    Side Side Note: Working with Floats in Programming

    In practice, this problem of precision means you need to use rounding functions to round your floating point numbers off to however many decimal places you’re interested in before you display them.

    You also need to replace equality tests with comparisons that allow some amount of tolerance, which means:

    Do not do if (x == y) { ... }

    Instead do if (abs(x - y) < myToleranceValue) { ... }.

    where abs is the absolute value. myToleranceValue needs to be chosen for your particular application – and it will have a lot to do with how much "wiggle room" you are prepared to allow, and what the largest number you are going to be comparing may be (due to loss of precision issues). Beware of "epsilon" style constants in your language of choice. These can be used as tolerance values but their effectiveness depends on the magnitude (size) of the numbers you’re working with, since calculations with large numbers may exceed the epsilon threshold.

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

Sidebar

Ask A Question

Stats

  • Questions 75k
  • Answers 75k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer TortoiseSVN has a setting to use '_svn' folders instead of… May 11, 2026 at 2:40 pm
  • added an answer I mixed your answers and I get my answer. I… May 11, 2026 at 2:40 pm
  • added an answer UITableViewController automatically handles this for you when you call super… May 11, 2026 at 2:40 pm

Related Questions

Consider the following code: for (var i = 0; i < 3; i++) {
Consider the following assembler output listing: START 100 MOVER BREG, ONE 101) + 04
Consider the following code: private static void WriteProcesses(StreamWriter sw, DateTime d) { sw.WriteLine(List of
Consider the following code and its output: Code #!/usr/bin/perl -w use strict; use Data::Dumper;

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.