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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:05:29+00:00 2026-05-30T14:05:29+00:00

Possible Duplicate: Why is floating point arithmetic in C# imprecise? If I loop through

  • 0

Possible Duplicate:
Why is floating point arithmetic in C# imprecise?

If I loop through numerous random doubles, and “round” them to two fractional digit places, each individual round appears to be correct (0.02, 0.01, 0.00, etc).

However, there appears to be a very small fractional part that is kept along with the round.

double total = 0;

for (int i = 0; i < 10000; i++)
{
    total += Math.Round(random.NextDouble() * 0.02, 2);
}

Console.WriteLine(total);

Sample Outputs:

100.600000000006

99.7400000000059

Anyone care to explain why this happens is a more intuitive way?

  • 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-30T14:05:30+00:00Added an answer on May 30, 2026 at 2:05 pm

    System.Double and System.Float are base 2 floating point types. There are many finite decimal values that have an infinite representation in base 2, much as 1/3 has an infinite representation in base 10. Therefore, when you round to such a value, the binary representation is approximate. To avoid this problem, use the decimal type, which is a base 10 floating point type.

    There must be 100 duplicates of this question on stackoverflow, but I am on my phone, which makes it inconvenient to find them and link to them.

    For more information, look at the wikipedia article for IEEE double.

    Many will say that doubles are “not exact”, which is false. Every double value represents an exact value that can be represented exactly in base 10 (except NaN and infinity, of course). That’s because 2 is one of the prime factors of 10. The only approximation is when you try to represent certain decimal fractions (or other rational numbers whose denominator has at least one prime factor other than 2).

    The best way to understand this, for me at least, is to work out, on paper, the binary representations of a few fractions. For example, try 0.5, 0.625, 3.25, 5/16, 1/3, 0.2, and 0.3.

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

Sidebar

Related Questions

Possible Duplicate: Why is floating point arithmetic in C# imprecise? Hi. I've got following
Possible Duplicate: Why is floating point arithmetic in C# imprecise? I have been dealing
Possible Duplicate: Retain precision with Doubles in java Floating point inaccuracy examples I know
Possible Duplicate: Java floating point arithmetic What is special about this double math in
Possible Duplicate: Floating point arithmetic not producing exact results in Java Floating point inaccuracy
Possible Duplicate: Floating point inaccuracy examples double arithmetic and equality in Java I caught
Possible Duplicate: Floating point inaccuracy examples double a = 0.3; std::cout.precision(20); std::cout << a
Possible Duplicate: Extract decimal part from a floating point number in C I would
Possible Duplicate: Dealing with accuracy problems in floating-point numbers I was quite surprised why
Possible Duplicate: Floating point inaccuracy examples PHP rounding issue - Is this a bug?

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.