Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
nf_stringToDoubles.c File Reference
#include <stdlib.h>
#include <ctype.h>
#include "nf_utilities.h"

Go to the source code of this file.

Macros

#define numberOfStaticDoubles   ( 100 * 1000 )
#define valid_digit(c)

Functions

double * nfu_stringToListOfDoubles (statusMessageReporting *smr, char const *str, char sep, int64_t *numberConverted, char **endCharacter, int useSystem_strtod)
double nf_strtod (char const *str, char **endCharacter)
char * nf_floatToShortestString (double value, int significantDigits, int favorEFormBy, int flags)

Macro Definition Documentation

◆ numberOfStaticDoubles

#define numberOfStaticDoubles   ( 100 * 1000 )

Definition at line 19 of file nf_stringToDoubles.c.

◆ valid_digit

#define valid_digit ( c)
Value:
( ( c ) >= '0' && ( c ) <= '9')

Definition at line 111 of file nf_stringToDoubles.c.

Referenced by nf_strtod().

Function Documentation

◆ nf_floatToShortestString()

char * nf_floatToShortestString ( double value,
int significantDigits,
int favorEFormBy,
int flags )

Definition at line 176 of file nf_stringToDoubles.c.

176 {
177
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 = "";
181
182 if( flags & nf_floatToShortestString_includeSign ) sign = "+";
183
184 if( !isfinite( value ) ) {
185 sprintf( Fmt, "%%%sf", sign );
186 sprintf( Str_e, Fmt, value );
187 return( strdup( Str_e ) );
188 }
189
190 significantDigits--;
191 if( significantDigits < 0 ) significantDigits = 0;
192 if( significantDigits > 24 ) significantDigits = 24;
193
194 sprintf( Fmt, "%%%s.%de", sign, significantDigits );
195 sprintf( Str_e, Fmt, value );
196
197 e1 = strchr( Str_e, 'e' );
198 if( significantDigits == 0 ) {
199 if( *(e1 - 1) != '.' ) {
200 char *e3;
201
202 e2 = strchr( e1, 0 );
203 e3 = e2 + 1;
204 for( ; e2 != e1; e2--, e3-- ) *e3 = *e2;
205 *(e1++) = '.';
206 }
207 }
208 *e1 = 0;
209 n1 = (int) strlen( Str_e ) - 1;
210 if( flags & nf_floatToShortestString_trimZeros ) while( Str_e[n1] == '0' ) n1--;
212 if( !( flags & nf_floatToShortestString_keepPeriod ) ) if( Str_e[n1] == '.' ) n1--;
213 n1++;
214 Str_e[n1] = 0;
215
216 e1++;
217 exponent = (int) strtol( e1, &e2, 10 );
218 if( exponent != 0 ) { /* If 0, the exponent was "e+00". */
219 for( e1 = Str_e; *e1 != 0; e1++ ) ;
220 sprintf( e1, "e%d", exponent );
221
222 digitsRightOfPeriod_f = significantDigits - exponent;
223 if( ( digitsRightOfPeriod_f > 25 ) || ( exponent > 50 ) ) return( strdup( Str_r ) );
224 if( digitsRightOfPeriod_f < 0 ) digitsRightOfPeriod_f = 0;
225
226 sprintf( Fmt, "%%%s.%df", sign, digitsRightOfPeriod_f );
227 sprintf( Str_f, Fmt, value );
228
229 ne = (int) strlen( Str_e );
230 nf = (int) strlen( Str_f );
231 if( strchr( Str_f, '.' ) != NULL ) { /* '.' in string. */
232 if( flags & nf_floatToShortestString_trimZeros ) while( Str_f[nf-1] == '0' ) nf--;
233 if( Str_f[nf-1] == '.' ) {
234 if( !( flags & nf_floatToShortestString_keepPeriod ) ) nf--;
235 } }
236 else { /* Maybe we want a '.' else it looks like an integer, "12345." vs "12345". */
238 Str_f[nf] = '.';
239 nf++;
240 }
241 }
242 Str_f[nf] = 0;
243
244 if( ( nf + favorEFormBy ) < ne ) Str_r = Str_f;
245 }
246 return( strdup( Str_r ) );
247}
G4int sign(const T t)
#define nf_floatToShortestString_includeSign
#define nf_floatToShortestString_trimZeros
#define nf_floatToShortestString_keepPeriod

Referenced by LUPI::Misc::doubleToShortestString().

◆ nf_strtod()

double nf_strtod ( char const * str,
char ** endCharacter )

Definition at line 115 of file nf_stringToDoubles.c.

115 {
116
117 *endCharacter = (char *) str;
118 char *ptr = *endCharacter;
119
120 while( isspace( *ptr ) ) ++ptr; /* Skip leading white space, if any. */
121
122 double sign = 1.0; /* Get sign, if any. */
123 if( *ptr == '-' ) {
124 sign = -1.0;
125 ++ptr; }
126 else if( *ptr == '+' ) {
127 ++ptr;
128 }
129
130 double value = 0.0; /* Get digits before decimal point or exponent, if any. */
131 for( ; valid_digit( *ptr ); ++ptr ) value = value * 10.0 + ( *ptr - '0' );
132
133 if( *ptr == '.' ) { /* Get digits after decimal point, if any. */
134 double invPow10 = 0.1;
135 ++ptr;
136 while( valid_digit( *ptr ) ) {
137 value += ( *ptr - '0' ) * invPow10;
138 invPow10 *= 0.1;
139 ++ptr;
140 }
141 }
142
143 if( ( *ptr == 'e' ) || ( *ptr == 'E' ) ) { /* Handle exponent, if any. */
144 int negativeExponent = 0;
145
146 ++ptr; /* Get sign of exponent, if any. */
147 if( *ptr == '-' ) {
148 negativeExponent = 1;
149 ++ptr; }
150 else if( *ptr == '+' ) {
151 ++ptr;
152 }
153
154 unsigned int exponent = 0; /* Get digits of exponent. There must be at least 1. */
155 for( ; valid_digit( *ptr ); ++ptr ) exponent = exponent * 10 + ( *ptr - '0' );
156 if( exponent > 308 ) {
157 return( strtod( str, endCharacter ) );
158 }
159
160 double scale = 1.0; /* Calculate scaling factor. */
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; }
165
166 if( negativeExponent ) scale = 1.0 / scale;
167 value *= scale;
168 }
169
170 *endCharacter = ptr;
171 return( sign * value );
172}
#define valid_digit(c)

◆ nfu_stringToListOfDoubles()

double * nfu_stringToListOfDoubles ( statusMessageReporting * smr,
char const * str,
char sep,
int64_t * numberConverted,
char ** endCharacter,
int useSystem_strtod )

Definition at line 26 of file nf_stringToDoubles.c.

27 {
28
29 if( strchr( "0123456789.+-eE", sep ) != NULL ) {
30 smr_setReportError2( smr, nfu_SMR_libraryID, nfu_badInput, "Invalid sep ='%c'.", sep );
31 return( NULL );
32 }
33
34 *numberConverted = 0;
35 *endCharacter = (char *) str;
36 if( isspace( sep ) ) sep = ' '; /* Make it the space character if any white space as it simplifies logic below. */
37 return( nfu_stringToListOfDoubles2( smr, str, sep, numberConverted, endCharacter, useSystem_strtod ) );
38}
@ nfu_badInput
int nfu_SMR_libraryID
#define smr_setReportError2(smr, libraryID, code, fmt,...)

Referenced by MCGIDI::convertACE_URR_probabilityTablesFromGIDI(), ptwX_fromString(), and ptwXY_fromString().