Bifalgorithm post · Computer science one · POSIX Error Codes

How can errno and strerror be used to identify mathematical errors?

How can errno and strerror be used to identify mathematical errors? (Computer science one · POSIX Error Codes) — "The output of this program is as follows. res…

Ideas in this flowchart

  • Gestión de errores matemáticos mediante errno.h en C
  • El programa muestra cómo detectar y describir errores producidos por funciones matemáticas.
  • sqrt(2) produce el resultado 1.4142 sin error.
  • sqrt(-1) produce -nan y establece el error EDOM.
  • errno se compara con la macro EDOM para identificar el error de dominio.
  • strerror(errno) obtiene el mensaje «Numerical argument out of domain».
  • log(0) produce -inf y establece el error ERANGE.
  • errno se compara con la macro ERANGE para identificar el error de rango.
  • strerror(errno) obtiene el mensaje «Numerical result out of range».
  • Los códigos EDOM y ERANGE valen 33 y 34 en este sistema, pero pueden variar en otros.
  • Para mantener la portabilidad, las comparaciones deben usar los nombres de macro.
Feed/

How can errno and strerror be used to identify mathematical errors?

No cover

Channel

Computer science one

1 page

"The output of this program is as follows. result: 1.4142, error: 0 result: -nan, error: 33 it was an EDOM error Error Message: Numerical argument out of domain result: -inf, error: 34 it was an ERANGE error Error Message: Numerical result out of range For this particular system, the EDOM and ERANGE error codes were associated with the integer values 33 and 34 respectively. These numbers are not necessarily the same on all systems so comparisons must be made against the macro names for portability.\n\nCode Sample 19.1.: Using the errno.h library\n\n#include <stdio.h>\n\n#include <stdlib.h>\n\n#include <math.h>\n\n#include <string.h>\n\n#include <errno.h>\n\nint main(int argc, char **argv) {\n\ndouble a = -1, b = 2, c = 0.0;\n\ndouble x;\n\n//okay\n\nx = sqrt(b);\n\nprintf(\"result: %.4f, error: %d\n\n\", x, errno);\n\n//NaN and EDOM error...\n\nx = sqrt(a);\n\nprintf(\"result: %.4f, error: %d\n\n\", x, errno);\n\n//make a comparison\n\nif(errno == EDOM) {\n\nprintf(\"it was an EDOM error\n\n\");\n\n}\n\n//error messages can be accessed via:\n\nchar *errMsg = strerror(errno);\n\nprintf(\"Error Message: %s\n\n\", errMsg);\n\n//ERANGE error\n\nx = log(c);\n\nprintf(\"result: %.4f, error: %d\n\n\", x, errno);\n\nif (errno == ERANGE) {\n\nprintf(\"it was an ERANGE error\n\n\");\n\n}\n\n//error messages can be accessed via:\n\nerrMsg = strerror(errno);\n\nprintf(\"Error Message: %s\n\n\", errMsg);\n\nreturn 0;\n\n}"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.