1

The following code snippet

void main()
{
   float a = 68440675640679078541805800652800.0f;
   float b = a*a;
   cout << b << endl;
}

produces a floating-point overflow runtime error in MSVC 2017. I do not want to check if the float a is small enough for a muliplication.

How is it possible to tell the C++ compiler to build a programm which ignores floating-point overflow and underflow errors? Researching for an answer I came across the function _controlfp_s at

https://learn.microsoft.com/de-de/cpp/c-runtime-library/reference/controlfp-s?view=vs-2019

But I did not manage to get it to work.

5
  • 1
    Warning C4756 is a compile time error and is letting you know that at compile time float b = a*a; will produce overflow. What value do you want to be stored into b (at compile time) in this case? Commented Nov 28, 2019 at 16:48
  • The above code snippet compiles just fine. The problem is that I am getting a runtime error. And I am asking how to set up the compiler so that the program will ignore the runtime error. The value to be stored in b is not relevant for me. Commented Nov 28, 2019 at 17:36
  • 1
    Warnings are not "compile time errors" @Richard. They can be made to act as errors but they are not errors. Commented Nov 28, 2019 at 17:53
  • Show us this runtime error, @BlueTune. Commented Nov 28, 2019 at 17:54
  • The runtime error shown in the output reads: Exception thrown at 0x000000014012D7EC in Main.exe: 0xC0000091: Floating-point overflow (parameters: 0x0000000000000000, 0x00000000000011A8). Unhandled exception at 0x000000014012D7EC in Main.exe: 0xC0000091: Floating-point overflow (parameters: 0x0000000000000000, 0x00000000000011A8). Commented Nov 28, 2019 at 18:55

2 Answers 2

1

For Microsoft Visual C++ you can use _controlfp_s to get and to set the floating-point control word. For your code snippet a possible solution would look like:

int main()
{
    unsigned int fp_control;
    //Reading
    _controlfp_s(&fp_control, 0, 0);
    //Make changes
    unsigned int new_fp_control = fp_control | _EM_OVERFLOW | _EM_UNDERFLOW;
    //Update
    _controlfp_s(&fp_control, new_fp_control, _MCW_EM);

    float a = 68440675640679078541805800652800.0f;
    float b = a*a;
    out << b << ed;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can disable the specific MSVC warning using the following #pragma (but it must be outside and before the main function):

#pragma warning(disable:4756)
void main()
{
   float a = 68440675640679078541805800652800.0f;
   float b = a*a;
   cout << b << endl;
}

The compiler will then silently give b the specific IEEE code that marks a floating-point value as "infinity" (see this Wikipedia page) and your cout line will reflect that.

At any point, you can re-enable the warning using a similar #pragma line but replacing "disable" with "default".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.