Geant4 10.7.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>

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.

◆ nf_floatToShortestString_keepPeriod

#define nf_floatToShortestString_keepPeriod   ( 1 << 1 )

Definition at line 17 of file nf_utilities.h.

◆ nf_floatToShortestString_trimZeros

#define nf_floatToShortestString_trimZeros   ( 1 << 0 )

Definition at line 16 of file nf_utilities.h.

◆ 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
Definition: nf_utilities.h:28
@ nfu_domainsNotMutual
Definition: nf_utilities.h:28
@ nfu_XNotAscending
Definition: nf_utilities.h:26
@ nfu_invalidInterpolation
Definition: nf_utilities.h:27
@ nfu_Okay
Definition: nf_utilities.h:25
@ nfu_badSelf
Definition: nf_utilities.h:27
@ nfu_oddNumberOfValues
Definition: nf_utilities.h:30
@ nfu_mallocError
Definition: nf_utilities.h:25
@ nfu_insufficientMemory
Definition: nf_utilities.h:25
@ nfu_badNorm
Definition: nf_utilities.h:29
@ nfu_badIntegrationInput
Definition: nf_utilities.h:29
@ nfu_XOutsideDomain
Definition: nf_utilities.h:26
@ nfu_badIndex
Definition: nf_utilities.h:26
@ nfu_failedToConverge
Definition: nf_utilities.h:30
@ nfu_tooFewPoints
Definition: nf_utilities.h:28
@ nfu_unsupportedInterpolationConversion
Definition: nf_utilities.h:27
@ nfu_badInput
Definition: nf_utilities.h:29
@ nfu_badIndexForX
Definition: nf_utilities.h:26
@ nfu_empty
Definition: nf_utilities.h:28
@ nfu_divByZero
Definition: nf_utilities.h:27
@ nfu_otherInterpolation
Definition: nf_utilities.h:29
enum nfu_status_e nfu_status

Function Documentation

◆ nf_floatToShortestString()

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

Definition at line 66 of file nf_stringToDoubles.cc.

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

◆ 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)
Definition: nf_utilities.cc:18

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, old = %12p\n", p, (long long unsigned) size, old );
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 29 of file nf_stringToDoubles.cc.

29 {
30
31 *numberConverted = 0;
32 *doublePtr = NULL;
33 return( nfu_stringToListOfDoubles2( str, numberConverted, doublePtr, endCharacter ) );
34}

Referenced by ptwX_fromString(), and ptwXY_fromString().