C++ floating point denormal

Posted on 2011-05-13
Last Modified: 2012-05-11
I have a large project with numerous floating point operations which I am converting from Visual C++ 6 to Visual Studio 2010 c++.  I've hit a problem in the different way that VS2010 deals with underflow denormals.

Consider the following code:
        unsigned int cw = _controlfp(0, 0);//this returns  589855 in both VC6 and VS2010
      float abba=12.5;
      int myin=0;
      for (myin=0; myin<100; myin ++)
            abba = abba/10.1;

Both VS2010 and VC6 behave the same up until abba reaches the FLT_MIN lower bound for a float, around 1e-38.  After that point in VC6 abba goes to 1e-45, and then to 0.0000.  In VS2010, it goes to a garbage 8e-39#DEN value.

500 points to the person who can show me how to get the VC6 behavior back.

Notice the _controlfp(0,0) integer in both VS2010 and VC6 they are the same, 589855.
Question by:ChemstationsDeveloper
    LVL 53

    Accepted Solution

    The precise result in case of a floating point underflow is implementation defined, so there is no standard way of forcing 0.0f

    However, MSDN claims that Visual Studio 2010 would set it to 0.0f in your case :


    so I'm not sure how to interpret the result you get.
    LVL 8

    Expert Comment

    Try to use _control87() (or even __control87_2()) instead of _controlfp().

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now