summaryrefslogtreecommitdiff
path: root/lib/libc/stdlib
diff options
context:
space:
mode:
authorriastradh <riastradh@NetBSD.org>2016-11-04 19:10:04 +0000
committerriastradh <riastradh@NetBSD.org>2016-11-04 19:10:04 +0000
commit8c1c14d98f020b139cdbddfe06cc7773ccbc1176 (patch)
tree9c90940485fe20833d23132b6ce1dd96376592dd /lib/libc/stdlib
parentc52085b2b01e9b3f8b6a575c6fd5ffcb926faae1 (diff)
Add example for strtod.
This illustrates all the cases you might be interested in and asserts theorems in those cases.
Diffstat (limited to 'lib/libc/stdlib')
-rw-r--r--lib/libc/stdlib/strtod.340
1 files changed, 39 insertions, 1 deletions
diff --git a/lib/libc/stdlib/strtod.3 b/lib/libc/stdlib/strtod.3
index 795d5679b7d..4eee9a44eaf 100644
--- a/lib/libc/stdlib/strtod.3
+++ b/lib/libc/stdlib/strtod.3
@@ -1,4 +1,4 @@
-.\" $NetBSD: strtod.3,v 1.23 2016/11/04 18:46:15 riastradh Exp $
+.\" $NetBSD: strtod.3,v 1.24 2016/11/04 19:10:04 riastradh Exp $
.\"
.\" Copyright (c) 1990, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@@ -170,6 +170,44 @@ the closest subnormal value, or zero, is returned, and
.Dv ERANGE
is stored in
.Va errno .
+.Sh EXAMPLES
+Since there is no out-of-band sentinel value to indicate an error,
+callers who wish to know whether there was overflow or underflow must
+set
+.Va errno
+to zero before calling
+.Fn strtod ,
+.Fn strtof ,
+or
+.Fn strtold ;
+in the case of no underflow or overflow, these functions preserve
+.Va errno .
+.Pp
+To check for syntax errors, callers must
+.Em also
+check whether
+.Fa endptr
+was updated to reflect the true end of the string in order to determine
+whether the full string was consumed or whether there were additional
+erroneous characters in it.
+.Bd -literal -offset abcd
+char *end;
+double d;
+
+\&...
+
+errno = 0;
+d = strtod(s, &end);
+if (s[0] == '\e0' || end[0] != '\e0')
+ errx(1, "invalid syntax");
+if (errno) {
+ assert(errno == ERANGE);
+ assert(d == HUGE_VAL || d == -HUGE_VAL || d == 0 ||
+ fpclassify(d) == FP_SUBNORMAL);
+ warnx("%s", d == HUGE_VAL ? "overflow" : "underflow");
+}
+/* d is the best floating-point approximation to the number in s */
+.Ed
.Sh ERRORS
.Bl -tag -width Er
.It Bq Er ERANGE