feisty meow concerns codebase  2.140
math_bits.h
Go to the documentation of this file.
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 
22 #include <basis/astring.h>
23 
24 namespace geometric {
25 
28 
35 
36 // type identification functions:
37 
39 
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))); }
45 
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 
54 template <class numeric_type>
55 bool is_short(numeric_type) { return sizeof(numeric_type) == 2; }
56 
58 template <class numeric_type>
59 bool is_unsigned(numeric_type t) { t = -1; return t > 0; }
61 template <class numeric_type>
62 bool is_signed(numeric_type t) { return !is_unsigned(t); }
63 
65 
68 template <class numeric_type>
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 
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
void insert(int position, const astring &to_insert)
Copies "to_insert" into "this" at the "position".
Definition: astring.cpp:892
type absolute_value(type a)
Returns a if a is non-negative, and returns -a otherwise.
Definition: functions.h:33
Contains all of our objects for geometry and avoids name clashes.
Definition: angle.h:25
bool is_signed(numeric_type t)
returns true if the templated type is signed.
Definition: math_bits.h:62
bool is_floating_point(numeric_type t)
returns true if the specified numeric_type is a floating_point type.
Definition: math_bits.h:42
bool is_unsigned(numeric_type t)
returns true if the templated type is unsigned.
Definition: math_bits.h:59
basis::astring numeric_specifier(numeric_type t)
Guesses the formatting string needed for the type provided.
Definition: math_bits.h:69
astring crop_non_numeric(const astring &input)
Eats the non-numeric characters from the front of "input".
Definition: math_bits.cpp:37
astring crop_numeric(const astring &input)
Eats the numeric characters from the front of "input".
Definition: math_bits.cpp:23
bool is_integral(numeric_type t)
returns true if the instantiation type is an integer.
Definition: math_bits.h:48
bool is_short(numeric_type)
returns true if the specified type is short on the PC.
Definition: math_bits.h:55