[問題] 請問浮點數的運算(SIGFPE)
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
GCC
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
#include <signal.h>
#include <float.h>
問題(Question):
想請問如何透過浮點數運算產生 SIGFPE 的 signal
餵入的資料(Input):
無 純測試
預期的正確結果(Expected Output):
./a.out
SIGFPE
錯誤結果(Wrong Output):
./a.out
min_normal = 2.22507e-308
min_normal = 1.7116e-309
max_normal = 1.79769e+308
max_normal = inf
程式碼(Code):(請善用置底文網頁, 記得排版)
程式很短 我直接貼在這裡
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <float.h>
void sig_fpe(int signo){
printf("SIGFPE\n");
exit(0);
}
int main(){
double x;
signal(SIGFPE, sig_fpe);
x = DBL_MIN;
printf("min_normal = %g\n", x);
x = x/13.0;
printf("min_normal = %g\n", x);
x = DBL_MAX;
printf("max_normal = %g\n", x);
x = x*x;
printf("max_normal = %g\n", x);
return 0;
}
補充說明(Supplement):
我想要利用浮點數運算產生 SIGFPE
所以程式裡面故意產生 underflow 與 overflow
甚至還試過 division by 0
但是程式好像什麼事都沒發生一樣 可以正常結束
想請問一下要如何產生 SIGFPE 並且被 handler 偵測到
謝謝
我的 compile option 是 gcc -ftrapv
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.30.46
→
04/05 15:19, , 1F
04/05 15:19, 1F
→
04/05 15:20, , 2F
04/05 15:20, 2F
→
04/05 15:20, , 3F
04/05 15:20, 3F
推
04/05 15:27, , 4F
04/05 15:27, 4F
→
04/05 15:28, , 5F
04/05 15:28, 5F
→
04/05 15:28, , 6F
04/05 15:28, 6F
→
04/05 15:29, , 7F
04/05 15:29, 7F
→
04/05 15:30, , 8F
04/05 15:30, 8F
推
04/05 15:54, , 9F
04/05 15:54, 9F
→
04/05 15:55, , 10F
04/05 15:55, 10F
→
04/05 15:55, , 11F
04/05 15:55, 11F
→
04/11 18:03, , 12F
04/11 18:03, 12F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章