delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2008/04/08/11:30:29

X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f
Date: Tue, 8 Apr 2008 11:30:26 -0400
Message-Id: <200804081530.m38FUQW8027275@envy.delorie.com>
From: DJ Delorie <dj AT delorie DOT com>
To: djgpp-workers AT delorie DOT com
In-reply-to: <200804081719.40163.juan.guerrero@gmx.de> (message from Juan
Manuel Guerrero on Tue, 8 Apr 2008 17:19:39 +0200)
Subject: Re: Bug fix for strtold
References: <200804081719 DOT 40163 DOT juan DOT guerrero AT gmx DOT de>
Reply-To: djgpp-workers AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

Linux parses what it can of the string, stopping at the first
non-numeric character.  Thus, "123.456 E+12" parses as 123.456 for all
three functions.

The Linux man page documents it that way; it converts "the initial
portion of the string".  *endptr points to the first character after
the number parsed.

DJGPP's documentation says the same: "This function converts as many
characters of @var{s} that look like a floating point number into that
number."

So, just stop at the space.  strtold is right, the other two need
fixing.



#define _XOPEN_SOURCE 600   /* or #define _ISOC99_SOURCE */
#include <stdio.h>
#include <stdlib.h>

main()
{
  float f;
  double d;
  long double l;
  char *str = "123.456 E+2";
  char *estr;

  f = strtof (str, &estr);
  d = f;
  printf("strtof %f %d\n", d, estr-str);

  d = strtod (str, &estr);
  printf("strtod %f %d\n", d, estr-str);

  l = strtold (str, &estr);
  d = l;
  printf("strtold %f %d\n", d, estr-str);
}

- Raw text -


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