delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2003/03/15/13:31:42

Sender: rich AT phekda DOT freeserve DOT co DOT uk
Message-ID: <3E736F69.93B74D9C@phekda.freeserve.co.uk>
Date: Sat, 15 Mar 2003 18:22:33 +0000
From: Richard Dawe <rich AT phekda DOT freeserve DOT co DOT uk>
X-Mailer: Mozilla 4.77 [en] (X11; U; Linux 2.2.23 i586)
X-Accept-Language: de,fr
MIME-Version: 1.0
To: djgpp-workers AT delorie DOT com
Subject: Re: strto{d,f,ld}, inf and nan patch
References: <200303142003 DOT h2EK3NH09554 AT speedy DOT ludd DOT luth DOT se>
Reply-To: djgpp-workers AT delorie DOT com

Hello.

ams AT ludd DOT luth DOT se wrote:
> Ok, this is what I have for inf and nan support for strto{d,f,ld}.
> Now all three of them are done. Many added test cases for
> strtof(). Somebody else might want to add some in the endptr category.
> 
> No test cases at all for strtold(). Sorry. There weren't any before
> either.

I don't think that should stop the patch going in. Although it would be nice
if there were test cases.

Maybe you could duplicate, say, t-strtof and change "float" to "long double",
strtof to strtold. That might not be much work...

> I'm unsure if my markup in wc204.txi is good.
> 
> Comments?

Comments inline.

[snip]
> Index: djgpp/include/math.h
> ===================================================================
> RCS file: /cvs/djgpp/djgpp/include/math.h,v
> retrieving revision 1.7
> diff -p -u -r1.7 math.h
> --- djgpp/include/math.h        20 Feb 2003 19:04:02 -0000      1.7
> +++ djgpp/include/math.h        14 Mar 2003 19:53:52 -0000
> @@ -51,6 +51,11 @@ extern long double __dj_huge_vall;
>  #define HUGE_VALF __dj_huge_valf
>  #define HUGE_VALL __dj_huge_vall
> 
> +#define INFINITY  HUGE_VALF
> +
> +extern float       __dj_nan;
> +#define NAN        __dj_nan
> +
>  #endif /* (__STDC_VERSION__ >= 199901L) || !__STRICT_ANSI__ */
> 
>  #ifndef __STRICT_ANSI__
> Index: djgpp/src/docs/kb/wc204.txi
> ===================================================================
> RCS file: /cvs/djgpp/djgpp/src/docs/kb/wc204.txi,v
> retrieving revision 1.149
> diff -p -u -r1.149 wc204.txi
> --- djgpp/src/docs/kb/wc204.txi 14 Mar 2003 19:11:31 -0000      1.149
> +++ djgpp/src/docs/kb/wc204.txi 14 Mar 2003 19:54:00 -0000
> @@ -917,3 +917,10 @@ The functions @code{readv} and @code{wri
> 
>  @findex pwrite
>  The function @code{pwrite} was added.
> +
> +@findex strtod AT r{, inf and nan in input}
> +@findex strtof AT r{, inf and nan in input}
> +@findex strtold AT r{, inf and nan in input}
> +The functions @code{strtod}, @code{strtof} and @code{strtold} now
> +understand ``inf'', ``infinity'', ``nan'' and ``nan(<anything>)'' in the
> +input string.

I think you should list _strtold too.

Maybe Inf, NaN should be capitalised in the same way as they are by printf?
(Inf and NaN.)

Maybe you could mention the fact that Inf, NaN are matched case-insensitively?

Also, we seem to understand nan(<anything>), but ignore it.

> Index: djgpp/src/libc/ansi/stdlib/strtod.c
> ===================================================================
> RCS file: /cvs/djgpp/djgpp/src/libc/ansi/stdlib/strtod.c,v
> retrieving revision 1.5
> diff -p -u -r1.5 strtod.c
> --- djgpp/src/libc/ansi/stdlib/strtod.c 17 Oct 2002 23:00:24 -0000      1.5
> +++ djgpp/src/libc/ansi/stdlib/strtod.c 14 Mar 2003 19:54:01 -0000
> @@ -1,13 +1,16 @@
> +/* Copyright (C) 2003 DJ Delorie, see COPYING.DJ for details */
>  /* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */
>  /* Copyright (C) 1998 DJ Delorie, see COPYING.DJ for details */
>  /* Copyright (C) 1997 DJ Delorie, see COPYING.DJ for details */
>  /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
>  /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
> +#include <libc/stubs.h>
>  #include <math.h>
>  #include <stdlib.h>
>  #include <float.h>
>  #include <errno.h>
>  #include <ctype.h>
> +#include <string.h>
>  #include <libc/unconst.h>
> 
>  double
> @@ -32,6 +35,7 @@ strtod(const char *s, char **sret)
>    while (isspace((unsigned char) *s))
>      s++;
> 
> +  /* Handle leading sign. */
>    if (*s == '+')
>      s++;
>    else if (*s == '-')
> @@ -40,6 +44,66 @@ strtod(const char *s, char **sret)
>      s++;
>    }
> 
> +  /* Handle INF and INFINITY. */
> +  if ( ! strnicmp( "INF", s, 3 ) )
> +  {
> +    if( sret )
> +    {
> +      if ( ! strnicmp( "INITY", &s[3], 5 ) )
> +      {
> +       *sret = unconst((&s[8]), char *);
> +      }
> +      else
> +      {
> +       *sret = unconst((&s[3]), char *);
> +      }
> +    }
> +
> +    if( 0 <= sign )
> +    {
> +      return INFINITY;
> +    }
> +    else
> +    {
> +      return -INFINITY;
> +    }
> +  }
> +
> +  /* Handle NAN and NAN(<whatever>). */
> +  if ( ! strnicmp( "NAN", s, 3 ) )
> +  {
> +    if( s[3] == '(' )
> +    {
> +      int end_of_n_char_sequence = 4;
> +
> +      while( s[end_of_n_char_sequence] != 0 &&
> +            s[end_of_n_char_sequence] != ')' )
> +      {
> +       end_of_n_char_sequence++;
> +      }
> +      if( s[end_of_n_char_sequence] == ')' )
> +      {
> +       /* If we are going to support "nan(0x1234) for setting specific bits,
> +        * that code goes here. Something like "bits = strtoul( &s[4], &end_p,
> +        * 0);".
> +        */
[snip]

Maybe you should mark this (and similar) comments with a TODO or FIXME? FIXME
seems to be the preferred to-do marker in the sources.

The diff didn't seem to include src/libc/c99/math/nan.c.

Other than that, the code looks fine to me.

Do the docs for strto* need updating? Maybe we could add a portability note
saying that support for Inf, NaN, NaN() is C99-specific?

Thanks, bye, Rich =]

-- 
Richard Dawe [ http://www.phekda.freeserve.co.uk/richdawe/ ]

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019