19#define numberOfStaticDoubles ( 100 * 1000 )
21static double *nfu_stringToListOfDoubles2(
statusMessageReporting *smr,
char const *str,
char sep, int64_t *numberConverted,
22 char **endCharacter,
int useSystem_strtod );
27 char **endCharacter,
int useSystem_strtod ) {
29 if( strchr(
"0123456789.+-eE", sep ) != NULL ) {
35 *endCharacter = (
char *) str;
36 if( isspace( sep ) ) sep =
' ';
37 return( nfu_stringToListOfDoubles2( smr, str, sep, numberConverted, endCharacter, useSystem_strtod ) );
42static double *nfu_stringToListOfDoubles2(
statusMessageReporting *smr,
char const *str,
char sep, int64_t *numberConverted,
43 char **endCharacter,
int useSystem_strtod ) {
45 int64_t i1, i2, numberConverted_initial = *numberConverted;
46 double *doublePtr = NULL;
48 double (*_strtod)(
char const *str,
char **endCharacter );
52 if( staticDoubles == NULL ) {
60 _strtod = useSystem_strtod ? strtod :
nf_strtod;
63 if( *numberConverted == 0 ) {
64 staticDoubles[i1] = _strtod( str, endCharacter ); }
66 char const *str2 = str;
68 while( isspace( *str2 ) ) ++str2;
76 if( str < str2 ) staticDoubles[i1] = _strtod( str2, endCharacter );
77 if( str2 == (
char const *) *endCharacter ) *endCharacter = (
char *) str;
79 if( str == (
char const *) *endCharacter ) {
80 int64_t
number = *numberConverted;
81 if( *numberConverted == 0 )
number = 1;
82 if( ( doublePtr = (
double *)
smr_malloc2( smr, (
size_t) number *
sizeof(
double ), 0,
"doublePtr" ) ) == NULL ) {
85 free( staticDoubles );
91 str = (
char const *) *endCharacter;
94 if( ( status ==
nfu_Okay ) && ( doublePtr == NULL ) )
95 doublePtr = nfu_stringToListOfDoubles2( smr, str, sep, numberConverted, endCharacter, useSystem_strtod );
96 if( doublePtr != NULL ) {
97 double *doublePtr2 = &(doublePtr[numberConverted_initial]);
98 char *end = *endCharacter;
100 for( i2 = 0; i2 < i1; i2++, doublePtr2++ ) *doublePtr2 = staticDoubles[i2];
101 while( isspace( *end ) ) ++end;
102 if( *end == 0 ) *endCharacter = end;
106 free( staticDoubles );
111#define valid_digit( c ) ( ( c ) >= '0' && ( c ) <= '9')
115double nf_strtod(
char const *str,
char **endCharacter ) {
117 *endCharacter = (
char *) str;
118 char *ptr = *endCharacter;
120 while( isspace( *ptr ) ) ++ptr;
126 else if( *ptr ==
'+' ) {
131 for( ;
valid_digit( *ptr ); ++ptr ) value = value * 10.0 + ( *ptr -
'0' );
134 double invPow10 = 0.1;
137 value += ( *ptr -
'0' ) * invPow10;
143 if( ( *ptr ==
'e' ) || ( *ptr ==
'E' ) ) {
144 int negativeExponent = 0;
148 negativeExponent = 1;
150 else if( *ptr ==
'+' ) {
154 unsigned int exponent = 0;
155 for( ;
valid_digit( *ptr ); ++ptr ) exponent = exponent * 10 + ( *ptr -
'0' );
156 if( exponent > 308 ) {
157 return( strtod( str, endCharacter ) );
161 if( exponent == 0 ) negativeExponent = 0;
162 while( exponent >= 50 ) { scale *= 1E50; exponent -= 50; }
163 while( exponent >= 8 ) { scale *= 1E8; exponent -= 8; }
164 while( exponent > 0 ) { scale *= 10.0; exponent -= 1; }
166 if( negativeExponent ) scale = 1.0 / scale;
171 return( sign * value );
178 int n1, ne, nf, digitsRightOfPeriod_f, exponent;
179 char Str_e[512], Str_f[512], *Str_r = Str_e, Fmt[32], *e1, *e2;
180 const char *sign =
"";
184 if( !isfinite( value ) ) {
185 sprintf( Fmt,
"%%%sf", sign );
186 sprintf( Str_e, Fmt, value );
187 return( strdup( Str_e ) );
191 if( significantDigits < 0 ) significantDigits = 0;
192 if( significantDigits > 24 ) significantDigits = 24;
194 sprintf( Fmt,
"%%%s.%de", sign, significantDigits );
195 sprintf( Str_e, Fmt, value );
197 e1 = strchr( Str_e,
'e' );
198 if( significantDigits == 0 ) {
199 if( *(e1 - 1) !=
'.' ) {
202 e2 = strchr( e1, 0 );
204 for( ; e2 != e1; e2--, e3-- ) *e3 = *e2;
209 n1 = (int) strlen( Str_e ) - 1;
217 exponent = (int) strtol( e1, &e2, 10 );
218 if( exponent != 0 ) {
219 for( e1 = Str_e; *e1 != 0; e1++ ) ;
220 sprintf( e1,
"e%d", exponent );
222 digitsRightOfPeriod_f = significantDigits - exponent;
223 if( ( digitsRightOfPeriod_f > 25 ) || ( exponent > 50 ) )
return( strdup( Str_r ) );
224 if( digitsRightOfPeriod_f < 0 ) digitsRightOfPeriod_f = 0;
226 sprintf( Fmt,
"%%%s.%df", sign, digitsRightOfPeriod_f );
227 sprintf( Str_f, Fmt, value );
229 ne = (int) strlen( Str_e );
230 nf = (int) strlen( Str_f );
231 if( strchr( Str_f,
'.' ) != NULL ) {
233 if( Str_f[nf-1] ==
'.' ) {
244 if( ( nf + favorEFormBy ) < ne ) Str_r = Str_f;
246 return( strdup( Str_r ) );
double * nfu_stringToListOfDoubles(statusMessageReporting *smr, char const *str, char sep, int64_t *numberConverted, char **endCharacter, int useSystem_strtod)
#define numberOfStaticDoubles
char * nf_floatToShortestString(double value, int significantDigits, int favorEFormBy, int flags)
double nf_strtod(char const *str, char **endCharacter)
#define nf_floatToShortestString_includeSign
enum nfu_status_e nfu_status
#define nf_floatToShortestString_trimZeros
double nf_strtod(char const *ptr, char **endCharacter)
#define nf_floatToShortestString_keepPeriod
#define smr_setReportError2(smr, libraryID, code, fmt,...)
#define smr_setReportError2p(smr, libraryID, code, fmt)
#define smr_malloc2(smr, size, zero, forItem)