Geant4 11.2.2
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>

Go to the source code of this file.

Macros

#define NUMERICALFUNCTIONS_SVN_VERSION   110+
 
#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_mallocError , nfu_insufficientMemory , nfu_badIndex ,
  nfu_XNotAscending , nfu_badIndexForX , nfu_XOutsideDomain , nfu_invalidInterpolation ,
  nfu_badSelf , nfu_divByZero , nfu_unsupportedInterpolationConversion , nfu_unsupportedInterpolation ,
  nfu_empty , nfu_tooFewPoints , nfu_domainsNotMutual , nfu_badInput ,
  nfu_badNorm , nfu_badIntegrationInput , nfu_otherInterpolation , nfu_failedToConverge ,
  nfu_oddNumberOfValues
}
 

Functions

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_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)
 
void nfu_printMsg (char *fmt,...)
 
void nfu_printErrorMsg (char *fmt,...)
 
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

◆ nf_floatToShortestString_includeSign

#define nf_floatToShortestString_includeSign   ( 1 << 2 )

Definition at line 18 of file nf_utilities.h.

Referenced by nf_floatToShortestString().

◆ nf_floatToShortestString_keepPeriod

#define nf_floatToShortestString_keepPeriod   ( 1 << 1 )

Definition at line 17 of file nf_utilities.h.

Referenced by nf_floatToShortestString().

◆ nf_floatToShortestString_trimZeros

#define nf_floatToShortestString_trimZeros   ( 1 << 0 )

Definition at line 16 of file nf_utilities.h.

Referenced by nf_floatToShortestString().

◆ NUMERICALFUNCTIONS_SVN_VERSION

#define NUMERICALFUNCTIONS_SVN_VERSION   110+

Definition at line 14 of file nf_utilities.h.

Typedef Documentation

◆ nfu_status

typedef enum nfu_status_e nfu_status

Enumeration Type Documentation

◆ nfu_status_e

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

Definition at line 25 of file nf_utilities.h.

@ nfu_unsupportedInterpolation
@ nfu_domainsNotMutual
@ nfu_XNotAscending
@ nfu_invalidInterpolation
@ nfu_Okay
@ nfu_badSelf
@ nfu_oddNumberOfValues
@ nfu_mallocError
@ nfu_insufficientMemory
@ nfu_badNorm
@ nfu_badIntegrationInput
@ nfu_XOutsideDomain
@ nfu_badIndex
@ nfu_failedToConverge
@ nfu_tooFewPoints
@ nfu_unsupportedInterpolationConversion
@ nfu_badInput
@ nfu_badIndexForX
@ 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 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_calloc()

void * nfu_calloc ( size_t size,
size_t n )

Definition at line 123 of file nf_utilities.cc.

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

Referenced by ptwX_new(), ptwXY_new(), and ptwXY_thin().

◆ nfu_free()

void * nfu_free ( void * p)

Definition at line 143 of file nf_utilities.cc.

143 {
144
145 if( p != NULL ) {
146 if( nfu_debugging ) printf( "nfu_free %12p\n", p );
147 free( p );
148 }
149 return( NULL );
150}

Referenced by nf_Legendre_free(), nf_Legendre_new(), nf_Legendre_release(), ptwX_free(), ptwX_fromString(), ptwX_new(), ptwX_release(), ptwXY_cloneToInterpolation(), ptwXY_copy(), ptwXY_free(), ptwXY_fromString(), ptwXY_mergeFromXYs(), ptwXY_new(), ptwXY_release(), and ptwXY_thin().

◆ nfu_getInfinity()

double nfu_getInfinity ( double sign)

Definition at line 68 of file nf_utilities.cc.

68 {
69
70 if( sign < 0 ) return( -INFINITY );
71 return( INFINITY );
72}

◆ nfu_getNAN()

double nfu_getNAN ( void )

Definition at line 54 of file nf_utilities.cc.

54 {
55
56 return( NAN );
57}

Referenced by ptwXY_div_ptwXY(), ptwXY_getXMaxAndFrom(), and ptwXY_getXMinAndFrom().

◆ nfu_isNAN()

int nfu_isNAN ( double d)

Definition at line 61 of file nf_utilities.cc.

61 {
62
63 return( is_nan( d ) );
64}
#define is_nan(a)

Referenced by ptwXY_div_ptwXY().

◆ nfu_malloc()

void * nfu_malloc ( size_t size)

Definition at line 113 of file nf_utilities.cc.

113 {
114
115 void *p = malloc( size );
116
117 if( nfu_debugging ) printf( "nfu_malloc %12p size = %8llu\n", p, (long long unsigned) size );
118 return( p );
119}

Referenced by nf_Legendre_new(), and ptwXY_mergeFromXYs().

◆ nfu_printErrorMsg()

void nfu_printErrorMsg ( char * fmt,
... )

Definition at line 166 of file nf_utilities.cc.

166 {
167
168 va_list args;
169
170 va_start( args, fmt );
171 vfprintf( stderr, fmt, args );
172 fprintf( stderr, "\n" );
173 va_end( args );
174
175 exit( EXIT_FAILURE );
176}

◆ nfu_printMsg()

void nfu_printMsg ( char * fmt,
... )

Definition at line 154 of file nf_utilities.cc.

154 {
155
156 va_list args;
157
158 va_start( args, fmt );
159 vfprintf( stderr, fmt, args );
160 fprintf( stderr, "\n" );
161 va_end( args );
162}

◆ nfu_realloc()

void * nfu_realloc ( size_t size,
void * old )

Definition at line 133 of file nf_utilities.cc.

133 {
134
135 void *p = realloc( old, size );
136
137 if( nfu_debugging ) printf( "nfu_realloc %12p size = %8llu", p, (long long unsigned) size );
138 return( p );
139}

Referenced by nf_Legendre_reallocateCls(), ptwX_reallocatePoints(), ptwXY_reallocateOverflowPoints(), and ptwXY_reallocatePoints().

◆ nfu_setMemoryDebugMode()

void nfu_setMemoryDebugMode ( int mode)

Definition at line 106 of file nf_utilities.cc.

106 {
107
108 nfu_debugging = mode;
109}

◆ nfu_statusMessage()

const char * nfu_statusMessage ( nfu_status status)

Definition at line 76 of file nf_utilities.cc.

76 {
77
78 switch( status ) {
79 case nfu_Okay : return( Okay_message );
80 case nfu_mallocError : return( mallocError_message );
81 case nfu_insufficientMemory : return( insufficientMemory_message );
82 case nfu_badIndex : return( badIndex_message );
83 case nfu_XNotAscending : return( XNotAscending_message );
84 case nfu_badIndexForX : return( badIndexForX_message );
85 case nfu_XOutsideDomain : return( XOutsideDomain_message );
86 case nfu_invalidInterpolation : return( invalidInterpolation_message );
87 case nfu_badSelf : return( badSelf_message );
88 case nfu_divByZero : return( divByZero_message );
89 case nfu_unsupportedInterpolation : return( unsupportedInterpolation_message );
90 case nfu_unsupportedInterpolationConversion : return( unsupportedInterpolationConversion_message );
91 case nfu_empty : return( empty_message );
92 case nfu_tooFewPoints : return( tooFewPoints_message );
93 case nfu_domainsNotMutual : return( notMutualDomian_message );
94 case nfu_badInput : return( badInput_message );
95 case nfu_badNorm : return( badNorm_message );
96 case nfu_badIntegrationInput : return( badIntegrationInput_message );
97 case nfu_otherInterpolation : return( otherInterpolation_message );
98 case nfu_failedToConverge : return( failedToConverge_message );
99 case nfu_oddNumberOfValues : return( oddNumberOfValues_message );
100 }
101 return( unknownStatus_message );
102}

Referenced by MCGIDI_angular_parseFromTOM(), MCGIDI_fromTOM_pdfOfX(), MCGIDI_product_parseFromTOM(), MCGIDI_target_heated_read(), and MCGIDI_target_heated_recast().

◆ 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().