Math library functions and error checking

Henry Spencer henry at utzoo.uucp
Tue May 8 08:35:55 AEST 1990


In article <1323 at tub.UUCP> net at tub.UUCP (Oliver Laumann) writes:
>What is the correct method to check whether an exception (overflow,
>invalid arguments, etc.) occurred during the execution of a C math
>library function (such as sqrt() or sin())?
>
>Do I have to test the result against NaN, or do I have to check errno
>(EDOM)?  If the latter is true, is errno cleared automatically on
>success or do I have to clear it before calling the function?

There is no simple, uniform, standard way of testing for trouble in the math
functions.  You have to look at the return value of each function in
combination with errno.  No, the functions do not clear errno on success,
and in certain circumstances they are not guaranteed to set it on failure.
No, they do not return NaN, since that is not a fully portable concept.

The whole area is kind of a mess, especially since even the unsatisfactory
existing state of things does not satisfy the high-performance fanatics,
who would prefer (last I heard) to get back something like a NaN rather
than having to test values and flags every time.  There are people looking
at better approaches.
-- 
If OSI is the answer, what is |     Henry Spencer at U of Toronto Zoology
the question?? -Rolf Nordhagen| uunet!attcan!utzoo!henry henry at zoo.toronto.edu



More information about the Comp.lang.c mailing list