The strtod(3) Function

The strtod(3) function is used to perform string-to-floating point conversions. This function is quite similar in operation to the integer conversion functions just covered, but it has a few new wrinkles. The synopsis for strtod(3) is as follows:

#include <stdlib.h>
#include <math.h>

double strtod(const char *nptr, char **endptr);

Note that there is no base argument. No radix conversions are available for floating-point conversions other than base 10.

The input string nptr and the second argument endptr are used in precisely the same way they are used in the strtol(3) function.

Using the strtod(3) Function

The following shows how the strtod(3) function can be used to convert a floating-point value in a string buffer to the C double type:

static char buf[] = "-32000.009E+01";
char *ep;               /* Returned pointer */
double dval;            /* Converted value */

dval = strtod(buf,&ep); /* Convert buf to double */

The input string is converted and the floating-point result is returned and assigned to the variable dval. The point where the conversion ends is passed back to the caller by storing the pointer in pointer variable ep. In this example, ep should end up pointing to the null byte at the end of the buf[] array.

Testing for Math Errors

This function adds a new twist to overflow and underflow detection. In order to test for overflows and underflows, you must include the file <math.h>:

#include <math.h>

This include file defines the macro HUGE_VAL, which will be needed in the tests. Three return values from strtod(3) require further investigation by the program:

  • +HUGE_VAL

  • 0.0

  • -HUGE_VAL

The test procedure will rely on the fact that the errno value is cleared before calling strtod(3).

Testing for Overflow

When the value +HUGE_VAL is returned, you must check errno to see if the value ERANGE was posted there. If errno is set to ERANGE, then the conversion process had an overflow. If errno remains cleared to zero, then the value returned is a valid number.

Testing for Underflow

When the value -HUGE_VAL is returned, you must also check errno to see if the error ERANGE was posted there. If errno remains cleared to zero, then the returned value is a valid number.

Testing for Exponent Underflow

The function strtod(3) returns 0.0 when the converted value is extremely small in value fractionally—so small that the underlying data type cannot represent it. When zero is returned and ERANGE is posted to the errno variable, then it is known that the conversion failed because the input value was too small a fraction to represent. Another way to state this is that the exponent value underflowed.

Handling Exponent Underflow

In many cases, you might be happy just to round that small fractional value to zero and move on. However, this may not be suitable for all applications, especially scientific ones.

A scientific model may depend on the precision of that variable to represent a very small value. If precision is maintained, then that value might be later multiplied by a large value to compute a reasonable result.

However, if you simply allow the value to round to zero, then the multiplied result will be zero also—leading to an incorrect answer. Thus, it is better to abort the computation and point out that the value could not be contained with the necessary precision.

Warning

Consider any computation that leads to a small result that does not quite underflow to be unstable. A significant loss of precision is likely when operating in this extreme exponent range.


Flowchart of Math Error Tests

The entire procedure for math error testing for strtod(3) is shown in Figure 10.1 as a flowchart. This should help summarize the overflow and underflow detection logic that should be used.

Figure 10.1. Testing for overflow and underflow after calling strtod(3).


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.153.69