diff options
author | jthorn <jthorn@f88db872-0e4f-0410-b76b-b9085cfa78c5> | 2003-01-18 16:18:55 +0000 |
---|---|---|
committer | jthorn <jthorn@f88db872-0e4f-0410-b76b-b9085cfa78c5> | 2003-01-18 16:18:55 +0000 |
commit | f595463096694fd5901bf2503d20d0e30269653d (patch) | |
tree | 1c83b18881c71da4b6186bbb9ef94c16a5e2c4fe /src/jtutil | |
parent | 1b6cae274a3362d6b2e4e9502318dfd4b6216141 (diff) |
Change various jtutil/ templates which used to be
template <typename fp>
to
template <typename fp_t>
(that is, rename the tempalte parameter from "fp" to "fp_t").
The reason for this change is that elsewhere in this thorn, I use
"fp" as a typedef for CCTK_REAL (= usually a typedef for double).
Using the same name for both a specific type and a template parameter
may be confusing to humans. It also may be confusing the Hitachi
compiler...
git-svn-id: http://svn.einsteintoolkit.org/cactus/EinsteinAnalysis/AHFinderDirect/trunk@925 f88db872-0e4f-0410-b76b-b9085cfa78c5
Diffstat (limited to 'src/jtutil')
-rw-r--r-- | src/jtutil/cpm_map.cc | 46 | ||||
-rw-r--r-- | src/jtutil/cpm_map.hh | 8 | ||||
-rw-r--r-- | src/jtutil/fuzzy.cc | 36 | ||||
-rw-r--r-- | src/jtutil/linear_map.cc | 108 | ||||
-rw-r--r-- | src/jtutil/linear_map.hh | 54 | ||||
-rw-r--r-- | src/jtutil/norm.cc | 24 | ||||
-rw-r--r-- | src/jtutil/round.cc | 19 | ||||
-rw-r--r-- | src/jtutil/test_fuzzy.cc | 117 | ||||
-rw-r--r-- | src/jtutil/test_linear_map.cc | 28 | ||||
-rw-r--r-- | src/jtutil/test_round.cc | 8 | ||||
-rw-r--r-- | src/jtutil/util.hh | 114 |
11 files changed, 285 insertions, 277 deletions
diff --git a/src/jtutil/cpm_map.cc b/src/jtutil/cpm_map.cc index 90a7dba..09ab35a 100644 --- a/src/jtutil/cpm_map.cc +++ b/src/jtutil/cpm_map.cc @@ -27,27 +27,27 @@ using jtutil::error_exit; // namespace jtutil { -template <typename fp> -cpm_map<fp>::cpm_map(int min_i_in, int max_i_in, - fp fixed_point) +template <typename fp_t> +cpm_map<fp_t>::cpm_map(int min_i_in, int max_i_in, + fp_t fixed_point) : min_i_(min_i_in), max_i_(max_i_in), map_is_plus_(false) { -const fp d_offset = 2.0 * fixed_point; -if (! fuzzy<fp>::is_integer(d_offset)) +const fp_t d_offset = 2.0 * fixed_point; +if (! fuzzy<fp_t>::is_integer(d_offset)) then error_exit(ERROR_EXIT, "***** cpm_map::cpm_map (mirror):\n" " fixed_point=%g isn't (fuzzily) integral or half-integral!\n" , double(fixed_point)); /*NOTREACHED*/ -offset_ = round<fp>::to_integer(d_offset); +offset_ = round<fp_t>::to_integer(d_offset); // verify that we have setup correct assert( - map_unchecked(fuzzy<fp>::floor (fixed_point)) + map_unchecked(fuzzy<fp_t>::floor (fixed_point)) == - fuzzy<fp>::ceiling(fixed_point) + fuzzy<fp_t>::ceiling(fixed_point) ); } } // namespace jtutil:: @@ -61,10 +61,10 @@ assert( // namespace jtutil { -template <typename fp> -cpm_map<fp>::cpm_map(int min_i_in, int max_i_in, - int sample_i, int sample_j, - bool map_is_plus_in) +template <typename fp_t> +cpm_map<fp_t>::cpm_map(int min_i_in, int max_i_in, + int sample_i, int sample_j, + bool map_is_plus_in) : min_i_(min_i_in), max_i_(max_i_in), offset_(map_is_plus_in ? sample_j - sample_i : sample_j + sample_i), @@ -87,16 +87,16 @@ assert( map_unchecked(sample_i) == sample_j ); // namespace jtutil { -template <typename fp> -cpm_map<fp>::cpm_map(int min_i_in, int max_i_in, - fp sample_i, fp sample_j, - bool map_is_plus_in) +template <typename fp_t> +cpm_map<fp_t>::cpm_map(int min_i_in, int max_i_in, + fp_t sample_i, fp_t sample_j, + bool map_is_plus_in) : min_i_(min_i_in), max_i_(max_i_in), map_is_plus_(map_is_plus_in) { -const fp fp_offset = map_is_plus_in ? sample_j - sample_i +const fp_t fp_offset = map_is_plus_in ? sample_j - sample_i : sample_j + sample_i; -if (! fuzzy<fp>::is_integer(fp_offset)) +if (! fuzzy<fp_t>::is_integer(fp_offset)) then error_exit(ERROR_EXIT, "***** cpm_map::cpm_map (generic via fp sample point):\n" " fp_offset=%g isn't fuzzily integral!\n" @@ -106,14 +106,14 @@ if (! fuzzy<fp>::is_integer(fp_offset)) double(fp_offset), double(sample_i), double(sample_j)); /*NOTREACHED*/ -offset_ = round<fp>::to_integer(fp_offset); +offset_ = round<fp_t>::to_integer(fp_offset); // verify that we have setup correct assert( - map_unchecked( fuzzy<fp>::floor(sample_i)) + map_unchecked( fuzzy<fp_t>::floor(sample_i)) == - (map_is_plus_in ? fuzzy<fp>::floor (sample_j) - : fuzzy<fp>::ceiling(sample_j)) ); + (map_is_plus_in ? fuzzy<fp_t>::floor (sample_j) + : fuzzy<fp_t>::ceiling(sample_j)) ); } } // namespace jtutil:: @@ -125,5 +125,5 @@ assert( // ***** template instantiations ***** // -// instantiation for <double> +template class jtutil::cpm_map<float>; template class jtutil::cpm_map<double>; diff --git a/src/jtutil/cpm_map.hh b/src/jtutil/cpm_map.hh index 210bfd9..4853820 100644 --- a/src/jtutil/cpm_map.hh +++ b/src/jtutil/cpm_map.hh @@ -26,7 +26,7 @@ namespace jtutil { -template <typename fp> +template <typename fp_t> class cpm_map { public: @@ -41,7 +41,7 @@ public: bool is_plus() const { return map_is_plus_; } bool is_minus() const { return !map_is_plus_; } int sign() const { return map_is_plus_ ? +1 : -1; } - fp fp_sign() const { return map_is_plus_ ? +1.0 : -1.0; } + fp_t fp_sign() const { return map_is_plus_ ? +1.0 : -1.0; } // the mapping itself int map_unchecked(int i) const @@ -88,7 +88,7 @@ public: // ... map specified by fixed point (must be integer or half-integer) // ... fixed point need not be in domain/range cpm_map(int min_i_in, int max_i_in, - fp fixed_point); + fp_t fixed_point); // "shift" map: i --> const + i // ... map specified by shift amount @@ -115,7 +115,7 @@ public: // must both be half-integral // ... sample point need *not* be in domain/range cpm_map(int min_i_in, int max_i_in, - fp sample_i, fp sample_j, + fp_t sample_i, fp_t sample_j, bool map_is_plus_in); // no need for explicit destructor, compiler-generated no-op is ok diff --git a/src/jtutil/fuzzy.cc b/src/jtutil/fuzzy.cc index 549012e..34cffa2 100644 --- a/src/jtutil/fuzzy.cc +++ b/src/jtutil/fuzzy.cc @@ -17,11 +17,11 @@ namespace jtutil { -template <typename fp> -bool fuzzy<fp>::EQ(fp x, fp y) +template <typename fp_t> +bool fuzzy<fp_t>::EQ(fp_t x, fp_t y) { -fp max_abs = jtutil::max(jtutil::abs(x), jtutil::abs(y)); -fp epsilon = jtutil::max(tolerance_, tolerance_*max_abs); +fp_t max_abs = jtutil::max(jtutil::abs(x), jtutil::abs(y)); +fp_t epsilon = jtutil::max(tolerance_, tolerance_*max_abs); return jtutil::abs(x-y) <= epsilon; } @@ -31,11 +31,11 @@ return jtutil::abs(x-y) <= epsilon; namespace jtutil { -template <typename fp> -bool fuzzy<fp>::is_integer(fp x) +template <typename fp_t> +bool fuzzy<fp_t>::is_integer(fp_t x) { -int i = round<fp>::to_integer(x); -return EQ(x, fp(i)); +int i = round<fp_t>::to_integer(x); +return EQ(x, fp_t(i)); } } // namespace jtutil:: @@ -43,12 +43,12 @@ return EQ(x, fp(i)); namespace jtutil { -template <typename fp> -int fuzzy<fp>::floor(fp x) +template <typename fp_t> +int fuzzy<fp_t>::floor(fp_t x) { -return fuzzy<fp>::is_integer(x) - ? round<fp>::to_integer(x) - : round<fp>::floor(x); +return fuzzy<fp_t>::is_integer(x) + ? round<fp_t>::to_integer(x) + : round<fp_t>::floor(x); } } // namespace jtutil:: @@ -56,12 +56,12 @@ return fuzzy<fp>::is_integer(x) namespace jtutil { -template <typename fp> -int fuzzy<fp>::ceiling(fp x) +template <typename fp_t> +int fuzzy<fp_t>::ceiling(fp_t x) { -return fuzzy<fp>::is_integer(x) - ? round<fp>::to_integer(x) - : round<fp>::ceiling(x); +return fuzzy<fp_t>::is_integer(x) + ? round<fp_t>::to_integer(x) + : round<fp_t>::ceiling(x); } } // namespace jtutil:: diff --git a/src/jtutil/linear_map.cc b/src/jtutil/linear_map.cc index ad4666b..636675e 100644 --- a/src/jtutil/linear_map.cc +++ b/src/jtutil/linear_map.cc @@ -25,13 +25,13 @@ using jtutil::error_exit; //****************************************************************************** // -// This function constructs a linear_map<fp> object. +// This function constructs a linear_map<fp_t> object. // namespace jtutil { -template <typename fp> -linear_map<fp>::linear_map(int min_int_in, int max_int_in, - fp min_fp_in, fp delta_fp_in, fp max_fp_in) +template <typename fp_t> +linear_map<fp_t>::linear_map(int min_int_in, int max_int_in, + fp_t min_fp_in, fp_t delta_fp_in, fp_t max_fp_in) : delta_(delta_fp_in), inverse_delta_(1.0 / delta_fp_in), min_int_(min_int_in), max_int_(max_int_in) { @@ -42,20 +42,20 @@ constructor_common(min_fp_in, max_fp_in); //****************************************************************************** // -// This function constructs a linear_map<fp> object with a subrange +// This function constructs a linear_map<fp_t> object with a subrange // of an existing one. // namespace jtutil { -template <typename fp> -linear_map<fp>::linear_map(const linear_map<fp> &lm_in, - int min_int_in, int max_int_in) // subrange +template <typename fp_t> +linear_map<fp_t>::linear_map(const linear_map<fp_t> &lm_in, + int min_int_in, int max_int_in) // subrange : delta_(lm_in.delta_fp()), inverse_delta_(lm_in.inverse_delta_fp()), min_int_(min_int_in), max_int_(max_int_in) { if (! (is_in_range(min_int_in) && is_in_range(max_int_in)) ) then error_exit(ERROR_EXIT, -"***** linear_map<fp>::linear_map:\n" +"***** linear_map<fp_t>::linear_map:\n" " min_int_in=%d and/or max_int_in=%d\n" " aren't in integer range [%d,%d] of existing linear_map!\n" , @@ -71,12 +71,12 @@ constructor_common(lm_in.fp_of_int_unchecked(min_int_in), // // This function does the common argument validation and setup for -// all the constructors of class linear_map<fp>:: . +// all the constructors of class linear_map<fp_t>:: . // namespace jtutil { -template <typename fp> -void linear_map<fp>::constructor_common(fp min_fp_in, fp max_fp_in) +template <typename fp_t> +void linear_map<fp_t>::constructor_common(fp_t min_fp_in, fp_t max_fp_in) // assumes // min_int_, max_int_, delta_, inverse_delta_ // are already initialized @@ -87,12 +87,12 @@ origin_ = 0.0; // temp value origin_ = min_fp_in - fp_of_int_unchecked(min_int()); // this should be guaranteed by the above calculation -assert(fuzzy<fp>::EQ(fp_of_int_unchecked(min_int()), min_fp_in)); +assert( fuzzy<fp_t>::EQ(fp_of_int_unchecked(min_int()), min_fp_in) ); // this is a test of the consistency of the input arguments -if (fuzzy<fp>::NE(fp_of_int_unchecked(max_int()), max_fp_in)) +if (fuzzy<fp_t>::NE(fp_of_int_unchecked(max_int()), max_fp_in)) then error_exit(ERROR_EXIT, -"***** linear_map<fp>::linear_map:\n" +"***** linear_map<fp_t>::linear_map:\n" " int range [%d,%d]\n" " and fp range [%g(%g)%g]\n" " are (fuzzily) inconsistent!\n" @@ -111,13 +111,13 @@ if (fuzzy<fp>::NE(fp_of_int_unchecked(max_int()), max_fp_in)) // namespace jtutil { -template <typename fp> -fp linear_map<fp>::fp_int_of_fp(fp x) +template <typename fp_t> +fp_t linear_map<fp_t>::fp_int_of_fp(fp_t x) const { if (! is_in_range(x)) then error_exit(ERROR_EXIT, -"***** linear_map<fp>::fp_int_of_fp:\n" +"***** linear_map<fp_t>::fp_int_of_fp:\n" " fp value x=%g is (fuzzily) outside the grid!\n" " {min(delta)max}_fp = %g(%g)%g\n" , @@ -137,32 +137,32 @@ return inverse_delta_ * (x - origin_); // result *isn't* fuzzily integral.) // // FIXME: -// Having to explicitly specify the namespace for jtutil::round<fp>:: +// Having to explicitly specify the namespace for jtutil::round<fp_t>:: // is ++ugly. :( // namespace jtutil { -template <typename fp> -int linear_map<fp>::int_of_fp(fp x, noninteger_action nia /* = error */) +template <typename fp_t> +int linear_map<fp_t>::int_of_fp(fp_t x, noninteger_action nia /* = nia_error */) const { -const fp fp_int = fp_int_of_fp(x); +const fp_t fp_int = fp_int_of_fp(x); -if (fuzzy<fp>::is_integer(fp_int)) +if (fuzzy<fp_t>::is_integer(fp_int)) then { // x is (fuzzily) a grid point ==> return that - return jtutil::round<fp>::to_integer(fp_int); // *** EARLY RETURN *** + return jtutil::round<fp_t>::to_integer(fp_int); // *** EARLY RETURN *** } // get to here ==> x isn't (fuzzily) a grid point static const char *const noninteger_msg = - "%s linear_map<fp>::int_of_fp:\n" + "%s linear_map<fp_t>::int_of_fp:\n" " x=%g isn't (fuzzily) a grid point!\n" " {min(delta)max}_fp() = %g(%g)%g\n" ; switch (nia) { -case error: +case nia_error: error_exit(ERROR_EXIT, noninteger_msg, "*****", @@ -170,26 +170,26 @@ case error: double(min_fp()), double(delta_fp()), double(max_fp())); /*NOTREACHED*/ -case warning: +case nia_warning: printf(noninteger_msg, "---", double(x), double(min_fp()), double(delta_fp()), double(max_fp())); // fall through -case round: - return jtutil::round<fp>::to_integer(fp_int); // *** EARLY RETURN *** +case nia_round: + return jtutil::round<fp_t>::to_integer(fp_int); // *** EARLY RETURN *** -case floor: - return jtutil::round<fp>::floor(fp_int); // *** EARLY RETURN *** +case nia_floor: + return jtutil::round<fp_t>::floor(fp_int); // *** EARLY RETURN *** -case ceiling: - return jtutil::round<fp>::ceiling(fp_int); // *** EARLY RETURN *** +case nia_ceiling: + return jtutil::round<fp_t>::ceiling(fp_int); // *** EARLY RETURN *** default: error_exit(PANIC_EXIT, -"***** linear_map<fp>::int_of_fp: illegal nia=(int)%d\n" -" (this should never happen!)\n" +"***** linear_map<fp_t>::int_of_fp: illegal nia=(int)%d\n" +" (this should never happen!)\n" , int(nia)); /*NOTREACHED*/ } @@ -207,35 +207,35 @@ return 0; // dummy return to quiet gcc // do if the result *isn't* fuzzily integral.) // // FIXME: -// Having to explicitly specify the namespace for jtutil::round<fp>:: +// Having to explicitly specify the namespace for jtutil::round<fp_t>:: // is ++ugly. :( // namespace jtutil { -template <typename fp> -int linear_map<fp>::delta_int_of_delta_fp(fp delta_x, - noninteger_action nia /* = error */) +template <typename fp_t> +int linear_map<fp_t>::delta_int_of_delta_fp + (fp_t delta_x, noninteger_action nia /* = nia_error */) const { -const fp fp_delta_int = inverse_delta_ * delta_x; +const fp_t fp_delta_int = inverse_delta_ * delta_x; -if (fuzzy<fp>::is_integer(fp_delta_int)) +if (fuzzy<fp_t>::is_integer(fp_delta_int)) then { // delta_x is (fuzzily) an integer number of grid spacings // ==> return that - return jtutil::round<fp>::to_integer(fp_delta_int); + return jtutil::round<fp_t>::to_integer(fp_delta_int); // *** EARLY RETURN *** } // get to here ==> delta_x isn't (fuzzily) an integer number of grid spacings static const char *const noninteger_msg = - "%s linear_map<fp>::delta_int_of_delta_fp:\n" + "%s linear_map<fp_t>::delta_int_of_delta_fp:\n" " delta_x=%g isn't (fuzzily) an integer number of grid spacings!\n" " {min(delta)max}_fp() = %g(%g)%g\n" ; switch (nia) { -case error: +case nia_error: error_exit(ERROR_EXIT, noninteger_msg, "*****", @@ -243,27 +243,28 @@ case error: double(min_fp()), double(delta_fp()), double(max_fp())); /*NOTREACHED*/ -case warning: +case nia_warning: printf(noninteger_msg, "---", double(delta_x), double(min_fp()), double(delta_fp()), double(max_fp())); // fall through -case round: - return jtutil::round<fp>::to_integer(fp_delta_int); +case nia_round: + return jtutil::round<fp_t>::to_integer(fp_delta_int); // *** EARLY RETURN *** -case floor: - return jtutil::round<fp>::floor(fp_delta_int); // *** EARLY RETURN *** +case nia_floor: + return jtutil::round<fp_t>::floor(fp_delta_int);// *** EARLY RETURN *** -case ceiling: - return jtutil::round<fp>::ceiling(fp_delta_int);// *** EARLY RETURN *** +case nia_ceiling: + return jtutil::round<fp_t>::ceiling(fp_delta_int); + // *** EARLY RETURN *** default: error_exit(PANIC_EXIT, -"***** linear_map<fp>::delta_int_of_delta_fp: illegal nia=(int)%d\n" -" (this should never happen!)\n" +"***** linear_map<fp_t>::delta_int_of_delta_fp: illegal nia=(int)%d\n" +" (this should never happen!)\n" , int(nia)); /*NOTREACHED*/ } @@ -280,4 +281,5 @@ return 0; // dummy return to quiet gcc // ***** template instantiation ***** // +template class jtutil::linear_map<float>; template class jtutil::linear_map<double>; diff --git a/src/jtutil/linear_map.hh b/src/jtutil/linear_map.hh index bd2d824..a04ee0a 100644 --- a/src/jtutil/linear_map.hh +++ b/src/jtutil/linear_map.hh @@ -27,7 +27,7 @@ namespace jtutil { -template <typename fp> +template <typename fp_t> class linear_map { public: @@ -48,30 +48,30 @@ public: } // convert int --> fp - fp fp_of_int_unchecked(int i) const + fp_t fp_of_int_unchecked(int i) const { return origin_ + delta_*i; } - fp fp_of_int(int i) const + fp_t fp_of_int(int i) const { assert(is_in_range(i)); return fp_of_int_unchecked(i); } // converg delta_int --> delta_fp - fp delta_fp_of_delta_int(int delta_i) const + fp_t delta_fp_of_delta_int(int delta_i) const { return delta_ * delta_i; } // fp bounds info - fp origin() const { return origin_; } - fp delta_fp() const { return delta_; } - fp inverse_delta_fp() const { return inverse_delta_; } - fp min_fp() const { return fp_of_int_unchecked(min_int_); } - fp max_fp() const { return fp_of_int_unchecked(max_int_); } - bool is_in_range(fp x) const + fp_t origin() const { return origin_; } + fp_t delta_fp() const { return delta_; } + fp_t inverse_delta_fp() const { return inverse_delta_; } + fp_t min_fp() const { return fp_of_int_unchecked(min_int_); } + fp_t max_fp() const { return fp_of_int_unchecked(max_int_); } + bool is_in_range(fp_t x) const { - return fuzzy<fp>::GE(x,min_fp()) - && fuzzy<fp>::LE(x,max_fp()); + return fuzzy<fp_t>::GE(x,min_fp()) + && fuzzy<fp_t>::LE(x,max_fp()); } - fp clamp(fp x) const + fp_t clamp(fp_t x) const { if (x < min_fp()) then return min_fp(); @@ -86,29 +86,31 @@ public: // convert fp --> int coordinate, but return result as fp // (which need not be fuzzily integral) - fp fp_int_of_fp(fp x) const; + fp_t fp_int_of_fp(fp_t x) const; // convert fp --> int, check being fuzzily integral enum noninteger_action // what to do if "int" // isn't fuzzily integral? { - error, // jtutil::error_exit(...) - warning, // print warning msg, then round to nearest - round, // (silently) round to nearest - floor, // (silently) round to -infinity - ceiling // (silently) round to +infinity + nia_error, // jtutil::error_exit(...) + nia_warning, // print warning msg, + // then round to nearest + nia_round, // (silently) round to nearest + nia_floor, // (silently) round to -infinity + nia_ceiling // (silently) round to +infinity }; - int int_of_fp(fp x, noninteger_action nia = error) const; + int int_of_fp(fp_t x, noninteger_action nia = nia_error) const; // convert delta_fp --> delta_int, check being fuzzily integral - int delta_int_of_delta_fp(fp delta_x, noninteger_action nia = error) + int delta_int_of_delta_fp(fp_t delta_x, + noninteger_action nia = nia_error) const; // constructors linear_map(int min_int_in, int max_int_in, - fp min_fp_in, fp delta_fp_in, fp max_fp_in); + fp_t min_fp_in, fp_t delta_fp_in, fp_t max_fp_in); // ... construct with subrange of existing linear_map - linear_map(const linear_map<fp> &lm_in, + linear_map(const linear_map<fp_t> &lm_in, int min_int_in, int max_int_in); // no need for explicit destructor, compiler-generated no-op is ok @@ -120,16 +122,16 @@ private: // common code (argument validation & setup) for all constructors // assumes min_int_, max_int_, delta_ already initialized, // other class members *not* initialized - void constructor_common(fp min_fp_in, fp max_fp_in); + void constructor_common(fp_t min_fp_in, fp_t max_fp_in); // these define the actual mapping // via the fp_of_int() function (above) - fp origin_, delta_; + fp_t origin_, delta_; // cache of 1.0/delta_ // ==> avoids fp divide in inverse_delta_fp() // ==> also makes fp --> int conversions slightly faster - fp inverse_delta_; + fp_t inverse_delta_; // bounds (inclusive) const int min_int_, max_int_; diff --git a/src/jtutil/norm.cc b/src/jtutil/norm.cc index bce9ee2..23cac5c 100644 --- a/src/jtutil/norm.cc +++ b/src/jtutil/norm.cc @@ -15,13 +15,13 @@ // specify data point namespace jtutil { -template <typename fp> - void norm<fp>::data(fp x) +template <typename fp_t> + void norm<fp_t>::data(fp_t x) { ++N_; sum_ += x; sum2_ += x*x; -infinity_norm_ = jtutil::max(infinity_norm_, jtutil::abs<fp>(x)); +infinity_norm_ = jtutil::max(infinity_norm_, jtutil::abs<fp_t>(x)); } } // namespace jtutil:: @@ -30,15 +30,15 @@ infinity_norm_ = jtutil::max(infinity_norm_, jtutil::abs<fp>(x)); // get norms etc namespace jtutil { -template<typename fp> - fp norm<fp>::mean() const { return sum_/fp(N_); } -template<typename fp> - fp norm<fp>::two_norm() const { return sqrt(sum2_); } -template<typename fp> - fp norm<fp>::rms_norm() const - { assert(is_nonempty()); return sqrt(sum2_/fp(N_)); } -template<typename fp> - fp norm<fp>::infinity_norm() const { return infinity_norm_; } +template<typename fp_t> + fp_t norm<fp_t>::mean() const { return sum_/fp_t(N_); } +template<typename fp_t> + fp_t norm<fp_t>::two_norm() const { return sqrt(sum2_); } +template<typename fp_t> + fp_t norm<fp_t>::rms_norm() const + { assert(is_nonempty()); return sqrt(sum2_/fp_t(N_)); } +template<typename fp_t> + fp_t norm<fp_t>::infinity_norm() const { return infinity_norm_; } } // namespace jtutil:: //****************************************************************************** diff --git a/src/jtutil/round.cc b/src/jtutil/round.cc index c0b094d..b990ee0 100644 --- a/src/jtutil/round.cc +++ b/src/jtutil/round.cc @@ -20,7 +20,7 @@ // *** Implementation Notes *** // // We assume throughout this code that C++'s "built-in" conversion -// from <fp> to integer takes the floor, at least for zero or positive +// from <fp_t> to integer takes the floor, at least for zero or positive // values. // @@ -29,8 +29,8 @@ // round to nearest integer, up for exact tie namespace jtutil { -template <typename fp> -int round<fp>::to_integer(fp x) +template <typename fp_t> +int round<fp_t>::to_integer(fp_t x) { return (x >= 0.0) ? int(x + 0.5) // eg 3.6 --> int(4.1) = 4 @@ -42,8 +42,8 @@ return (x >= 0.0) namespace jtutil { -template <typename fp> -int round<fp>::floor(fp x) +template <typename fp_t> +int round<fp_t>::floor(fp_t x) { return (x >= 0.0) ? int(x) @@ -55,11 +55,11 @@ return (x >= 0.0) namespace jtutil { -template <typename fp> -int round<fp>::ceiling(fp x) +template <typename fp_t> +int round<fp_t>::ceiling(fp_t x) { return (x >= 0.0) - ? int(x) + (x != fp(int(x))) + ? int(x) + (x != fp_t(int(x))) : - floor(-x); } } // namespace jtutil:: @@ -72,8 +72,5 @@ return (x >= 0.0) // ***** template instantiations ***** // -// instantiation for <float> template class jtutil::round<float>; - -// instantiations for <double> template class jtutil::round<double>; diff --git a/src/jtutil/test_fuzzy.cc b/src/jtutil/test_fuzzy.cc index b68d9e5..437df36 100644 --- a/src/jtutil/test_fuzzy.cc +++ b/src/jtutil/test_fuzzy.cc @@ -2,9 +2,9 @@ // $Header$ // // main -// check<fp> -// check_binary<fp> -// check_unary<fp> +// check<fp_t> +// check_binary<fp_t> +// check_unary<fp_t> // *** template instantiations #include <stdio.h> @@ -16,19 +16,19 @@ using jtutil::error_exit; using jtutil::fuzzy; // prototypes -template <typename fp> +template <typename fp_t> void check(const char* print_string, - fp x, fp y, + fp_t x, fp_t y, char xy_relation, bool unary_flag, bool x_is_fuzzy_integer, int fuzzy_floor_of_x, int fuzzy_ceiling_of_x); -template <typename fp> - void check_binary(fp x, fp y, +template <typename fp_t> + void check_binary(fp_t x, fp_t y, char xy_relation); -template <typename fp> - void check_unary(fp x, +template <typename fp_t> + void check_unary(fp_t x, bool x_is_fuzzy_integer, int fuzzy_floor_of_x, int fuzzy_ceiling_of_x); @@ -104,18 +104,17 @@ return 0; // // This function template is a driver to do all the tests for a single -// argument or pair of arguments. It calls check_binary<fp>() to do the -// binary-function tests, and optionally calls check_unary<fp>() to do the -// unary-function tests. +// argument or pair of arguments. It calls check_binary<fp_t>() to do +// the binary-function tests, and optionally calls check_unary<fp_t>() +// to do the unary-function tests. // -template <typename fp> +template <typename fp_t> void check(const char* print_string, - fp x, fp y, + fp_t x, fp_t y, char xy_relation, bool unary_flag, - bool x_is_fuzzy_integer, - int fuzzy_floor_of_x, - int fuzzy_ceiling_of_x) + bool x_is_fuzzy_integer, int fuzzy_floor_of_x, + int fuzzy_ceiling_of_x) { if (print_string != NULL) then printf(" testing %s %s: x=%.15f y=%.15f\n", @@ -123,30 +122,31 @@ if (print_string != NULL) unary_flag ? "op(x,y) + op(x)" : "op(x,y) only ", double(x), double(y)); -check_binary<fp>(x, y, xy_relation); +check_binary<fp_t>(x, y, xy_relation); if (unary_flag) - then check_unary(x, x_is_fuzzy_integer, fuzzy_floor_of_x, fuzzy_ceiling_of_x); + then check_unary(x, + x_is_fuzzy_integer, fuzzy_floor_of_x, fuzzy_ceiling_of_x); } //****************************************************************************** // // This function template tests the binary relations -// fuzzy<fp>::EQ() -// fuzzy<fp>::NE() -// fuzzy<fp>::LT() -// fuzzy<fp>::GT() -// fuzzy<fp>::LE() -// fuzzy<fp>::GE() +// fuzzy<fp_t>::EQ() +// fuzzy<fp_t>::NE() +// fuzzy<fp_t>::LT() +// fuzzy<fp_t>::GT() +// fuzzy<fp_t>::LE() +// fuzzy<fp_t>::GE() // for a single argument. // // The xy_relation argument must be one of '<', '=', or '>', indicating // the desired fuzzy relationship between the two arguments. The function // infers the correct values of the binary functions from this. // -template <typename fp> - void check_binary(fp x, fp y, +template <typename fp_t> + void check_binary(fp_t x, fp_t y, char xy_relation) { // @@ -154,14 +154,17 @@ template <typename fp> // // trichotomy (we have exactly one of <, =, > -assert( fuzzy<fp>::LT(x,y) + fuzzy<fp>::EQ(x,y) + fuzzy<fp>::GT(x,y) == 1 ); +assert( fuzzy<fp_t>::LT(x,y) + fuzzy<fp_t>::EQ(x,y) + fuzzy<fp_t>::GT(x,y) + == 1 ); // consistency of EQ() with NE() -assert( fuzzy<fp>::NE(x,y) == ! fuzzy<fp>::EQ(x,y) ); +assert( fuzzy<fp_t>::NE(x,y) == ! fuzzy<fp_t>::EQ(x,y) ); // consistency of LE() with LT() and EQ(), GE() with GT() and EQ() -assert( fuzzy<fp>::LE(x,y) == (fuzzy<fp>::LT(x,y) || fuzzy<fp>::EQ(x,y)) ); -assert( fuzzy<fp>::GE(x,y) == (fuzzy<fp>::GT(x,y) || fuzzy<fp>::EQ(x,y)) ); +assert( fuzzy<fp_t>::LE(x,y) + == (fuzzy<fp_t>::LT(x,y) || fuzzy<fp_t>::EQ(x,y)) ); +assert( fuzzy<fp_t>::GE(x,y) + == (fuzzy<fp_t>::GT(x,y) || fuzzy<fp_t>::EQ(x,y)) ); // @@ -174,24 +177,24 @@ assert( fuzzy<fp>::GE(x,y) == (fuzzy<fp>::GT(x,y) || fuzzy<fp>::EQ(x,y)) ); switch (xy_relation) { case '<': - assert( fuzzy<fp>::LT(x,y) == true ); - assert( fuzzy<fp>::EQ(x,y) == false ); - assert( fuzzy<fp>::GT(x,y) == false ); + assert( fuzzy<fp_t>::LT(x,y) == true ); + assert( fuzzy<fp_t>::EQ(x,y) == false ); + assert( fuzzy<fp_t>::GT(x,y) == false ); break; case '=': - assert( fuzzy<fp>::LT(x,y) == false ); - assert( fuzzy<fp>::EQ(x,y) == true ); - assert( fuzzy<fp>::GT(x,y) == false ); + assert( fuzzy<fp_t>::LT(x,y) == false ); + assert( fuzzy<fp_t>::EQ(x,y) == true ); + assert( fuzzy<fp_t>::GT(x,y) == false ); break; case '>': - assert( fuzzy<fp>::LT(x,y) == false ); - assert( fuzzy<fp>::EQ(x,y) == false ); - assert( fuzzy<fp>::GT(x,y) == true ); + assert( fuzzy<fp_t>::LT(x,y) == false ); + assert( fuzzy<fp_t>::EQ(x,y) == false ); + assert( fuzzy<fp_t>::GT(x,y) == true ); break; default: error_exit(PANIC_EXIT, -"***** check_binary<fp>: bad xy_relation=(int)'%c'\n" -" (this should never happen!)\n" +"***** check_binary<fp_t>: bad xy_relation=(int)'%c'\n" +" (this should never happen!)\n" , int(xy_relation)); /*NOTREACHED*/ } @@ -201,21 +204,21 @@ default: // // This function template tests the unary functions -// fuzzy<fp>::is_integer() -// fuzzy<fp>::floor() -// fuzzy<fp>::ceiling() +// fuzzy<fp_t>::is_integer() +// fuzzy<fp_t>::floor() +// fuzzy<fp_t>::ceiling() // for a single argument. // -template <typename fp> - void check_unary(fp x, +template <typename fp_t> + void check_unary(fp_t x, // remaining arguments are the correct results of... - bool x_is_fuzzy_integer, // ... fuzzy<fp>::is_integer(x) - int fuzzy_floor_of_x, // ... fuzzy<fp>::floor(x) - int fuzzy_ceiling_of_x) // ... fuzzy<fp>::ceiling(x) + bool x_is_fuzzy_integer, // fuzzy<fp_t>::is_integer(x) + int fuzzy_floor_of_x, // fuzzy<fp_t>::floor(x) + int fuzzy_ceiling_of_x) // fuzzy<fp_t>::ceiling(x) { -assert( fuzzy<fp>::is_integer(x) == x_is_fuzzy_integer ); -assert( fuzzy<fp>::floor(x) == fuzzy_floor_of_x ); -assert( fuzzy<fp>::ceiling(x) == fuzzy_ceiling_of_x ); +assert( fuzzy<fp_t>::is_integer(x) == x_is_fuzzy_integer ); +assert( fuzzy<fp_t>::floor(x) == fuzzy_floor_of_x ); +assert( fuzzy<fp_t>::ceiling(x) == fuzzy_ceiling_of_x ); } //****************************************************************************** @@ -224,8 +227,12 @@ assert( fuzzy<fp>::ceiling(x) == fuzzy_ceiling_of_x ); // template instantiations for <float> and <double> // -template void check<float>(const char* , float, float, char, bool, bool, int, int); -template void check<double>(const char* , double, double, char, bool, bool, int, int); +template void check<float>(const char*, + float, float, + char, bool, bool, int, int); +template void check<double>(const char*, + double, double, + char, bool, bool, int, int); template void check_binary<float>(float, float, char); template void check_binary<double>(double, double, char); diff --git a/src/jtutil/test_linear_map.cc b/src/jtutil/test_linear_map.cc index 0a185e7..f38d362 100644 --- a/src/jtutil/test_linear_map.cc +++ b/src/jtutil/test_linear_map.cc @@ -88,20 +88,20 @@ printf("checking delta roundings...\n"); double di = double(i); // test silent roundings - assert( lm.delta_int_of_delta_fp((di-0.49)*delta_fp, lm.round) == i ); - assert( lm.delta_int_of_delta_fp((di+0.49)*delta_fp, lm.round) == i ); + assert( lm.delta_int_of_delta_fp((di-0.49)*delta_fp, lm.nia_round) == i ); + assert( lm.delta_int_of_delta_fp((di+0.49)*delta_fp, lm.nia_round) == i ); // test floor/ceiling - assert( lm.delta_int_of_delta_fp((di-0.01)*delta_fp, lm.floor ) == i-1 ); - assert( lm.delta_int_of_delta_fp((di-0.01)*delta_fp, lm.ceiling) == i ); - assert( lm.delta_int_of_delta_fp((di+0.01)*delta_fp, lm.round ) == i ); - assert( lm.delta_int_of_delta_fp((di+0.01)*delta_fp, lm.ceiling) == i+1 ); + assert( lm.delta_int_of_delta_fp((di-0.01)*delta_fp, lm.nia_floor ) == i-1 ); + assert( lm.delta_int_of_delta_fp((di-0.01)*delta_fp, lm.nia_ceiling) == i ); + assert( lm.delta_int_of_delta_fp((di+0.01)*delta_fp, lm.nia_round ) == i ); + assert( lm.delta_int_of_delta_fp((di+0.01)*delta_fp, lm.nia_ceiling) == i+1 ); } } printf("this should produce a pair of warning messages...\n"); -assert( lm.delta_int_of_delta_fp(2.99*delta_fp, lm.warning) == 3 ); -assert( lm.delta_int_of_delta_fp(3.01*delta_fp, lm.warning) == 3 ); +assert( lm.delta_int_of_delta_fp(2.99*delta_fp, lm.nia_warning) == 3 ); +assert( lm.delta_int_of_delta_fp(3.01*delta_fp, lm.nia_warning) == 3 ); printf("checking mappings...\n"); { @@ -132,15 +132,15 @@ double x; // because we have *two* loop variables! // test silent rounding if (i > min_int) - assert( lm.int_of_fp(x - 0.49*delta_fp, lm.round) == i ); + assert( lm.int_of_fp(x - 0.49*delta_fp, lm.nia_round) == i ); if (i < max_int) - assert( lm.int_of_fp(x + 0.49*delta_fp, lm.round) == i ); + assert( lm.int_of_fp(x + 0.49*delta_fp, lm.nia_round) == i ); // test floor/ceiling if (i > min_int) - assert( lm.int_of_fp(x - 0.01*delta_fp, lm.floor) == i-1 ); + assert( lm.int_of_fp(x - 0.01*delta_fp, lm.nia_floor) == i-1 ); if (i < max_int) - assert( lm.int_of_fp(x + 0.01*delta_fp, lm.ceiling) == i+1 ); + assert( lm.int_of_fp(x + 0.01*delta_fp, lm.nia_ceiling) == i+1 ); // test zero-origin conversions assert( lm.zero_origin_int(i) == i - min_int ); @@ -150,9 +150,9 @@ double x; // because we have *two* loop variables! printf("this should produce a pair of warning messages...\n"); assert( lm.int_of_fp(min_fp + 0.01*delta_fp, - linear_map<double>::warning) == min_int ); + linear_map<double>::nia_warning) == min_int ); assert( lm.int_of_fp(max_fp - 0.01*delta_fp, - linear_map<double>::warning) == max_int ); + linear_map<double>::nia_warning) == max_int ); printf("this should produce an error message...\n"); #ifdef TEST_RANGE diff --git a/src/jtutil/test_round.cc b/src/jtutil/test_round.cc index e5a900f..5e3354a 100644 --- a/src/jtutil/test_round.cc +++ b/src/jtutil/test_round.cc @@ -1,4 +1,4 @@ -// test_round.cc -- test driver for round<fp> and round<double> +// test_round.cc -- test driver for round<float> and round<double> // $Header$ #include <stdio.h> @@ -23,7 +23,7 @@ int main(int argc, const char* const argv[]) // - // round<fp>::to_integer() + // round<fp_t>::to_integer() // assert( round<double>::to_integer(d - 0.49) == i ); @@ -48,7 +48,7 @@ int main(int argc, const char* const argv[]) // - // round<fp>::floor() + // round<fp_t>::floor() // assert( round<double>::floor(d - 0.49) == i-1 ); @@ -75,7 +75,7 @@ int main(int argc, const char* const argv[]) // - // round<fp>::ceiling() + // round<fp_t>::ceiling() // assert( round<double>::ceiling(d - 0.49) == i ); diff --git a/src/jtutil/util.hh b/src/jtutil/util.hh index 3cb2181..df3daeb 100644 --- a/src/jtutil/util.hh +++ b/src/jtutil/util.hh @@ -23,32 +23,32 @@ inline int is_odd (int i) { return (i & 0x1); } // FIXME: <algorithm> is supposed to have min/max, but these are // broken on too many platforms (eg older gcc versions) // -template <typename fp> - inline fp min(fp x, fp y) { return (x < y) ? x : y; } -template <typename fp> - inline fp max(fp x, fp y) { return (x > y) ? x : y; } -template <typename fp> - inline fp abs(fp x) { return (x > 0.0) ? x : -x; } - -// -// These functions raise their arguments (presumably floating-point) -// to various small-integer powers. -// FIXME: do we ever use these? -// -template <typename fp> - inline fp pow2(fp x) { return x*x; } -template <typename fp> - inline fp pow3(fp x) { return x*x*x; } -template <typename fp> - inline fp pow4(fp x) { return pow2(pow2(x)); } -template <typename fp> - inline fp pow5(fp x) { return x * pow4(x); } -template <typename fp> - inline fp pow6(fp x) { return pow3(pow2(x)); } -template <typename fp> - inline fp pow7(fp x) { return x * pow6(x); } -template <typename fp> - inline fp pow8(fp x) { return pow2(pow2(pow2(x))); } +template <typename T> + inline T min(T x, T y) { return (x < y) ? x : y; } +template <typename T> + inline T max(T x, T y) { return (x > y) ? x : y; } +template <typename T> + inline T abs(T x) { return (x > 0.0) ? x : -x; } + +// +// These functions raise their arguments to various small-integer powers. +// +template <typename T> + inline T pow2(T x) { return x*x; } +template <typename T> + inline T pow3(T x) { return x*x*x; } +template <typename T> + inline T pow4(T x) { return pow2(pow2(x)); } +#ifdef NOT_USED +template <typename T> + inline T pow5(T x) { return x * pow4(x); } +template <typename T> + inline T pow6(T x) { return pow3(pow2(x)); } +template <typename T> + inline T pow7(T x) { return x * pow6(x); } +template <typename T> + inline T pow8(T x) { return pow2(pow2(pow2(x))); } +#endif // // misc math stuff @@ -66,10 +66,10 @@ double modulo_reduce(double x, double xmod, double xmin, double xmax); // #ifdef PI // from "jt/stdc.h" // convert degrees <--> radians - template <typename fp> - inline fp degrees_of_radians(fp radians) { return (180.0/PI)*radians; } - template <typename fp> - inline fp radians_of_degrees(fp degrees) { return (PI/180.0)*degrees; } + template <typename fp_t> + inline fp_t degrees_of_radians(fp_t radians) { return (180.0/PI)*radians; } + template <typename fp_t> + inline fp_t radians_of_degrees(fp_t degrees) { return (PI/180.0)*degrees; } #endif //****************************************************************************** @@ -77,18 +77,18 @@ double modulo_reduce(double x, double xmod, double xmin, double xmax); // // This template class computes means, 2-norms, rms-norms, and infinity-norms. // -template <typename fp> +template <typename fp_t> class norm { public: // get norms etc - fp mean() const; - fp two_norm() const; // sqrt(sum x_i^2) - fp rms_norm() const; // sqrt(average of x_i^2) - fp infinity_norm() const; // max(|x_i|) + fp_t mean() const; + fp_t two_norm() const; // sqrt(sum x_i^2) + fp_t rms_norm() const; // sqrt(average of x_i^2) + fp_t infinity_norm() const; // max(|x_i|) // specify data point - void data(fp x); + void data(fp_t x); // have any data points been specified? bool is_empty() const { return N_ == 0; } @@ -112,9 +112,9 @@ private: private: int N_; // # of data points - fp sum_; // sum(data) - fp sum2_; // sum(data^2) - fp infinity_norm_; // max |data| + fp_t sum_; // sum(data) + fp_t sum2_; // sum(data^2) + fp_t infinity_norm_; // max |data| }; //****************************************************************************** @@ -133,32 +133,32 @@ private: // than the individual members being conceptually-unrelated templates // ... moreover, we need the *data* member (template) tolerance , and // it seems C++ doesn't grok data templates which aren't in a class -template <typename fp> +template <typename fp_t> class fuzzy { public: // comparison tolerance (may be modified by user code if needed) - static fp get_tolerance() { return tolerance_; } - static void set_tolerance(fp new_tolerance) + static fp_t get_tolerance() { return tolerance_; } + static void set_tolerance(fp_t new_tolerance) { tolerance_ = new_tolerance; } // fuzzy commparisons - static bool EQ(fp x, fp y); - static bool NE(fp x, fp y) { return ! EQ(x,y); } - static bool LT(fp x, fp y) { return EQ(x,y) ? false : (x < y); } - static bool LE(fp x, fp y) { return EQ(x,y) ? true : (x < y); } - static bool GT(fp x, fp y) { return EQ(x,y) ? false : (x > y); } - static bool GE(fp x, fp y) { return EQ(x,y) ? true : (x > y); } + static bool EQ(fp_t x, fp_t y); + static bool NE(fp_t x, fp_t y) { return ! EQ(x,y); } + static bool LT(fp_t x, fp_t y) { return EQ(x,y) ? false : (x < y); } + static bool LE(fp_t x, fp_t y) { return EQ(x,y) ? true : (x < y); } + static bool GT(fp_t x, fp_t y) { return EQ(x,y) ? false : (x > y); } + static bool GE(fp_t x, fp_t y) { return EQ(x,y) ? true : (x > y); } - static bool is_integer(fp x); // is x fuzzily an integer? - static int floor(fp x); // round x fuzzily down to integer - static int ceiling(fp x); // round x fuzzily up to integer + static bool is_integer(fp_t x); // is x fuzzily an integer? + static int floor(fp_t x); // round x fuzzily down to integer + static int ceiling(fp_t x); // round x fuzzily up to integer private: // comparison tolerance // ... must be explicitly initialized when instantiating - // for a new <fp> type, see "fuzzy.cc" for details/examples - static fp tolerance_; + // for a new <fp_t> type, see "fuzzy.cc" for details/examples + static fp_t tolerance_; }; //****************************************************************************** @@ -172,14 +172,14 @@ private: // ... it's a class, not a namespace, because we want to express the // semantics that the entire class is a single template, rather // than the individual members being conceptually-unrelated templates -template <typename fp> +template <typename fp_t> class round { public: - static int to_integer(fp x); // round to nearest integer + static int to_integer(fp_t x); // round to nearest integer - static int floor(fp x); // round down to integer - static int ceiling(fp x); // round up to integer + static int floor(fp_t x); // round down to integer + static int ceiling(fp_t x); // round up to integer }; //****************************************************************************** |