C99:处理 `pow()`(溢出或复数)引发的异常的推荐方法是什么
C99: what is the recomended way to handle exceptions raised by `pow()` (overflow or complex number)
正在执行
double result = pow(base, exponent);
与任意 base
和 exponent
可能会导致尝试计算太大或复杂的值。
例如 base=-2
、exponent=.5
(-2 的平方根)
我应该只检查 result==NAN
还是 result==HUGE_VAL
?
该代码是否兼容 C99 并跨平台?
抓住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);
}
正在执行
double result = pow(base, exponent);
与任意 base
和 exponent
可能会导致尝试计算太大或复杂的值。
例如 base=-2
、exponent=.5
(-2 的平方根)
我应该只检查 result==NAN
还是 result==HUGE_VAL
?
该代码是否兼容 C99 并跨平台?
抓住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);
}