Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
nf_utilities.h File Reference
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdarg.h>
#include <math.h>
#include <statusMessageReporting.h>

Go to the source code of this file.

Macros

#define _USE_MATH_DEFINES
#define nf_floatToShortestString_trimZeros   ( 1 << 0 )
#define nf_floatToShortestString_keepPeriod   ( 1 << 1 )
#define nf_floatToShortestString_includeSign   ( 1 << 2 )

Typedefs

typedef enum nfu_status_e nfu_status

Enumerations

enum  nfu_status_e {
  nfu_Okay , nfu_Error , nfu_badSelf , nfu_mallocError ,
  nfu_insufficientMemory , nfu_badIndex , nfu_XNotAscending , nfu_badIndexForX ,
  nfu_XOutsideDomain , nfu_invalidInterpolation , nfu_divByZero , nfu_unsupportedInterpolationConversion ,
  nfu_unsupportedInterpolation , nfu_empty , nfu_tooFewPoints , nfu_domainsNotMutual ,
  nfu_badInput , nfu_badNorm , nfu_badIntegrationInput , nfu_otherInterpolation ,
  nfu_flatInterpolation , nfu_failedToConverge , nfu_oddNumberOfValues , nfu_badLogValue
}

Functions

int nfu_setup (void)
double nfu_getNAN (void)
int nfu_isNAN (double d)
double nfu_getInfinity (double sign)
const char * nfu_statusMessage (nfu_status status)
void nfu_setMemoryDebugMode (int mode)
void nfu_printMsg (char const *fmt,...)
void nfu_printErrorMsg (char const *fmt,...)
void * nfu_malloc (size_t size)
void * nfu_calloc (size_t size, size_t n)
void * nfu_realloc (size_t size, void *old)
void * nfu_free (void *p)
int32_t * nfu_stringToListOfInt32s (statusMessageReporting *smr, char const *str, char sep, int64_t *numberConverted, char **endCharacter)
int nfu_stringToInt32 (statusMessageReporting *smr, char const *str, char **endCharacter, int32_t *value)
double * nfu_stringToListOfDoubles (statusMessageReporting *smr, char const *str, char sep, int64_t *numberConverted, char **endCharacter, int useSystem_strtod)
double nf_strtod (char const *ptr, char **endCharacter)
char * nf_floatToShortestString (double value, int significantDigits, int favorEFormBy, int flags)

Variables

int nfu_SMR_libraryID

Macro Definition Documentation

◆ _USE_MATH_DEFINES

#define _USE_MATH_DEFINES

Definition at line 13 of file nf_utilities.h.

◆ nf_floatToShortestString_includeSign

#define nf_floatToShortestString_includeSign   ( 1 << 2 )

Definition at line 32 of file nf_utilities.h.

Referenced by nf_floatToShortestString().

◆ nf_floatToShortestString_keepPeriod

#define nf_floatToShortestString_keepPeriod   ( 1 << 1 )

Definition at line 31 of file nf_utilities.h.

Referenced by nf_floatToShortestString().

◆ nf_floatToShortestString_trimZeros

#define nf_floatToShortestString_trimZeros   ( 1 << 0 )

Definition at line 30 of file nf_utilities.h.

Referenced by LUPI::Misc::doubleToShortestString(), and nf_floatToShortestString().

Typedef Documentation

◆ nfu_status

typedef enum nfu_status_e nfu_status

Enumeration Type Documentation

◆ nfu_status_e

Enumerator
nfu_Okay 
nfu_Error 
nfu_badSelf 
nfu_mallocError 
nfu_insufficientMemory 
nfu_badIndex 
nfu_XNotAscending 
nfu_badIndexForX 
nfu_XOutsideDomain 
nfu_invalidInterpolation 
nfu_divByZero 
nfu_unsupportedInterpolationConversion 
nfu_unsupportedInterpolation 
nfu_empty 
nfu_tooFewPoints 
nfu_domainsNotMutual 
nfu_badInput 
nfu_badNorm 
nfu_badIntegrationInput 
nfu_otherInterpolation 
nfu_flatInterpolation 
nfu_failedToConverge 
nfu_oddNumberOfValues 
nfu_badLogValue 

Definition at line 38 of file nf_utilities.h.

38 {
39 nfu_Okay,
40 nfu_Error,
52 nfu_empty,
@ nfu_unsupportedInterpolation
@ nfu_domainsNotMutual
@ nfu_XNotAscending
@ nfu_invalidInterpolation
@ nfu_Okay
@ nfu_badSelf
@ nfu_badLogValue
@ nfu_oddNumberOfValues
@ nfu_mallocError
@ nfu_insufficientMemory
@ nfu_flatInterpolation
@ nfu_badNorm
@ nfu_badIntegrationInput
@ nfu_XOutsideDomain
@ nfu_badIndex
@ nfu_failedToConverge
@ nfu_tooFewPoints
@ nfu_unsupportedInterpolationConversion
@ nfu_badInput
@ nfu_badIndexForX
@ nfu_Error
@ nfu_empty
@ nfu_divByZero
@ nfu_otherInterpolation
enum nfu_status_e nfu_status

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 * ptr,
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_calloc()

void * nfu_calloc ( size_t size,
size_t n )

Definition at line 133 of file nf_utilities.c.

133 {
134
135 void *p = calloc( size, n );
136
137 if( nfu_debugging ) printf( "nfu_calloc %12p size = %8llu, n = %8llu\n", p, (long long unsigned) size, (long long unsigned) n );
138 return( p );
139}
voidp calloc(uInt items, uInt size)

◆ nfu_free()

void * nfu_free ( void * p)

Definition at line 153 of file nf_utilities.c.

153 {
154
155 if( p != NULL ) {
156 if( nfu_debugging ) printf( "nfu_free %12p\n", p );
157 free( p );
158 }
159 return( NULL );
160}
void free(voidpf ptr)

Referenced by nf_Legendre_free(), nf_Legendre_new(), and nf_Legendre_release().

◆ nfu_getInfinity()

double nfu_getInfinity ( double sign)

Definition at line 74 of file nf_utilities.c.

74 {
75
76 if( sign < 0 ) return( -INFINITY );
77 return( INFINITY );
78}

◆ nfu_getNAN()

double nfu_getNAN ( void )

Definition at line 60 of file nf_utilities.c.

60 {
61
62 return( NAN );
63}

Referenced by ptwXY_div_ptwXY().

◆ nfu_isNAN()

int nfu_isNAN ( double d)

Definition at line 67 of file nf_utilities.c.

67 {
68
69 return( isnan( d ) );
70}

Referenced by ptwXY_div_ptwXY().

◆ nfu_malloc()

void * nfu_malloc ( size_t size)

Definition at line 123 of file nf_utilities.c.

123 {
124
125 void *p = malloc( size );
126
127 if( nfu_debugging ) printf( "nfu_malloc %12p size = %8llu\n", p, (long long unsigned) size );
128 return( p );
129}
voidp malloc(uInt size)

◆ nfu_printErrorMsg()

void nfu_printErrorMsg ( char const * fmt,
... )

Definition at line 176 of file nf_utilities.c.

176 {
177
178 va_list args;
179
180 va_start( args, fmt );
181 vfprintf( stderr, fmt, args );
182 fprintf( stderr, "\n" );
183 va_end( args );
184
185 exit( EXIT_FAILURE );
186}

◆ nfu_printMsg()

void nfu_printMsg ( char const * fmt,
... )

Definition at line 164 of file nf_utilities.c.

164 {
165
166 va_list args;
167
168 va_start( args, fmt );
169 vfprintf( stderr, fmt, args );
170 fprintf( stderr, "\n" );
171 va_end( args );
172}

◆ nfu_realloc()

void * nfu_realloc ( size_t size,
void * old )

Definition at line 143 of file nf_utilities.c.

143 {
144
145 void *p = realloc( old, size );
146
147 if( nfu_debugging ) printf( "nfu_realloc %12p size = %8llu, old = %12p\n", p, (long long unsigned) size, old );
148 return( p );
149}

Referenced by nf_Legendre_reallocateCls().

◆ nfu_setMemoryDebugMode()

void nfu_setMemoryDebugMode ( int mode)

Definition at line 116 of file nf_utilities.c.

116 {
117
118 nfu_debugging = mode;
119}

◆ nfu_setup()

int nfu_setup ( void )

Definition at line 51 of file nf_utilities.c.

51 {
52
53 nfu_SMR_libraryID = smr_registerLibrary( "numericalFunctions" );
54
55 return( 0 );
56}
int nfu_SMR_libraryID
int smr_registerLibrary(char const *libraryName)

◆ nfu_statusMessage()

const char * nfu_statusMessage ( nfu_status status)

Definition at line 82 of file nf_utilities.c.

82 {
83
84printf( "status = %d\n", status );
85 switch( status ) {
86 case nfu_Okay : return( Okay_message );
87 case nfu_Error : return( Error_message );
88 case nfu_mallocError : return( mallocError_message );
89 case nfu_insufficientMemory : return( insufficientMemory_message );
90 case nfu_badIndex : return( badIndex_message );
91 case nfu_XNotAscending : return( XNotAscending_message );
92 case nfu_badIndexForX : return( badIndexForX_message );
93 case nfu_XOutsideDomain : return( XOutsideDomain_message );
94 case nfu_invalidInterpolation : return( invalidInterpolation_message );
95 case nfu_badSelf : return( badSelf_message );
96 case nfu_divByZero : return( divByZero_message );
97 case nfu_unsupportedInterpolation : return( unsupportedInterpolation_message );
98 case nfu_unsupportedInterpolationConversion : return( unsupportedInterpolationConversion_message );
99 case nfu_empty : return( empty_message );
100 case nfu_tooFewPoints : return( tooFewPoints_message );
101 case nfu_domainsNotMutual : return( notMutualDomian_message );
102 case nfu_badInput : return( badInput_message );
103 case nfu_badNorm : return( badNorm_message );
104 case nfu_badIntegrationInput : return( badIntegrationInput_message );
105 case nfu_otherInterpolation : return( otherInterpolation_message );
106 case nfu_flatInterpolation : return( flatInterpolation_message );
107 case nfu_failedToConverge : return( failedToConverge_message );
108 case nfu_oddNumberOfValues : return( oddNumberOfValues_message );
109 case nfu_badLogValue : return( badLogValue_message );
110 }
111 return( unknownStatus_message );
112}

◆ nfu_stringToInt32()

int nfu_stringToInt32 ( statusMessageReporting * smr,
char const * str,
char ** endCharacter,
int32_t * value )

Definition at line 126 of file nf_stringToInt32s.c.

126 {
127
128 long lValue = strtol( str, endCharacter, 10 );
129
130 if( lValue < INT32_MIN ) {
131 smr_setReportError2( smr, nfu_SMR_libraryID, nfu_badInput, "int32_t underflow: %l", lValue );
132 return( -1 ); }
133 else if( lValue > INT32_MAX ) {
134 smr_setReportError2( smr, nfu_SMR_libraryID, nfu_badInput, "int32_t overflow: %l", lValue );
135 return( 1 );
136 }
137 *value = (int) lValue;
138 return( 0 );
139}
#define INT32_MAX
#define INT32_MIN
#define smr_setReportError2(smr, libraryID, code, fmt,...)

◆ 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}

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

◆ nfu_stringToListOfInt32s()

int32_t * nfu_stringToListOfInt32s ( statusMessageReporting * smr,
char const * str,
char sep,
int64_t * numberConverted,
char ** endCharacter )

Definition at line 27 of file nf_stringToInt32s.c.

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

Variable Documentation

◆ nfu_SMR_libraryID

int nfu_SMR_libraryID
extern

Definition at line 46 of file nf_utilities.c.

Referenced by nf_Legendre_allocated(), nf_Legendre_clone(), nf_Legendre_evauluateAtMu(), nf_Legendre_from_ptwXY(), nf_Legendre_getCl(), nf_Legendre_initialize(), nf_Legendre_maxOrder(), nf_Legendre_normalize(), nf_Legendre_reallocateCls(), nf_Legendre_setCl(), nf_Legendre_to_ptwXY(), nfu_setup(), nfu_stringToInt32(), nfu_stringToListOfDoubles(), nfu_stringToListOfInt32s(), ptwX_abs(), ptwX_add_double(), ptwX_add_ptwX(), ptwX_ascendingOrder(), ptwX_clone(), ptwX_close(), ptwX_closesDifferenceInRange(), ptwX_compare(), ptwX_copy(), ptwX_countOccurrences(), ptwX_create(), ptwX_createLine(), ptwX_deletePoints(), ptwX_fromString(), ptwX_getPointAtIndex(), ptwX_initialize(), ptwX_insertPointsAtIndex(), ptwX_length(), ptwX_mul_double(), ptwX_neg(), ptwX_new(), ptwX_range(), ptwX_reallocatePoints(), ptwX_reverse(), ptwX_setData(), ptwX_setPointAtIndex(), ptwX_slice(), ptwX_slopeOffset(), ptwX_sort(), ptwX_sub_ptwX(), ptwX_unique(), ptwXY_abs(), ptwXY_add_double(), ptwXY_add_ptwXY(), ptwXY_appendXY(), ptwXY_applyFunction(), ptwXY_areDomainsMutual(), ptwXY_binary_ptwXY(), ptwXY_clear(), ptwXY_clip(), ptwXY_clone(), ptwXY_clone2(), ptwXY_cloneToInterpolation(), ptwXY_coalescePoints(), ptwXY_convolution(), ptwXY_copy(), ptwXY_copyPointsOnly(), ptwXY_copyToC_XY(), ptwXY_create(), ptwXY_createFrom_Xs_Ys(), ptwXY_createFromFunction(), ptwXY_createFromFunction2(), ptwXY_createGaussian(), ptwXY_createGaussianCenteredSigma1(), ptwXY_deletePoints(), ptwXY_div_doubleFrom(), ptwXY_div_fromDouble(), ptwXY_div_ptwXY(), ptwXY_domainMax(), ptwXY_domainMaxAndFrom(), ptwXY_domainMaxSlice(), ptwXY_domainMin(), ptwXY_domainMinAndFrom(), ptwXY_domainMinSlice(), ptwXY_domainSlice(), ptwXY_dullEdges(), ptwXY_equalProbableBins(), ptwXY_exp(), ptwXY_f_integrate(), ptwXY_flatInterpolationToLinear(), ptwXY_fromString(), ptwXY_fromUnitbase(), ptwXY_getLowerIndexBoundingX(), ptwXY_getNonOverflowLength(), ptwXY_getPointAtIndex(), ptwXY_getPointsAroundX(), ptwXY_getPointsAroundX_closeIsEqual(), ptwXY_getSlopeAtX(), ptwXY_getValueAtX(), ptwXY_getXArray(), ptwXY_getXYPairAtIndex(), ptwXY_groupOneFunction(), ptwXY_groupThreeFunctions(), ptwXY_groupTwoFunctions(), ptwXY_initialize(), ptwXY_integrate(), ptwXY_integrateDomain(), ptwXY_integrateDomainWithWeight_sqrt_x(), ptwXY_integrateDomainWithWeight_x(), ptwXY_integrateWithFunction(), ptwXY_integrateWithWeight_sqrt_x(), ptwXY_integrateWithWeight_x(), ptwXY_interpolatePoint(), ptwXY_intersectionWith_ptwX(), ptwXY_inverse(), ptwXY_length(), ptwXY_mapToXsAndAdd(), ptwXY_mergeClosePoints(), ptwXY_mergeFromXsAndYs(), ptwXY_mergeFromXYs(), ptwXY_mod(), ptwXY_mul2_ptwXY(), ptwXY_mul_double(), ptwXY_mul_ptwXY(), ptwXY_mutualifyDomains(), ptwXY_neg(), ptwXY_new(), ptwXY_normalize(), ptwXY_pow(), ptwXY_range(), ptwXY_rangeMax(), ptwXY_rangeMin(), ptwXY_reallocateOverflowPoints(), ptwXY_reallocatePoints(), ptwXY_runningIntegral(), ptwXY_scaleAndOffsetDomainWith_ptwXYs(), ptwXY_scaleOffsetXAndY(), ptwXY_setValueAtX(), ptwXY_setValueAtX_overrideIfClose(), ptwXY_setXYData(), ptwXY_setXYDataFromXsAndYs(), ptwXY_setXYPairAtIndex(), ptwXY_simpleCoalescePoints(), ptwXY_slice(), ptwXY_slopeOffset(), ptwXY_startIndex(), ptwXY_sub_doubleFrom(), ptwXY_sub_fromDouble(), ptwXY_sub_ptwXY(), ptwXY_thicken(), ptwXY_thin(), ptwXY_thinDomain(), ptwXY_toOtherInterpolation(), ptwXY_toUnitbase(), ptwXY_trim(), ptwXY_tweakDomainsToMutualify(), ptwXY_union(), ptwXY_unitbaseInterpolate(), ptwXY_valuesToC_XsAndYs(), ptwXY_valueTo_ptwXY(), and ptwXY_ysMappedToXs().