473,395 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

fetestexcept fails

Hi all!

I try to implement floating point error handling, (i.e. trap nan and if
errors in floating point opperations) with gnu c standard library.
However fetestexcept does not give me the expected result.
The following code prints
Floating point error:
without the expected error code

Thank you!
Expand|Select|Wrap|Line Numbers
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <fenv.h>
  5. #include <setjmp.h>
  6. #include <signal.h>
  7. #include <math.h>
  8.  
  9.  
  10. #ifndef __SUPPORT_SNAN__
  11. #define __SUPPORT_SNAN__
  12. #endif
  13.  
  14. jmp_buf return_to_top_level;
  15.  
  16.  
  17. /* floating-point error handler */
  18. void fperror(int sig)
  19. {
  20.  
  21. int ret;
  22. int set_excepts;
  23. char temp[256];
  24.  
  25. strcpy(temp, "Floating point error: ");
  26.  
  27. set_excepts = fetestexcept(FE_INEXACT | FE_DIVBYZERO | FE_UNDERFLOW |
  28. FE_OVERFLOW);
  29.  
  30. if(set_excepts & FE_INEXACT)
  31. strcat(temp,"[Inexact] ");
  32.  
  33. if(set_excepts & FE_DIVBYZERO)
  34. strcat(temp,"[Divide by 0] ");
  35.  
  36. if(set_excepts & FE_UNDERFLOW)
  37. strcat(temp,"[Underflow] ");
  38.  
  39. if(set_excepts & FE_OVERFLOW)
  40. strcat(temp,"[Overflow] ");
  41.  
  42. if(set_excepts & FE_INVALID)
  43. strcat(temp,"[Invalid] ");
  44.  
  45. printf("\a%s\n", temp);
  46.  
  47. keep_going = 0;
  48. longjmp(return_to_top_level, 1);
  49.  
  50. }
  51.  
  52. int main()
  53. {
  54.  
  55. double test1, test2;
  56. int ret;
  57.  
  58. /* assigning exception handler */
  59.  
  60. signal(SIGFPE, fperror);
  61.  
  62. /* enableing floating point exception signaling */
  63. ret = feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
  64.  
  65. /* set-up return address for errors */
  66. if(setjmp(return_to_top_level)){
  67. return 1;
  68. }
  69.  
  70. test1 = 0.0;
  71. test2 = 3.14/test1;
  72. test1 = -1.0;
  73. test2 = sqrt(test1);
  74.  
  75. return EXIT_SUCCESS;
  76. }
Sep 5 '07 #1
2 2036
rembremading wrote:
Hi all!
[snip]

You have several errors in your code. I rewrote it
like this, and compiled it with lcc-win32. It should
also compile with gcc since it is pure C99

Expand|Select|Wrap|Line Numbers
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <fenv.h>
  5. #include <setjmp.h>
  6. #include <signal.h>
  7. #include <math.h>
  8.  
  9.  
  10. #ifndef __SUPPORT_SNAN__
  11. #define __SUPPORT_SNAN__
  12. #endif
  13.  
  14. jmp_buf return_to_top_level;
  15.  
  16.  
  17. /* floating-point error handler */
  18. void fperror(int sig)
  19. {
  20.  
  21. int ret;
  22. int set_excepts;
  23. char temp[256];
  24.  
  25. strcpy(temp, "Floating point error: ");
  26.  
  27. set_excepts = fetestexcept(FE_INEXACT | FE_DIVBYZERO | FE_UNDERFLOW |
  28. FE_OVERFLOW);
  29.  
  30. if(set_excepts & FE_INEXACT)
  31. strcat(temp,"[Inexact] ");
  32.  
  33. if(set_excepts & FE_DIVBYZERO)
  34. strcat(temp,"[Divide by 0] ");
  35.  
  36. if(set_excepts & FE_UNDERFLOW)
  37. strcat(temp,"[Underflow] ");
  38.  
  39. if(set_excepts & FE_OVERFLOW)
  40. strcat(temp,"[Overflow] ");
  41.  
  42. if(set_excepts & FE_INVALID)
  43. strcat(temp,"[Invalid] ");
  44.  
  45. printf("\a%s\n", temp);
  46.  
  47. // NOT DEFINED keep_going = 0;
  48. longjmp(return_to_top_level, 1);
  49.  
  50. }
  51.  
  52. int main()
  53. {
  54.  
  55. double test1, test2;
  56. int ret;
  57. fenv_t env;
  58. fexcept_t flagp;
  59.  
  60. /* assigning exception handler */
  61.  
  62. signal(SIGFPE, fperror);
  63.  
  64. fegetexceptflag(&flagp,FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
  65. /* enableing floating point exception signaling */
  66. fesetexceptflag(&flagp,FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
  67.  
  68. /* set-up return address for errors */
  69. if(setjmp(return_to_top_level)){
  70. return 1;
  71. }
  72.  
  73. test1 = 0.0;
  74. test2 = 3.14/test1;
  75. fperror(0);
  76. test1 = -1.0;
  77. test2 = sqrt(test1);
  78. fperror(0);
  79.  
  80. return EXIT_SUCCESS;
  81. }
Sep 5 '07 #2
Yes it works!
I conclude, that the main error (appart from improper copy&paste) was, that
I forgot to define
fenv_t env;
fexcept_t flagp;
and to hand over &flagp to fegetexceptflag.
My gcc compiler did not complain. (but I dont wont to blame it)

Thank you.


jacob navia wrote:
rembremading wrote:
>Hi all!

[snip]

You have several errors in your code. I rewrote it
like this, and compiled it with lcc-win32. It should
also compile with gcc since it is pure C99

Expand|Select|Wrap|Line Numbers
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <fenv.h>
  5. #include <setjmp.h>
  6. #include <signal.h>
  7. #include <math.h>
  8. #ifndef __SUPPORT_SNAN__
  9.          #define __SUPPORT_SNAN__
  10. #endif
  11. jmp_buf return_to_top_level;
  12. /* floating-point error handler */
  13. void fperror(int sig)
  14. {
  15. int ret;
  16. int set_excepts;
  17. char temp[256];
  18. strcpy(temp, "Floating point error: ");
  19. set_excepts = fetestexcept(FE_INEXACT | FE_DIVBYZERO | FE_UNDERFLOW |
  20. FE_OVERFLOW);
  21. if(set_excepts & FE_INEXACT)
  22.          strcat(temp,"[Inexact] ");
  23. if(set_excepts & FE_DIVBYZERO)
  24.          strcat(temp,"[Divide by 0] ");
  25. if(set_excepts & FE_UNDERFLOW)
  26.          strcat(temp,"[Underflow] ");
  27. if(set_excepts & FE_OVERFLOW)
  28.          strcat(temp,"[Overflow] ");
  29. if(set_excepts & FE_INVALID)
  30.          strcat(temp,"[Invalid] ");
  31.          printf("\a%s\n", temp);
  32.          // NOT DEFINED keep_going = 0;
  33.          longjmp(return_to_top_level, 1);
  34. }
  35. int main()
  36. {
  37. double test1, test2;
  38. int ret;
  39. fenv_t env;
  40. fexcept_t flagp;
  41. /* assigning exception handler */
  42.    signal(SIGFPE, fperror);
  43.    fegetexceptflag(&flagp,FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
  44. /* enableing floating point exception signaling */
  45.    fesetexceptflag(&flagp,FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
  46. /* set-up return address for errors */
  47. if(setjmp(return_to_top_level)){
  48.          return 1;
  49. }
  50. test1 = 0.0;
  51. test2 = 3.14/test1;
  52. fperror(0);
  53. test1 = -1.0;
  54. test2 = sqrt(test1);
  55. fperror(0);
  56. return EXIT_SUCCESS;
  57. }
Sep 5 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andy Todd | last post by:
Hi We have just moved an ASP.NET application into the live environment which is as follows: Sun Proxy Server / Firewall Windows 2000 Server / IIS5 The URL for the site maps to the Sun...
4
by: John MacIntyre | last post by:
Hi, I have a page with a series of child pages loaded into an iframe. When I move from page to page, I store an object containing the child's control data in a variable on the main page, then...
6
by: kenneth fleckenstein nielsen | last post by:
Hi guru's It runs ok on my developmaschine, and on the test server that i've set up. but fails after installing on the customers server. I made a XML webservice that does these steps: a) access a...
7
by: SevDer | last post by:
Hi I have a class library that needs to download the HTML in a specific page of ours with provided querystring. When I open this URL with any browser, it loads fine. When I do WebRequest from Web...
1
by: comp.lang.php | last post by:
Consider my code: if ($this->isSuccessful && is_file($_FILES)) { // STEP 6: MOVE RESUME TO DIRECTORY $uuid = $this->sfug->getUUID(); if (!$uuid) $this->sfug->setUUID(); $uuid =...
2
by: Richard Hsu | last post by:
// code #include "stdio.h" int status(FILE * f) { printf("ftell:%d, feof:%s\n", ftell(f), feof(f) != 0 ? "true" : "false"); } int case1() { FILE * f = fopen("c:\\blah", "wb+"); int i = 5;
2
by: Anbu | last post by:
Sorry for cross posting the query. But I need a resolution as early as possible. I have developed an application to authenticate the user based on LDAP Search and authentication. The Windows...
12
by: Jim Rodgers | last post by:
I have a big asp file that has an error under certain conditions -- totally repeatable. However, it only fails when I set response.buffer = True at the top. WHen I set it False in order to debug...
2
by: =?Utf-8?B?YWxiZXJ0b3Nvcmlh?= | last post by:
Hi, I'm using Threads, and when I try to do Server.Transfer, I recieved an error. (child object does not exist...) My Code: Dim t As New Thread(AddressOf Hilo) Private Sub Hilo()...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.