From owner-freebsd-standards Sat Feb 8 5:58:44 2003 Delivered-To: freebsd-standards@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 91C8F37B401 for ; Sat, 8 Feb 2003 05:58:36 -0800 (PST) Received: from thufir.bluecom.no (thufir.bluecom.no [217.118.32.12]) by mx1.FreeBSD.org (Postfix) with ESMTP id 2E22243F3F for ; Sat, 8 Feb 2003 05:58:35 -0800 (PST) (envelope-from eirik@eirikn.net) Received: from eirikn.net (a217-118-47-122.bluecom.no [217.118.47.122]) by thufir.bluecom.no (Postfix) with ESMTP id 7ADE450EC45 for ; Sat, 8 Feb 2003 14:58:27 +0100 (CET) Received: by eirikn.net (Postfix, from userid 1001) id 4FBC861; Sat, 8 Feb 2003 14:59:21 +0100 (CET) Date: Sat, 8 Feb 2003 14:59:21 +0100 From: Eirik Nygaard To: standards@FreeBSD.ORG Subject: strtof() Message-ID: <20030208135921.GA72899@eirikn.net> Mail-Followup-To: standards@FreeBSD.ORG Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="V0207lvV8h4k8FAm" Content-Disposition: inline User-Agent: Mutt/1.5.3i Sender: owner-freebsd-standards@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG --V0207lvV8h4k8FAm Content-Type: multipart/mixed; boundary="fUYQa+Pmc3FrFX/N" Content-Disposition: inline --fUYQa+Pmc3FrFX/N Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable I tave made a strtof(3) function. Attached is the function, a small test program and the man page. I will also start on the strtold(3) now. --=20 Eirik Nygaard PGP Key: 83C55EDE --fUYQa+Pmc3FrFX/N Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="strtof.c" Content-Transfer-Encoding: quoted-printable /* * * Copyright (c) 2003 Eirik Nygaard * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPO= SE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTI= AL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRI= CT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include float strtof(const char * __restrict nptr, char ** __restrict endptr) { char c; const char *s; float acc, decnum; int neg, any, dec, deccnt, a; =09 s =3D nptr; do { c =3D *s++; } while (isspace((unsigned char)c)); if (c =3D=3D '-') { neg =3D 1; c =3D *s++; } else { neg =3D 0; if (c =3D=3D '+') c =3D *s++; } =09 any =3D acc =3D dec =3D 0; for ( ; ; c =3D *s++) { if (c =3D=3D '.') { dec =3D 1; deccnt =3D 0; continue; } if (c >=3D '0' && c <=3D '9') c -=3D '0'; else break; any =3D 1; if (dec) { deccnt++; a =3D deccnt; decnum =3D c; for (; a > 0; a--) decnum /=3D 10; if ((FLT_MAX - decnum < acc) && !neg) { any =3D -1; break; } if ((FLT_MIN + decnum < acc) && neg) { any =3D -1; break; } else { if (neg) acc -=3D decnum; else acc +=3D decnum; } } else { any =3D 1; acc *=3D 10; if ((FLT_MAX - c < acc) && !neg) { any =3D -1; break; } if ((FLT_MIN + c < acc) && neg) { any =3D -1; break; } else { if (neg) acc -=3D c; else acc +=3D c; } } } if (any < 0) { acc =3D neg ? FLT_MIN : FLT_MAX; errno =3D ERANGE; } else if (!any) errno =3D EINVAL; =09 if (endptr !=3D NULL) *endptr =3D (char *)(any ? s - 1 : nptr); return (acc); } --fUYQa+Pmc3FrFX/N Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="strtof.3" .\" Copyright (c) 2003 Eirik Nygaard .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" $FreeBSD$ .\" .Dd February 8, 2003 .Dt STRTOF 3 .Os .Sh NAME .Nm strtof .Nd "convert a string value to a" .Vt float .Sh LIBRARY .Lb libc .Sh SYNOPSIS .In stdlib.h .Ft float .Fn strtof "const char * restrict nptr" "char ** restrict endptr" .Sh DESCRIPTION The .Fn strtof function converts the string in .Fa nptr to a .Vt float value. .Pp The string may begin with an arbitrary amount of white space (as determined by .Xr isspace 3 ) followed by a single optional .Ql + or .Ql - sign. .Pp The remainder of the string is converted to a .Vt float value in the obvious manner, stopping at the first character which is not a valid digit. .Pp If .Fa endptr is not .Dv NULL , .Fn strtof stores the address of the first invalid character in .Fa *endptr . If there were no digits at all, however, .Fn strtof stores the original value of .Fa nptr in .Fa *endptr . (Thus, if .Fa *nptr is not .Ql \e0 but .Fa **endptr is .Ql \e0 on return, the entire string was valid.) .Sh RETURN VALUES The .Fn strtof , function return the result of the conversion, unless the value would underflow or overflow. If no conversion could be performed, 0 is returned and the global variable .Va errno is set to .Er EINVAL . If an overflow or underflow occurs, .Va errno is set to .Er ERANGE and the function return value is clamped according to the following table. .Bl -column -offset indent ".Fn strtoimax" ".Sy overflow" ".Sy underflow" .It Sy Function Ta Sy overflow Ta Sy underflow .It Fn strtof Ta Dv FLT_MIN Ta Dv FLT_MAX .El .Sh ERRORS .Bl -tag -width Er .It Bq Er EINVAL The value of .Fa base is not supported or no conversion could be performed. .It Bq Er ERANGE The given string was out of range; the value converted has been clamped. .El .Sh SEE ALSO .Xr atof 3 , .Xr atoi 3 , .Xr atol 3 , .Xr strtod 3 , .Xr strtoul 3 , .Xr strtol 3 , .Xr wcstol 3 .Sh STANDARDS The .Fn strtof function conform to .St -isoC-99 . --fUYQa+Pmc3FrFX/N Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="mainstrtof.c" #include #include #include int main () { char *endp; float f; f = strtof("-5214.452", &endp); if (errno == ERANGE) printf("ERANGE\n"); else if (errno == EINVAL) printf("EINVAL\n"); else printf("It's a float with value %f\n", f); return 0; } --fUYQa+Pmc3FrFX/N-- --V0207lvV8h4k8FAm Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (FreeBSD) iD8DBQE+RQ041JB0Z4PFXt4RAkWrAJ4vFB2in8LMK96RwR0Dnx27Iow9pQCfYjmK jK1lrlDu/f+tSW4VlxBzAtM= =M8ed -----END PGP SIGNATURE----- --V0207lvV8h4k8FAm-- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-standards" in the body of the message