Skip to content

Instantly share code, notes, and snippets.

@kosh04
Created July 17, 2014 16:13

Revisions

  1. kosh04 created this gist Jul 17, 2014.
    46 changes: 46 additions & 0 deletions test-matherr.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    // see also:
    // http://msdn.microsoft.com/ja-jp/library/k3backsw.aspx
    // http://linuxjm.sourceforge.jp/html/LDP_man-pages/man3/matherr.3.html

    #define _SVID_SOURCE
    #include <math.h>
    #include <string.h>
    #include <stdio.h>

    #ifdef _WIN32
    #define matherr _matherr
    #define exception _exception
    #endif

    int main(int argc, char **argv)
    {
    #ifdef __linux__
    _LIB_VERSION = _SVID_;
    #endif

    printf("log(-2.0) = %e\n", log(-2.0));
    printf("log10(-5.0) = %e\n", log10(-5.0));
    printf("log(0.0) = %e\n", log(0.0));

    return 0;
    }

    int matherr(struct exception *e)
    {
    if (e->type == DOMAIN) {
    if (strcmp(e->name, "log") == 0)
    {
    e->retval = log(-(e->arg1));
    fprintf(stderr, "Special: using absolute value: %s: _DOMAIN error\n", e->name);
    return 1;
    }
    else if (strcmp(e->name, "log10") == 0)
    {
    e->retval = log10(-(e->arg1));
    fprintf(stderr, "Special: using absolute value: %s _DOMAIN error\n", e->name);
    return 1;
    }
    }
    fprintf(stderr, "Normal: ");
    return 0;
    }