1 #ifndef MATHEMATICAL_OPERATIONS_GROUP
2 #define MATHEMATICAL_OPERATIONS_GROUP
4 /*****************************************************************************\
6 * Name : mathematical operations group *
7 * Author : Chris Koeritz *
9 *******************************************************************************
10 * Copyright (c) 1996-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
19 @brief Provides some fairly low-level math support.
22 #include <basis/astring.h>
26 basis::astring crop_numeric(const basis::astring &input);
27 //!< Eats the numeric characters from the front of "input".
28 /*!< This and crop_non_numeric() return a string that is constructed from
29 "input" by chopping the numerical (or non-numerical) characters off of the
30 beginning. The types of numbers supported are floating point, but it will
31 generally work for integers also (problems arise when mixing in
32 the period, e, E, +, and - characters with integer numbers). */
33 basis::astring crop_non_numeric(const basis::astring &input);
34 //!< Eats the non-numeric characters from the front of "input".
36 // type identification functions:
38 //! returns true if the specified numeric_type is a floating_point type.
39 /*! this is useful in templates where one wants to know about the templated
41 template <class numeric_type>
42 bool is_floating_point(numeric_type t)
43 { t = numeric_type(5.1); t = numeric_type(t * 3.0);
44 return 0.001 < float(basis::absolute_value(numeric_type(t - 15.0))); }
46 //! returns true if the instantiation type is an integer.
47 template <class numeric_type>
48 bool is_integral(numeric_type t) { return !is_floating_point(t); }
50 // the following functions (is_short, is_signed and is_unsigned) are only
51 // useful for integral types.
53 //! returns true if the specified type is short on the PC.
54 template <class numeric_type>
55 bool is_short(numeric_type) { return sizeof(numeric_type) == 2; }
57 //! returns true if the templated type is unsigned.
58 template <class numeric_type>
59 bool is_unsigned(numeric_type t) { t = -1; return t > 0; }
60 //! returns true if the templated type is signed.
61 template <class numeric_type>
62 bool is_signed(numeric_type t) { return !is_unsigned(t); }
64 //! Guesses the formatting string needed for the type provided.
65 /*! Returns a string that is appropriate as the format specifier of a printf
66 (or the astring constructor) given the template type. templates can rely
67 on this to print numerical types correctly. */
68 template <class numeric_type>
69 basis::astring numeric_specifier(numeric_type t) {
70 basis::astring to_return("%d");
71 if (is_floating_point(t))
72 to_return = basis::astring("%f");
75 to_return = basis::astring("%u");
77 to_return.insert(1, "h");
84 #endif // outer guard.