-2

guys. I am doing some work around float point operations. The 0.1 is inexact represented by binary float point format. Thus I wrote down this

float i = 0.1f;

and expecting the inexact exception to arise. I turnned on the -fp-trap-all=all option, set fp-mode to be strict and installed SIGFPE signal handler in my code. But nothing happened. Then I tried

float i = 0.1f,j = 0.2f, c;
c = i + j;

still can not catch any exceptions! It drive my crazy.

Sorry to mention that I am using intel c++ compiler on Linux at last.

iqapple
  • 75
  • 8

2 Answers2

2

You have to test for exceptions yourself. The following code works for me:

#include <stdio.h>
#include <fenv.h>

#ifndef FE_INEXACT
#  error No FP Exception handling!
#endif

int main()
{
    double a = 4.0;
    a /= 3.0;

    if (fetestexcept(FE_INEXACT) & FE_INEXACT)
    {
        printf("Exception occurred\n");
    }
    else
    {
        printf("No exception.\n");
    }
}

If you replace 4.0 by 3.0, you will not get the exception.

You can do something similar with double a = 0.0; a = sin(a);.


Trapping exceptions are only supported conditionally. To check, use the macros described in the documentation:

#define _GNU_SOURCE
#include <fenv.h>

#ifndef FE_NOMASK_ENV
#  warning Cannot raise FP exceptions!
#else
#  warning Trapping FP exceptions enabled.
feenableexcept(FE_INEXACT);
#endif
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • The invocation of feenableexcept is required in my code. My handler was invoked after the exception emerged. Your code seems no handler is invoked but flags were set. I think the ieee754 mentioned a "alternate handler of exception". My handler could be considered as the "alternate handler"? Your code doesn't have that although you did something on the exception. – iqapple May 20 '13 at 23:14
  • This is a good example. It gives me some inspirations, Thanks alot. – iqapple May 20 '13 at 23:21
  • @iqapple: Did you do the feature check? I'll update the post. My platform (x86) doesn't have it. – Kerrek SB May 20 '13 at 23:21
  • No, I am using the Intel c++ compiler. It doesn't support the _GNU_SOURCE macro. My linux is pretty old and has no means to install a new gcc that support this feature(not connected to Internet, something is broken). – iqapple May 20 '13 at 23:27
  • Your x86 doesn't support it? I think all x87 float-point coprocessor support it. – iqapple May 20 '13 at 23:31
  • 2
    @iqapple: The fact that a processor supports exceptions (or traps) does not imply that a C implementation executing on that processor supports them. – Eric Postpischil May 21 '13 at 14:04
  • 1
    Some terminology to be careful about: In the IEEE 754 floating-point standard, an **exception** occurs when an operation does not have a suitable result; e.g., the exact mathematical result is outside the ability of the floating-point format to represent it. An exception may result in a substituted result (a value that is close, an infinity, a NaN, or something else), a flag, and/or a trap. In other contexts, **exception** may refer to some sort of change of program control, often due to a program error. – Eric Postpischil May 21 '13 at 14:09
  • 1
    To be standard C, this code should contain `#pragma STDC FENV_ACCESS on` after `#include `, to inform the compiler that the code may test floating-point status flags or may run with non-default floating-point control modes. However, the compiler may assume default floating-point operation, which can affect code generation. However, common compilers might not support this yet. – Eric Postpischil May 21 '13 at 15:58
0

According to this answer, inexact exception is only raised if the rounded version of the float isn't the same as the mathematically exact amount. In your case, the rounded response is the same, so no exception raised.

Community
  • 1
  • 1
PaulProgrammer
  • 16,175
  • 4
  • 39
  • 56
  • But I tried sin function which is intrinsically inexact. But I saw nothing, my program exit without catch the exception. – iqapple May 20 '13 at 22:48
  • Ok, I got it. I got the inexact exception. Thanks for your help. I also need to invoke feenableexcept. – iqapple May 20 '13 at 23:05