15 #ifndef INCLUDED_numeric_numeric_functions_hh
16 #define INCLUDED_numeric_numeric_functions_hh
22 #include <platform/types.hh>
36 #if (defined min) && (defined WIN32) // Workaround for MSVC and windows.h include which used #define min
40 #if (defined max) && (defined WIN32) // Workaround for MSVC and windows.h include which used #define max
47 min(
short int const a,
short int const b )
49 return ( a < b ? a : b );
58 return ( a < b ? a : b );
65 min(
long int const a,
long int const b )
67 return ( a < b ? a : b );
74 min(
unsigned short int const a,
unsigned short int const b )
76 return ( a < b ? a : b );
83 min(
unsigned int const a,
unsigned int const b )
85 return ( a < b ? a : b );
92 min(
unsigned long int const a,
unsigned long int const b )
94 return ( a < b ? a : b );
103 return ( a < b ? a : b );
110 min(
double const a,
double const b )
112 return ( a < b ? a : b );
119 min(
long double const a,
long double const b )
121 return ( a < b ? a : b );
130 template<
typename T >
135 return ( a < b ? ( a < c ? a : c ) : ( b < c ? b : c ) );
140 template<
typename T >
143 min(
T const &
a,
T const &
b,
T const & c,
T const & d )
150 template<
typename T >
153 min(
T const &
a,
T const &
b,
T const & c,
T const & d,
T const & e )
160 template<
typename T >
163 min(
T const &
a,
T const &
b,
T const & c,
T const & d,
T const & e,
T const &
f )
175 max(
short int const a,
short int const b )
177 return ( a < b ? b : a );
186 return ( a < b ? b : a );
193 max(
long int const a,
long int const b )
195 return ( a < b ? b : a );
202 max(
unsigned short int const a,
unsigned short int const b )
204 return ( a < b ? b : a );
211 max(
unsigned int const a,
unsigned int const b )
213 return ( a < b ? b : a );
220 max(
unsigned long int const a,
unsigned long int const b )
222 return ( a < b ? b : a );
231 return ( a < b ? b : a );
238 max(
double const a,
double const b )
240 return ( a < b ? b : a );
247 max(
long double const a,
long double const b )
249 return ( a < b ? b : a );
258 template<
typename T >
263 return ( a < b ? ( b < c ? c : b ) : ( a < c ? c : a ) );
268 template<
typename T >
271 max(
T const &
a,
T const &
b,
T const & c,
T const & d )
278 template<
typename T >
281 max(
T const &
a,
T const &
b,
T const & c,
T const & d,
T const & e )
288 template<
typename T >
291 max(
T const &
a,
T const &
b,
T const & c,
T const & d,
T const & e,
T const &
f )
301 template<
typename T >
311 template<
typename T >
321 template<
typename T >
326 return ( x >=
T( 0 ) ? +1 : -1 );
331 template<
typename S,
typename T >
341 template<
typename T >
346 return max( a, b ) -
min( a, b );
351 template<
typename R,
typename T,
bool >
365 template<
typename R,
typename T >
373 return R( x + (
sign( x ) *
T( 0.5 ) ) );
379 template<
typename R,
typename T >
389 template<
typename T >
394 return std::size_t( x >
T( 0 ) ? x + (
sign( x ) *
T( 0.5 ) ) : 0 );
399 template<
typename T >
409 template<
typename T >
414 return static_cast< int >( x + (
sign( x ) *
T( 0.5 ) ) );
419 template<
typename T >
424 return static_cast< int >( x + (
sign( x ) *
T( 0.5 ) ) );
429 template<
typename T,
bool >
437 return ( y !=
T( 0 ) ? x - (
T( static_cast< SSize >( x / y ) ) * y ) :
T( 0 ) );
445 template<
typename T >
453 return ( y !=
T( 0 ) ? x - ( ( x / y ) * y ) :
T( 0 ) );
461 template<
typename T >
471 template<
typename T,
bool >
479 return ( y !=
T( 0 ) ? x - ( std::floor( x / y ) * y ) :
T( 0 ) );
485 template<
typename T >
493 return ( y !=
T( 0 ) ? x - (
T( std::floor( static_cast< long double >( x ) / y ) ) * y ) :
T( 0 ) );
499 template<
typename T >
509 template<
typename T,
bool >
520 T const x_over_y( x / y );
523 T const n_minus_x_over_y(
T( n ) - x_over_y );
524 if ( n_minus_x_over_y ==
T( 0.5 ) ) {
526 }
else if ( n_minus_x_over_y ==
T( -0.5 ) ) {
530 return ( x - ( n * y ) );
537 template<
typename T >
548 long double const x_over_y( static_cast< long double >( x ) / y );
551 long double const n_minus_x_over_y( static_cast< long double >( n ) - x_over_y );
552 if ( n_minus_x_over_y == 0.5l ) {
554 }
else if ( n_minus_x_over_y == -0.5l ) {
558 return ( x - ( n * y ) );
569 template<
typename T >
579 template<
typename T,
bool >
593 template<
typename T >
601 return ( y !=
T( 0 ) ? x - (
nearest_ssize( static_cast< long double >( x ) / y ) * y ) :
T( 0 ) );
610 template<
typename T >
620 template<
typename T,
typename S >
631 template<
typename T >
638 while ( lo >
T( 0 ) ) {
639 T const rem =
mod( hi, lo );
648 template<
typename T >
654 assert( r_tol >=
T( 0 ) );
655 assert( a_tol >=
T( 0 ) );
661 template<
typename T >
667 assert( r_tol >=
T( 0 ) );
668 assert( a_tol >=
T( 0 ) );
674 template<
typename T >
680 assert( r_tol >=
T( 0 ) );
681 assert( a_tol >=
T( 0 ) );
687 template<
typename T >
693 assert( r_tol >=
T( 0 ) );
694 assert( a_tol >=
T( 0 ) );
700 template<
typename T >
706 assert( r_tol >=
T( 0 ) );
707 assert( a_tol >=
T( 0 ) );
717 if ( (a*s) != (s*cos(b)) )
return false;
718 if ( s * 100.0 == s * 1000.00 )
return false;
726 #endif // INCLUDED_numeric_numeric_functions_HH
int nearest_int(T const &x)
nearest_int( x ): Nearest int
Modulo function selector class for non-integer types.
bool eq_tol(T const &x, T const &y, T const &r_tol, T const &a_tol)
Equal within specified relative and absolute tolerances?
bool lt_tol(T const &x, T const &y, T const &r_tol, T const &a_tol)
Less than within specified relative and absolute tolerances?
T remainder(T const &x, T const &y)
Remainder of x with respect to division by y that is of smallest magnitude.
std::size_t nearest_size(T const &x)
nearest_size( x ): Nearest std::size_t
T mod(T const &x, T const &y)
x(mod y) computational modulo returning magnitude < | y | and sign of x
static T fast_remainder(T const &x, T const &y)
static T remainder(T const &x, T const &y)
T fast_remainder(T const &x, T const &y)
Remainder of x with respect to division by y that is of smallest magnitude.
T modulo(T const &x, T const &y)
x(mod y) mathematical modulo returning magnitude < | y | and sign of y
T gcd(T const &m, T const &n)
Greatest common divisor.
short int min(short int const a, short int const b)
min( short int, short int )
T abs_difference(T const &a, T const &b)
Absolute difference.
short int max(short int const a, short int const b)
max( short int, short int )
T cube(T const &x)
cube( x ) == x^3
static R nearest(T const &x)
R nearest(T const &x)
nearest< R >( x ): Nearest R
static T mod(T const &x, T const &y)
T square(T const &x)
square( x ) == x^2
T abs(T const &x)
std::abs( x ) == | x |
Mod function selector class for non-integer types.
T remainder_conversion(T const &t, S &s)
Remainder and result of conversion to a different type.
bool is_a_finitenumber(double s, double a, double b)
static T remainder(T const &x, T const &y)
bool ge_tol(T const &x, T const &y, T const &r_tol, T const &a_tol)
Greater than or equal within specified relative and absolute tolerances?
rosetta project type declarations. Should be kept updated with core/types.hh. This exists because num...
SSize nearest_ssize(T const &x)
nearest_ssize( x ): Nearest SSize
Nearest function selector class for R non-integer or T integer.
static R nearest(T const &x)
static T modulo(T const &x, T const &y)
bool gt_tol(T const &x, T const &y, T const &r_tol, T const &a_tol)
Greater than within specified relative and absolute tolerances?
static T fast_remainder(T const &x, T const &y)
static T mod(T const &x, T const &y)
Remainder function selector class for non-integer types.
int nint(T const &x)
nint( x ): Nearest int
T sign_transfered(S const &sigma, T const &x)
Sign transfered value.
bool le_tol(T const &x, T const &y, T const &r_tol, T const &a_tol)
Less than or equal within specified relative and absolute tolerances?
int sign(T const &x)
sign( x )
static T modulo(T const &x, T const &y)
Fast remainder function selector class for non-integer types.