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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:34:30+00:00 2026-06-13T23:34:30+00:00

This surprised me – the same arithmetic gives different results depending on how its

  • 0

This surprised me – the same arithmetic gives different results depending on how its executed:

> 0.1f+0.2f==0.3f
False

> var z = 0.3f;
> 0.1f+0.2f==z
True

> 0.1f+0.2f==(dynamic)0.3f
True

(Tested in Linqpad)

What’s going on?


Edit: I understand why floating point arithmetic is imprecise, but not why it would be inconsistent.

The venerable C reliably confirms that 0.1 + 0.2 == 0.3 holds for single-precision floats, but not double-precision floating points.

  • 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-13T23:34:32+00:00Added an answer on June 13, 2026 at 11:34 pm

    I strongly suspect you may find that you get different results running this code with and without the debugger, and in release configuration vs in debug configuration.

    In the first version, you’re comparing two expressions. The C# language allows those expressions to be evaluated in higher precision arithmetic than the source types.

    In the second version, you’re assigning the addition result to a local variable. In some scenarios, that will force the result to be truncated down to 32 bits – leading to a different result. In other scenarios, the CLR or C# compiler will realize that it can optimize away the local variable.

    From section 4.1.6 of the C# 4 spec:

    Floating point operations may be performed with higher precision than the result type of the operation. For example, some hardware architectures support an “extended” or “long double” floating point type with greater range and precision than the double type, and implicitly perform all floating point operations with the higher precision type. Only at excessive cost in performance can such hardware architectures be made to perform floating point operations with less precision. Rather than require an implementation to forfeit both performance and precision, C# allows a higher precision type to be used for all floating point operations. Other than delivering more precise results, this rarely has any measurable effects.

    EDIT: I haven’t tried compiling this, but in the comments, Chris says the first form isn’t being evaluated at execution time at all. The above can still apply (I’ve tweaked my wording slightly) – it’s just shifted the evaluation time of a constant from execution time to compile-time. So long as it behaves the same way as a valid evaluation, that seems okay to me – so the compiler’s own constant expression evaluation can use higher-precision arithmetic too.

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

Sidebar

Related Questions

this had me surprised: var parts = email.split('@'); if (parts < 2) { reference:
I'm surprised this doesn't happen automatically, but I would like my applications window to
I'm very surprised this issue hasn't been discussed in-depth: This article tells us how
I'm surprised I can't find this through googling, but I have a Viewbox in
Surprised that i havent been able to find this myself, but anyway. Let's say
I'm surprised I can't find a clear answer to this. So, in jQuery, you
Does anyone know how to do this? I am really surprised at the scarce
Somehow I never noticed until today that C++ supports nested classes. This surprised me
I ran across an assertion that HashSet<T>.Contains() is an O(1) operation. This surprised me
I'm surprised this question wasn't asked before on SO (well, at least I couldn't

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.