12#if defined __cplusplus
17#define numberOfStaticDoubles ( 100 * 1000 )
19static nfu_status nfu_stringToListOfDoubles2(
char const *str, int64_t *numberConverted,
double **doublePtr,
char **endCharacter );
27 return( nfu_stringToListOfDoubles2( str, numberConverted, doublePtr, endCharacter ) );
32static nfu_status nfu_stringToListOfDoubles2(
char const *str, int64_t *numberConverted,
double **doublePtr,
char **endCharacter ) {
34 int64_t i1, i2, numberConverted_initial = *numberConverted;
39 staticDoubles[i1] = strtod( str, endCharacter );
40 if( str == (
char const *) *endCharacter ) {
41 if( *numberConverted > 0 ) {
42 if( ( *doublePtr = (
double *)
nfu_malloc( (
size_t) *numberConverted *
sizeof(
double ) ) ) == NULL ) status =
nfu_mallocError;
46 str = (
char const *) *endCharacter;
49 if( ( status ==
nfu_Okay ) && ( *doublePtr == NULL ) ) status = nfu_stringToListOfDoubles2( str, numberConverted, doublePtr, endCharacter );
50 if( *doublePtr != NULL ) {
51 double *doublePtr2 = &((*doublePtr)[numberConverted_initial]);
53 for( i2 = 0; i2 < i1; i2++, doublePtr2++ ) *doublePtr2 = staticDoubles[i2];
62 int n1, ne, nf, digitsRightOfPeriod_f, exponent;
63 char Str_e[512], Str_f[512], *Str_r = Str_e, Fmt[32], *e1, *e2;
64 const char *sign =
"";
68 if( !std::isfinite( value ) ) {
69 snprintf( Fmt,
sizeof Fmt,
"%%%sf", sign );
70 snprintf( Str_e,
sizeof Str_e, Fmt, value );
71 return( strdup( Str_e ) );
75 if( significantDigits < 0 ) significantDigits = 0;
76 if( significantDigits > 24 ) significantDigits = 24;
78 snprintf( Fmt,
sizeof Fmt,
"%%%s.%de", sign, significantDigits );
79 snprintf( Str_e,
sizeof Str_e, Fmt, value );
81 e1 = strchr( Str_e,
'e' );
82 if( significantDigits == 0 ) {
83 if( *(e1 - 1) !=
'.' ) {
88 for( ; e2 != e1; e2--, e3-- ) *e3 = *e2;
93 n1 = (int) strlen( Str_e ) - 1;
101 exponent = (int) strtol( e1, &e2, 10 );
102 if( exponent != 0 ) {
103 for( e1 = Str_e; *e1 != 0; e1++ ) ;
104 snprintf( e1,
sizeof Str_e,
"e%d", exponent );
106 digitsRightOfPeriod_f = significantDigits - exponent;
107 if( ( digitsRightOfPeriod_f > 25 ) || ( exponent > 50 ) )
return( strdup( Str_r ) );
108 if( digitsRightOfPeriod_f < 0 ) digitsRightOfPeriod_f = 0;
110 snprintf( Fmt,
sizeof Fmt,
"%%%s.%df", sign, digitsRightOfPeriod_f );
111 snprintf( Str_f,
sizeof Str_f, Fmt, value );
113 ne = (int) strlen( Str_e );
114 nf = (int) strlen( Str_f );
115 if( strchr( Str_f,
'.' ) != NULL ) {
117 if( Str_f[nf-1] ==
'.' ) {
128 if( ( nf + favorEFormBy ) < ne ) Str_r = Str_f;
130 return( strdup( Str_r ) );
133#if defined __cplusplus
#define numberOfStaticDoubles
#define nf_floatToShortestString_includeSign
enum nfu_status_e nfu_status
#define nf_floatToShortestString_trimZeros
nfu_status nfu_stringToListOfDoubles(char const *str, int64_t *numberConverted, double **doublePtr, char **endCharacter)
void * nfu_malloc(size_t size)
#define nf_floatToShortestString_keepPeriod
char * nf_floatToShortestString(double value, int significantDigits, int favorEFormBy, int flags)