Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
nf_stringToDoubles.cc File Reference
#include <stdlib.h>
#include <float.h>
#include <cmath>
#include "nf_utilities.h"

Go to the source code of this file.

Macros

#define numberOfStaticDoubles   ( 100 * 1000 )
 

Functions

nfu_status nfu_stringToListOfDoubles (char const *str, int64_t *numberConverted, double **doublePtr, char **endCharacter)
 
char * nf_floatToShortestString (double value, int significantDigits, int favorEFormBy, int flags)
 

Macro Definition Documentation

◆ numberOfStaticDoubles

#define numberOfStaticDoubles   ( 100 * 1000 )

Definition at line 17 of file nf_stringToDoubles.cc.

Function Documentation

◆ nf_floatToShortestString()

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

Definition at line 60 of file nf_stringToDoubles.cc.

60 {
61
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 = "";
65
67
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 ) );
72 }
73
74 significantDigits--;
75 if( significantDigits < 0 ) significantDigits = 0;
76 if( significantDigits > 24 ) significantDigits = 24;
77
78 snprintf( Fmt, sizeof Fmt, "%%%s.%de", sign, significantDigits );
79 snprintf( Str_e, sizeof Str_e, Fmt, value );
80
81 e1 = strchr( Str_e, 'e' );
82 if( significantDigits == 0 ) {
83 if( *(e1 - 1) != '.' ) {
84 char *e3;
85
86 e2 = strchr( e1, 0 );
87 e3 = e2 + 1;
88 for( ; e2 != e1; e2--, e3-- ) *e3 = *e2;
89 *(e1++) = '.';
90 }
91 }
92 *e1 = 0;
93 n1 = (int) strlen( Str_e ) - 1;
94 if( flags & nf_floatToShortestString_trimZeros ) while( Str_e[n1] == '0' ) n1--; // Loop checking, 11.06.2015, T. Koi
96 if( !( flags & nf_floatToShortestString_keepPeriod ) ) if( Str_e[n1] == '.' ) n1--;
97 n1++;
98 Str_e[n1] = 0;
99
100 e1++;
101 exponent = (int) strtol( e1, &e2, 10 );
102 if( exponent != 0 ) { /* If 0, the exponent was "e+00". */
103 for( e1 = Str_e; *e1 != 0; e1++ ) ;
104 snprintf( e1, sizeof Str_e, "e%d", exponent );
105
106 digitsRightOfPeriod_f = significantDigits - exponent;
107 if( ( digitsRightOfPeriod_f > 25 ) || ( exponent > 50 ) ) return( strdup( Str_r ) );
108 if( digitsRightOfPeriod_f < 0 ) digitsRightOfPeriod_f = 0;
109
110 snprintf( Fmt, sizeof Fmt, "%%%s.%df", sign, digitsRightOfPeriod_f );
111 snprintf( Str_f, sizeof Str_f, Fmt, value );
112
113 ne = (int) strlen( Str_e );
114 nf = (int) strlen( Str_f );
115 if( strchr( Str_f, '.' ) != NULL ) { /* '.' in string. */
116 if( flags & nf_floatToShortestString_trimZeros ) while( Str_f[nf-1] == '0' ) nf--; // Loop checking, 11.06.2015, T. Koi
117 if( Str_f[nf-1] == '.' ) {
118 if( !( flags & nf_floatToShortestString_keepPeriod ) ) nf--;
119 } }
120 else { /* Maybe we want a '.' else it looks like an integer, "12345." vs "12345". */
122 Str_f[nf] = '.';
123 nf++;
124 }
125 }
126 Str_f[nf] = 0;
127
128 if( ( nf + favorEFormBy ) < ne ) Str_r = Str_f;
129 }
130 return( strdup( Str_r ) );
131}
G4int sign(const T t)
#define nf_floatToShortestString_includeSign
#define nf_floatToShortestString_trimZeros
#define nf_floatToShortestString_keepPeriod

◆ nfu_stringToListOfDoubles()

nfu_status nfu_stringToListOfDoubles ( char const * str,
int64_t * numberConverted,
double ** doublePtr,
char ** endCharacter )

Definition at line 23 of file nf_stringToDoubles.cc.

23 {
24
25 *numberConverted = 0;
26 *doublePtr = NULL;
27 return( nfu_stringToListOfDoubles2( str, numberConverted, doublePtr, endCharacter ) );
28}

Referenced by ptwX_fromString(), and ptwXY_fromString().