Detecting Math Errors

This is a discussion on Detecting Math Errors within the C++ Programming forums, part of the General Programming Boards category; Hi, How does one detect errors in math library routines using only methods available in the C/C++ standard libraries? For ...

  1. #1
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812

    Detecting Math Errors

    Hi,

    How does one detect errors in math library routines using only methods available in the C/C++ standard libraries?

    For example:

    double rslt = sqrt(-36);

    I know that in Windows there are numerous ways to do this using errno, _matherr, _isnan etc, but all these are not generally portable.

    Any help would be great?

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    The data passed as arguments to functions can come from two sources, the programmer who hard codes the argument or has the program calculate the value to be passed somewhere along the way and the user. The stuff that's hard coded by the programmer can't be checked---it's up to you to know what you are doing. The stuff calculated by the program can be checked for size and or sign before being passed. User input can be screened by a variety of techniques including accepting input only as strings and then parsing the string for errors before converting to appropriate numerical types and or direct input into type but screen with istream methods like good(), fail(), clear(), etc. You can screen for size, sign, etc once input is validated for type if desired.
    Last edited by elad; 03-06-2003 at 03:02 PM.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,701
    Apart from what elad has already posted, you could design an abstraction layer for general purpose math error handling - and implement the platform specifics for each platform you want to support.
    A better option would be to look for a portable C++ math class that has already solved this problem (probably by checking the parameters before passing them to the standard C math routines).

    gg

    [EDIT]
    I don't see the forest from the trees sometimes (or something like that).
    ANSI standard defines ERANGE and EDOM (and that's it) for errno, so any conforming C library should support them when calling standard math routines.
    Last edited by Codeplug; 03-06-2003 at 04:33 PM.

  4. #4
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    There doesn't appear to be any universal way of checking for error in maths routines? It appears I have to check validility of input parameters for every operation.

    Thanks for your help.

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: Detecting Math Errors

    Originally posted by Davros
    I know that in Windows there are numerous ways to do this using errno
    errno is not windows only. It's defined by the standard library. Any compliant compiler will support it.

  6. #6
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    My documentation states that errno is used only in DOS, Windows & OS2. But it then goes on to describe UNIX errno values and their compatiblity with Windows. This has confused me.

    You are saying I can check the errno value after each math call?
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  7. #7
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    It's completely standard. Just clear errno to 0, then call sqrt. If the number you provided was negitive it will set errno to EDOM (domain error).

  8. #8
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Thanks
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 04:11 PM
  2. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 11:50 AM
  3. Header File Errors...
    By Junior89 in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2007, 01:28 AM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21