first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / graphiq / library / geometric / math_bits.h
1 #ifndef MATHEMATICAL_OPERATIONS_GROUP
2 #define MATHEMATICAL_OPERATIONS_GROUP
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : mathematical operations group                                     *
7 *  Author : Chris Koeritz                                                     *
8 *                                                                             *
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 \*****************************************************************************/
17
18 /*! @file math_bits.h
19   @brief Provides some fairly low-level math support.
20 */
21
22 #include <basis/astring.h>
23
24 namespace geometric {
25
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".
35
36 // type identification functions:
37
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
40 type. */
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(absolute_value(numeric_type(t - 15.0))); }
45
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); }
49
50 // the following functions (is_short, is_signed and is_unsigned) are only
51 // useful for integral types.
52
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; }
56
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); }
63
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");
73   else {  // integral.
74     if (is_unsigned(t))
75       to_return = basis::astring("%u");
76     if (is_short(t))
77       to_return.insert(1, "h");
78   }
79   return to_return;
80 }
81
82 }
83
84 #endif // outer guard.
85