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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:48:35+00:00 2026-05-15T14:48:35+00:00

I have noticed an interesting behavior with float rounding / truncation by the C#

  • 0

I have noticed an interesting behavior with float rounding / truncation by the C# compiler. Namely, when a float literal is beyond the guaranteed representable range (7 decimal digits), then a) explicitly casting a float result to float (a semantically unnecessary operation) and b) storing intermediate calculation results in a local variable both change the output. An example:

using System;

class Program
{
    static void Main()
    {
        float f = 2.0499999f;
        var a = f * 100f;
        var b = (int) (f * 100f);
        var c = (int) (float) (f * 100f);
        var d = (int) a;
        var e = (int) (float) a;
        Console.WriteLine(a);
        Console.WriteLine(b);
        Console.WriteLine(c);
        Console.WriteLine(d);
        Console.WriteLine(e);
    }
}

The output is:

205
204
205
205
205

In the JITted debug build on my computer, b is calculated as follows:

          var b = (int) (f * 100f);
0000005a  fld         dword ptr [ebp-3Ch] 
0000005d  fmul        dword ptr ds:[035E1648h] 
00000063  fstp        qword ptr [ebp-5Ch] 
00000066  movsd       xmm0,mmword ptr [ebp-5Ch] 
0000006b  cvttsd2si   eax,xmm0 
0000006f  mov         dword ptr [ebp-44h],eax 

whereas d is calculated as

          var d = (int) a;
00000096  fld         dword ptr [ebp-40h] 
00000099  fstp        qword ptr [ebp-5Ch] 
0000009c  movsd       xmm0,mmword ptr [ebp-5Ch] 
000000a1  cvttsd2si   eax,xmm0 
000000a5  mov         dword ptr [ebp-4Ch],eax 

Finally, my question: why is the second line of the output different from the fourth? Does that extra fmul make such a difference? Also note that if the last (already unrepresentable) digit from the float f is removed or even reduced, everything “falls in place”.

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

    Your question can be simplified to asking why these two results are different:

    float f = 2.0499999f;
    var a = f * 100f;
    var b = (int)(f * 100f);
    var d = (int)a;
    Console.WriteLine(b);
    Console.WriteLine(d);
    

    If you look at the code in .NET Reflector you can see that the above code is actually compiled as if it were the following code:

    float f = 2.05f;
    float a = f * 100f;
    int b = (int) (f * 100f);
    int d = (int) a;
    Console.WriteLine(b);
    Console.WriteLine(d);
    

    Floating point calculations cannot always be made exactly. The result of 2.05 * 100f is not exactly equal to 205, but just a little less due to rounding errors. When this intermediate result is converted to an integer is truncated. When stored as a float it is rounded to the nearest representable form. These two methods of rounding give different results.


    Regarding your comment to my answer when you write this:

    Console.WriteLine((int) (2.0499999f * 100f));
    Console.WriteLine((int)(float)(2.0499999f * 100f));
    

    The calculations are done entirely in the compiler. The above code is equivalent to this:

    Console.WriteLine(204);
    Console.WriteLine(205);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 542k
  • Answers 542k
  • 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
  • Editorial Team
    Editorial Team added an answer With the PSTerinalServices powershell module you can get the user… May 17, 2026 at 3:12 am
  • Editorial Team
    Editorial Team added an answer mySQL expects input for DATETIME columns to be YYYY-MM-DD HH:MM:SS… May 17, 2026 at 3:12 am
  • Editorial Team
    Editorial Team added an answer You seem to have misunderstood how class code works? GetFileList()… May 17, 2026 at 3:12 am

Trending Tags

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

Top Members

Related Questions

I encountered an interesting thing today that I have never noticed before. It appears
On Railcasts I have noticed a very interesting feature 'Go to symbol' window. It
This one is interesting to me - despite the almost inane title. I have
I am relatively new to C#, and I noticed something interesting today that I
comrades) I've found some interesting behavior of Invalidate method in multithreaded applications. I hope
I have a SqlServer 2008 table which has a Primary Key (IsIdentity=Yes) and three
I have developed an ASP.NET application that includes a WCF service. This service needs
I am facing some strange behavior with file-name completion in emacs. C-x C-f to
I'm using Visual Studio 2008 Team System with SP1, and I've noticed an annoying
I have some doubts over how Enumerators work, and LINQ. Consider these two simple

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.