2016-02-07 64 views
8

執行C99:什麼是處理通過`POW引發的異常()`

double result = pow(base, exponent); 

任意baseexponent

可能會導致在試圖計算(溢出或複數)的recomended方式價值太大或太複雜。

例如與base=-2exponent=.5(-2的平方根)

我應該檢查是否result==NANresult==HUGE_VAL

該代碼是否符合C99和跨平臺?

+0

如果你想趕上這種情況下,考慮啓用浮點異常,把他們捉住。 – fuz

+1

@FUZxxl:我不知道浮點異常。你能指點我一些參考或張貼代碼片段作爲答案嗎?謝謝 – Paolo

+0

C上沒有例外,你必須檢查errno。 –

回答

3

抓住SIGFPE並嘶嘶作響。有一些比墜毀的程序更糟糕:一個悄悄地給出不正確的答案。下面

示例代碼採取from a random site about SIGFPE

/* demo_SIGFPE.c 

    Demonstrate the generation of the SIGFPE signal. 

    Usage: demo_SIGFPE [optstr] 

    The main program executes code the generates a SIGFPE signal. Before doing 
    so, the program optionally ignores and/or blocks SIGFPE. If 'optstr' 
    contains 'i', then SIGFPE is ignored, otherwise it is caught by a handler. 
    If 'optstr' contains 'b', then SIGFPE is blocked before it is delivered. 
    The behavior that occurs when SIGFPE is generated depends on the kernel 
    version (Linux 2.6 is different from Linux 2.4 and earlier). 

    NOTE: Don't compile this program with optimization, as the arithmetic 
    below is likely to be optimized away completely, with the result that 
    we don't get SIGFPE at all. 
*/ 
#define _GNU_SOURCE  /* Get strsignal() declaration from <string.h> */ 
#include <string.h> 
#include <signal.h> 

static void 
sigfpeCatcher(int sig) 
{ 
    printf("Caught signal %d (%s)\n", sig, strsignal(sig)); 
           /* UNSAFE (see Section 21.1.2) */ 
    sleep(1);     /* Slow down execution of handler */ 
} 

int 
main(int argc, char *argv[]) 
{ 
    int x, y; 
    sigset_t blockSet, prevMask; 
    Boolean blocking; 
    struct sigaction sa; 

    /* If no command-line arguments specified, catch SIGFPE, else ignore it */ 

    if (argc > 1 && strchr(argv[1], 'i') != NULL) { 
     printf("Ignoring SIGFPE\n"); 
     if (signal(SIGFPE, SIG_IGN) == SIG_ERR) 
      errExit("signal"); 
    } else { 
     printf("Catching SIGFPE\n"); 
     sigemptyset(&sa.sa_mask); 
     sa.sa_flags = SA_RESTART; 
     sa.sa_handler = sigfpeCatcher; 
     if (sigaction(SIGFPE, &sa, NULL) == -1) 
      errExit("sigaction"); 
    } 

    blocking = argc > 1 && strchr(argv[1], 'b') != NULL; 
    if (blocking) { 
     printf("Blocking SIGFPE\n"); 
     sigemptyset(&blockSet); 
     sigaddset(&blockSet, SIGFPE); 
     if (sigprocmask(SIG_BLOCK, &blockSet, &prevMask) == -1) 
      errExit("sigprocmask"); 
    } 

    printf("About to generate SIGFPE\n"); 
    y = 0; 
    x = 1/y; 
    y = x;  /* Avoid complaints from "gcc -Wunused-but-set-variable" */ 


    if (blocking) { 
     printf("Sleeping before unblocking\n"); 
     sleep(2); 
     printf("Unblocking SIGFPE\n"); 
     if (sigprocmask(SIG_SETMASK, &prevMask, NULL) == -1) 
      errExit("sigprocmask"); 
    } 

    printf("Shouldn't get here!\n"); 
    exit(EXIT_FAILURE); 
} 
+0

謝謝你的回答;我會考慮信號處理作爲一種選擇。我花了一些時間閱讀這個問題......問題是我不想在異常情況下中止整個過程。相反,我需要將異常處理爲「輕鬆」,並且該函數(包含有問題的'pow()')僅返回特定參數的錯誤值。爲此,我需要一個全局變量,其中信號處理程序會寫入發生異常,但這種方法會使我的函數不再安全多線程。請糾正我,如果我失去了一些東西... – Paolo

相關問題