GeographicLib 2.5.2
Loading...
Searching...
No Matches
Utility.hpp
Go to the documentation of this file.
1/**
2 * \file Utility.hpp
3 * \brief Header for GeographicLib::Utility class
4 *
5 * Copyright (c) Charles Karney (2011-2024) <karney@alum.mit.edu> and licensed
6 * under the MIT/X11 License. For more information, see
7 * https://geographiclib.sourceforge.io/
8 **********************************************************************/
9
10#if !defined(GEOGRAPHICLIB_UTILITY_HPP)
11#define GEOGRAPHICLIB_UTILITY_HPP 1
12
14#include <iomanip>
15#include <vector>
16#include <sstream>
17#include <cctype>
18#include <ctime>
19#include <cstring>
20
21#if defined(_MSC_VER)
22// Squelch warnings about constant conditional expressions
23# pragma warning (push)
24# pragma warning (disable: 4127)
25#endif
26
27namespace GeographicLib {
28
29 /**
30 * \brief Some utility routines for %GeographicLib
31 *
32 * Example of use:
33 * \include example-Utility.cpp
34 **********************************************************************/
36 private:
37 static bool gregorian(int y, int m, int d) {
38 // The original cut over to the Gregorian calendar in Pope Gregory XIII's
39 // time had 1582-10-04 followed by 1582-10-15. Here we implement the
40 // switch over used by the English-speaking world where 1752-09-02 was
41 // followed by 1752-09-14. We also assume that the year always begins
42 // with January 1, whereas in reality it often was reckoned to begin in
43 // March.
44 return 100 * (100 * y + m) + d >= 17520914; // or 15821015
45 }
46 static bool gregorian(int s) {
47 return s >= 639799; // 1752-09-14
48 }
49 public:
50
51 /**
52 * Convert a date to the day numbering sequentially starting with
53 * 0001-01-01 as day 1.
54 *
55 * @param[in] y the year (must be positive).
56 * @param[in] m the month, Jan = 1, etc. (must be positive). Default = 1.
57 * @param[in] d the day of the month (must be positive). Default = 1.
58 * @return the sequential day number.
59 **********************************************************************/
60 static int day(int y, int m = 1, int d = 1);
61
62 /**
63 * Convert a date to the day numbering sequentially starting with
64 * 0001-01-01 as day 1.
65 *
66 * @param[in] y the year (must be positive).
67 * @param[in] m the month, Jan = 1, etc. (must be positive). Default = 1.
68 * @param[in] d the day of the month (must be positive). Default = 1.
69 * @param[in] check whether to check the date.
70 * @exception GeographicErr if the date is invalid and \e check is true.
71 * @return the sequential day number.
72 **********************************************************************/
73 static int day(int y, int m, int d, bool check);
74
75 /**
76 * Given a day (counting from 0001-01-01 as day 1), return the date.
77 *
78 * @param[in] s the sequential day number (must be positive)
79 * @param[out] y the year.
80 * @param[out] m the month, Jan = 1, etc.
81 * @param[out] d the day of the month.
82 **********************************************************************/
83 static void date(int s, int& y, int& m, int& d);
84
85 /**
86 * Given a date as a string in the format yyyy, yyyy-mm, or yyyy-mm-dd,
87 * return the numeric values for the year, month, and day. No checking is
88 * done on these values. The string "now" is interpreted as the present
89 * date (in UTC).
90 *
91 * @param[in] s the date in string format.
92 * @param[out] y the year.
93 * @param[out] m the month, Jan = 1, etc.
94 * @param[out] d the day of the month.
95 * @exception GeographicErr is \e s is malformed.
96 **********************************************************************/
97 static void date(const std::string& s, int& y, int& m, int& d);
98
99 /**
100 * Given the date, return the day of the week.
101 *
102 * @param[in] y the year (must be positive).
103 * @param[in] m the month, Jan = 1, etc. (must be positive).
104 * @param[in] d the day of the month (must be positive).
105 * @return the day of the week with Sunday, Monday--Saturday = 0,
106 * 1--6.
107 **********************************************************************/
108 static int dow(int y, int m, int d) { return dow(day(y, m, d)); }
109
110 /**
111 * Given the sequential day, return the day of the week.
112 *
113 * @param[in] s the sequential day (must be positive).
114 * @return the day of the week with Sunday, Monday--Saturday = 0,
115 * 1--6.
116 **********************************************************************/
117 static int dow(int s) {
118 return (s + 5) % 7; // The 5 offset makes day 1 (0001-01-01) a Saturday.
119 }
120
121 /**
122 * Convert a string representing a date to a fractional year.
123 *
124 * @tparam T the type of the argument.
125 * @param[in] s the string to be converted.
126 * @exception GeographicErr if \e s can't be interpreted as a date.
127 * @return the fractional year.
128 *
129 * The string is first read as an ordinary number (e.g., 2010 or 2012.5);
130 * if this is successful, the value is returned. Otherwise the string
131 * should be of the form yyyy-mm or yyyy-mm-dd and this is converted to a
132 * number with 2010-01-01 giving 2010.0 and 2012-07-03 giving 2012.5. The
133 * string "now" is interpreted as the present date.
134 **********************************************************************/
135 template<typename T> static T fractionalyear(const std::string& s) {
136 try {
137 return val<T>(s);
138 }
139 catch (const std::exception&) {}
140 int y, m, d;
141 date(s, y, m, d);
142 int t = day(y, m, d, true);
143 return T(y) + T(t - day(y)) / T(day(y + 1) - day(y));
144 }
145
146 /**
147 * Convert a object of type T to a string.
148 *
149 * @tparam T the type of the argument.
150 * @param[in] x the value to be converted.
151 * @param[in] p the precision used (default &minus;1).
152 * @exception std::bad_alloc if memory for the string can't be allocated.
153 * @return the string representation.
154 *
155 * If \e p &ge; 0, then the number fixed format is used with \e p bits of
156 * precision. With \e p < 0, there is no manipulation of the format,
157 * except that <code>boolalpha</code> is used to represent bools as "true"
158 * and "false". There is an overload of this function if T is Math::real;
159 * this deals with inf and nan.
160 **********************************************************************/
161 template<typename T> static std::string str(T x, int p = -1) {
162 std::ostringstream s;
163 if (p >= 0) s << std::fixed << std::setprecision(p);
164 s << std::boolalpha << x; return s.str();
165 }
166
167 /**
168 * Trim the white space from the beginning and end of a string.
169 *
170 * @param[in] s the string to be trimmed
171 * @return the trimmed string
172 **********************************************************************/
173 static std::string trim(const std::string& s);
174
175 /**
176 * Lookup up a character in a string.
177 *
178 * @param[in] s the string to be searched.
179 * @param[in] c the character to look for.
180 * @return the index of the first occurrence character in the string or
181 * &minus;1 is the character is not present.
182 *
183 * \e c is converted to upper case before search \e s. Therefore, it is
184 * intended that \e s should not contain any lower case letters.
185 **********************************************************************/
186 static int lookup(const std::string& s, char c);
187
188 /**
189 * Lookup up a character in a char*.
190 *
191 * @param[in] s the char* string to be searched.
192 * @param[in] c the character to look for.
193 * @return the index of the first occurrence character in the string or
194 * &minus;1 is the character is not present.
195 *
196 * \e c is converted to upper case before search \e s. Therefore, it is
197 * intended that \e s should not contain any lower case letters.
198 **********************************************************************/
199 static int lookup(const char* s, char c);
200
201 /**
202 * Convert a string to type T.
203 *
204 * @tparam T the type of the return value.
205 * @param[in] s the string to be converted.
206 * @exception GeographicErr is \e s is not readable as a T.
207 * @return object of type T.
208 *
209 * White space at the beginning and end of \e s is ignored.
210 *
211 * Special handling is provided for some types.
212 *
213 * If T is a floating point type, then inf and nan are recognized.
214 *
215 * If T is bool, then \e s should either be string a representing 0 (false)
216 * or 1 (true) or one of the strings
217 * - "false", "f", "nil", "no", "n", "off", or "" meaning false,
218 * - "true", "t", "yes", "y", or "on" meaning true;
219 * .
220 * case is ignored.
221 *
222 * If T is std::string, then \e s is returned (with the white space at the
223 * beginning and end removed).
224 **********************************************************************/
225 template<typename T> static T val(const std::string& s) {
226 // If T is bool, then the specialization val<bool>() defined below is
227 // used.
228 T x;
229 std::string errmsg, t(trim(s));
230 do { // Executed once (provides the ability to break)
231 std::istringstream is(t);
232 if (!(is >> x)) {
233 errmsg = "Cannot decode " + t;
234 break;
235 }
236 int pos = int(is.tellg()); // Returns -1 at end of string?
237 if (!(pos < 0 || pos == int(t.size()))) {
238 errmsg = "Extra text " + t.substr(pos) + " at end of " + t;
239 break;
240 }
241 return x;
242 } while (false);
243 x = std::numeric_limits<T>::is_integer ? 0 : nummatch<T>(t);
244 if (x == 0)
245 throw GeographicErr(errmsg);
246 return x;
247 }
248
249 /**
250 * Match "nan" and "inf" (and variants thereof) in a string.
251 *
252 * @tparam T the type of the return value (this should be a floating point
253 * type).
254 * @param[in] s the string to be matched.
255 * @return appropriate special value (&plusmn;&infin;, nan) or 0 if none is
256 * found.
257 *
258 * White space is not allowed at the beginning or end of \e s.
259 **********************************************************************/
260 template<typename T> static T nummatch(const std::string& s) {
261 if (s.length() < 3)
262 return 0;
263 std::string t(s);
264 for (std::string::iterator p = t.begin(); p != t.end(); ++p)
265 *p = char(std::toupper(*p));
266 for (size_t i = s.length(); i--;)
267 t[i] = char(std::toupper(s[i]));
268 int sign = t[0] == '-' ? -1 : 1;
269 std::string::size_type p0 = t[0] == '-' || t[0] == '+' ? 1 : 0;
270 std::string::size_type p1 = t.find_last_not_of('0');
271 if (p1 == std::string::npos || p1 + 1 < p0 + 3)
272 return 0;
273 // Strip off sign and trailing 0s
274 t = t.substr(p0, p1 + 1 - p0); // Length at least 3
275 if (t == "NAN" || t == "1.#QNAN" || t == "1.#SNAN" || t == "1.#IND" ||
276 t == "1.#R")
277 return Math::NaN<T>();
278 else if (t == "INF" || t == "1.#INF" || t == "INFINITY")
279 return sign * Math::infinity<T>();
280 return 0;
281 }
282
283 /**
284 * Read a simple fraction, e.g., 3/4, from a string to an object of type T.
285 *
286 * @tparam T the type of the return value.
287 * @param[in] s the string to be converted.
288 * @exception GeographicErr is \e s is not readable as a fraction of type
289 * T.
290 * @return object of type T
291 *
292 * \note The msys shell under Windows converts arguments which look like
293 * pathnames into their Windows equivalents. As a result the argument
294 * "-1/300" gets mangled into something unrecognizable. A workaround is to
295 * use a floating point number in the numerator, i.e., "-1.0/300". (Recent
296 * versions of the msys shell appear \e not to have this problem.)
297 **********************************************************************/
298 template<typename T> static T fract(const std::string& s) {
299 std::string::size_type delim = s.find('/');
300 return
301 !(delim != std::string::npos && delim >= 1 && delim + 2 <= s.size()) ?
302 val<T>(s) :
303 // delim in [1, size() - 2]
304 val<T>(s.substr(0, delim)) / val<T>(s.substr(delim + 1));
305 }
306
307 /**
308 * Read data of type ExtT from a binary stream to an array of type IntT.
309 * The data in the file is in (bigendp ? big : little)-endian format.
310 *
311 * @tparam ExtT the type of the objects in the binary stream (external).
312 * @tparam IntT the type of the objects in the array (internal).
313 * @tparam bigendp true if the external storage format is big-endian.
314 * @param[in] str the input stream containing the data of type ExtT
315 * (external).
316 * @param[out] array the output array of type IntT (internal).
317 * @param[in] num the size of the array.
318 * @exception GeographicErr if the data cannot be read.
319 *
320 * This routine is used to read binary data files for the Geoid,
321 * GravityModel, and MagneticModel classes. In the case of GravityModel
322 * and MagneticMode, the data is published by a government agency as text
323 * files, and the coefficient to realize the models are converted to a
324 * double precision binary format to minimize storage and to simplify
325 * reading the data.
326 *
327 * For GEOGRAPHIC_PRECISION == 2, the data is read faithfully. For
328 * GEOGRAPHICLIB_PRECISION > 2, external data of type double is interpreted
329 * as an approximation of an exact decimal value; this exact number is
330 * convered to a real number at the higher precision.
331 **********************************************************************/
332 template<typename ExtT, typename IntT, bool bigendp>
333 static void readarray(std::istream& str, IntT array[], size_t num) {
334#if GEOGRAPHICLIB_PRECISION < 4
335 // for C++17 use if constexpr
336 if (sizeof(IntT) == sizeof(ExtT) &&
337 std::numeric_limits<IntT>::is_integer ==
338 std::numeric_limits<ExtT>::is_integer)
339 {
340 // Data is compatible (aside from the issue of endian-ness).
341 str.read(reinterpret_cast<char*>(array), num * sizeof(ExtT));
342 if (!str.good())
343 throw GeographicErr("Failure reading data");
344 // for C++17 use if constexpr
345 if (bigendp != Math::bigendian) { // endian mismatch -> swap bytes
346 for (size_t i = num; i--;)
347 array[i] = Math::swab<IntT>(array[i]);
348 }
349 }
350 else
351#endif
352 {
353 const int bufsize = 1024; // read this many values at a time
354 ExtT buffer[bufsize]; // temporary buffer
355 int k = int(num); // data values left to read
356 int i = 0; // index into output array
357 while (k) {
358 int n = (std::min)(k, bufsize);
359 str.read(reinterpret_cast<char*>(buffer), n * sizeof(ExtT));
360 if (!str.good())
361 throw GeographicErr("Failure reading data");
362 for (int j = 0; j < n; ++j) {
363 // fix endian-ness
364 ExtT x = bigendp == Math::bigendian ? buffer[j] :
365 Math::swab<ExtT>(buffer[j]);
366#if GEOGRAPHICLIB_PRECISION > 2
367 // for C++17 use if constexpr folding in test of
368 if (typeid(ExtT) == typeid(double) &&
369 typeid(IntT) == typeid(Math::real)) {
370 // readarray is used to read in coefficient data rapidly. Thus
371 // 8.3n is stored in its IEEE double representation. This is
372 // fine is the working precision is double. However, when
373 // working at higher precision, how should be interpret the
374 // constant 8.3 appearing in a published table? Possibilities
375 // are
376 //
377 // (a) treat this as an exact decimal number 83/10;
378 //
379 // (b) treat this as the approximate decimal representation of
380 // an exact double precision number 2336242306698445/2^48 =
381 // 8.300000000000000710542735760100185871124267578125
382 //
383 // Here use (a) if the number of significant digits in the
384 // number is 15 or less. Otherwise, we use (b).
385 //
386 // We implement this as follows. Any double which can be
387 // represented as a decimal number with precision 14 = digis10
388 // - 1 (= 15 sig figs) is treated as an approximation to that
389 // decimal number. The high precision number is then obtained
390 // by reading the decimal number at that precision. Otherwise
391 // the double is treated as exact. The high precision number
392 // is obtained by adding zeros in the binary fraction.
393 //
394 // N.B. printing with precision 14 = digis10 - 1 allows short
395 // numbers to be represended with trailing zeros. This isn't
396 // necessarily the case with precision = digits10, e.g., 8.3
397 // becomes 8.300000000000001.
398 //
399 // This prescription doesn't exactly implement the method
400 // proposed. If the published table of numbers includes
401 // 8.300000000000001, this will be interpreted as 8.3. This
402 // doesn't apply to any published magnetic or gravity data.
403 // E.g., the coefficients for EGM96, resp. EGM2008, are given
404 // with precision 11, resp. 14.
405 //
406 // This conversion of doubles to Math::real comes at a
407 // substantial cost. It adds about 14 s to the time it takes
408 // to read the egm2008 gravity model for quad and mpfr
409 // precisions. This is acceptable, however, because high
410 // precision is only used for benchmarking.
411 std::ostringstream str;
412 str << std::scientific
413 << std::setprecision(std::numeric_limits<ExtT>::digits10-1)
414 << x;
415 // Code for GEOGRAPHILIB_PRECISION > 2 and types double/real
416 if (val<ExtT>(str.str()) == x)
417 array[i++] = val<IntT>(str.str());
418 else
419 array[i++] = IntT(x);
420 } else {
421 // Code for GEOGRAPHILIB_PRECISION > 2 but types not
422 // double/real
423 array[i++] = IntT(x);
424 }
425#else
426 // Code for GEOGRAPHILIB_PRECISION <= 2
427 array[i++] = IntT(x);
428#endif
429 }
430 k -= n;
431 }
432 }
433 return;
434 }
435
436 /**
437 * Read data of type ExtT from a binary stream to a vector array of type
438 * IntT. The data in the file is in (bigendp ? big : little)-endian
439 * format.
440 *
441 * @tparam ExtT the type of the objects in the binary stream (external).
442 * @tparam IntT the type of the objects in the array (internal).
443 * @tparam bigendp true if the external storage format is big-endian.
444 * @param[in] str the input stream containing the data of type ExtT
445 * (external).
446 * @param[out] array the output vector of type IntT (internal).
447 * @exception GeographicErr if the data cannot be read.
448 **********************************************************************/
449 template<typename ExtT, typename IntT, bool bigendp>
450 static void readarray(std::istream& str, std::vector<IntT>& array) {
451 if (array.size() > 0)
452 readarray<ExtT, IntT, bigendp>(str, &array[0], array.size());
453 }
454
455 /**
456 * Write data in an array of type IntT as type ExtT to a binary stream.
457 * The data in the file is in (bigendp ? big : little)-endian format.
458 *
459 * @tparam ExtT the type of the objects in the binary stream (external).
460 * @tparam IntT the type of the objects in the array (internal).
461 * @tparam bigendp true if the external storage format is big-endian.
462 * @param[out] str the output stream for the data of type ExtT (external).
463 * @param[in] array the input array of type IntT (internal).
464 * @param[in] num the size of the array.
465 * @exception GeographicErr if the data cannot be written.
466 **********************************************************************/
467 template<typename ExtT, typename IntT, bool bigendp>
468 static void writearray(std::ostream& str, const IntT array[], size_t num)
469 {
470#if GEOGRAPHICLIB_PRECISION < 4
471 if (sizeof(IntT) == sizeof(ExtT) &&
472 std::numeric_limits<IntT>::is_integer ==
473 std::numeric_limits<ExtT>::is_integer &&
474 bigendp == Math::bigendian)
475 {
476 // Data is compatible (including endian-ness).
477 str.write(reinterpret_cast<const char*>(array), num * sizeof(ExtT));
478 if (!str.good())
479 throw GeographicErr("Failure writing data");
480 }
481 else
482#endif
483 {
484 const int bufsize = 1024; // write this many values at a time
485 ExtT buffer[bufsize]; // temporary buffer
486 int k = int(num); // data values left to write
487 int i = 0; // index into output array
488 while (k) {
489 int n = (std::min)(k, bufsize);
490 for (int j = 0; j < n; ++j)
491 // cast to ExtT and fix endian-ness
492 buffer[j] = bigendp == Math::bigendian ? ExtT(array[i++]) :
493 Math::swab<ExtT>(ExtT(array[i++]));
494 str.write(reinterpret_cast<const char*>(buffer), n * sizeof(ExtT));
495 if (!str.good())
496 throw GeographicErr("Failure writing data");
497 k -= n;
498 }
499 }
500 return;
501 }
502
503 /**
504 * Write data in an array of type IntT as type ExtT to a binary stream.
505 * The data in the file is in (bigendp ? big : little)-endian format.
506 *
507 * @tparam ExtT the type of the objects in the binary stream (external).
508 * @tparam IntT the type of the objects in the array (internal).
509 * @tparam bigendp true if the external storage format is big-endian.
510 * @param[out] str the output stream for the data of type ExtT (external).
511 * @param[in] array the input vector of type IntT (internal).
512 * @exception GeographicErr if the data cannot be written.
513 **********************************************************************/
514 template<typename ExtT, typename IntT, bool bigendp>
515 static void writearray(std::ostream& str, std::vector<IntT>& array) {
516 if (array.size() > 0)
517 writearray<ExtT, IntT, bigendp>(str, &array[0], array.size());
518 }
519
520 /**
521 * Parse a KEY [=] VALUE line.
522 *
523 * @param[in] line the input line.
524 * @param[out] key the KEY.
525 * @param[out] value the VALUE.
526 * @param[in] equals character representing "equals" to separate KEY and
527 * VALUE, if NULL (the default) use first space character.
528 * @param[in] comment character to use as the comment character; if
529 * non-NULL, this character and everything after it is discarded; default
530 * is '#'.
531 * @exception std::bad_alloc if memory for the internal strings can't be
532 * allocated.
533 * @return whether a key was found.
534 *
535 * The \e comment character (default is '#') and everything after it are
536 * discarded and the result trimmed of leading and trailing white space.
537 * Use the \e equals delimiter character (or, if it is NULL -- the default,
538 * the first white space) to separate \e key and \e value. \e key and \e
539 * value are trimmed of leading and trailing white space. If \e key is
540 * empty, then \e value is set to "" and false is returned.
541 **********************************************************************/
542 static bool ParseLine(const std::string& line,
543 std::string& key, std::string& value,
544 char equals = '\0', char comment = '#');
545
546 /**
547 * Set the binary precision of a real number.
548 *
549 * @param[in] ndigits the number of bits of precision. If ndigits is 0
550 * (the default), then determine the precision from the environment
551 * variable GEOGRAPHICLIB_DIGITS. If this is undefined, use ndigits =
552 * 256 (i.e., about 77 decimal digits).
553 * @return the resulting number of bits of precision.
554 *
555 * This only has an effect when GEOGRAPHICLIB_PRECISION >= 5. The
556 * precision should only be set once and before calls to any other
557 * GeographicLib functions. (Several functions, for example Math::pi(),
558 * cache the return value in a static local variable. The precision needs
559 * to be set before a call to any such functions.) In multi-threaded
560 * applications, it is necessary also to set the precision in each thread
561 * (see the example GeoidToGTX.cpp). If GEOGRAPHICLIB_PRECISION > 5, then
562 * the precision is set to GEOGRAPHICLIB_PRECISION, the compile-time value,
563 * and \e ndigits is ignored.
564 *
565 * \note Use Math::digits() to return the current precision in bits.
566 **********************************************************************/
567 static int set_digits(int ndigits = 0);
568
569 };
570
571 /**
572 * The specialization of Utility::val<T>() for strings.
573 *
574 * @param[in] s the string to be converted.
575 * @exception GeographicErr is \e s is not readable as a T.
576 * @return the string trimmed of its whitespace.
577 **********************************************************************/
578 template<> inline std::string Utility::val<std::string>(const std::string& s)
579 { return trim(s); }
580
581 /**
582 * The specialization of Utility::val<T>() for bools.
583 *
584 * @param[in] s the string to be converted.
585 * @exception GeographicErr is \e s is not readable as a T.
586 * @return boolean value.
587 *
588 * \e s should either be string a representing 0 (false)
589 * or 1 (true) or one of the strings
590 * - "false", "f", "nil", "no", "n", "off", or "" meaning false,
591 * - "true", "t", "yes", "y", or "on" meaning true;
592 * .
593 * case is ignored.
594 **********************************************************************/
595 template<> inline bool Utility::val<bool>(const std::string& s) {
596 std::string t(trim(s));
597 if (t.empty()) return false;
598 bool x;
599 {
600 std::istringstream is(t);
601 if (is >> x) {
602 int pos = int(is.tellg()); // Returns -1 at end of string?
603 if (!(pos < 0 || pos == int(t.size())))
604 throw GeographicErr("Extra text " + t.substr(pos) +
605 " at end of " + t);
606 return x;
607 }
608 }
609 for (std::string::iterator p = t.begin(); p != t.end(); ++p)
610 *p = char(std::tolower(*p));
611 switch (t[0]) { // already checked that t isn't empty
612 case 'f':
613 if (t == "f" || t == "false") return false;
614 break;
615 case 'n':
616 if (t == "n" || t == "nil" || t == "no") return false;
617 break;
618 case 'o':
619 if (t == "off") return false;
620 else if (t == "on") return true;
621 break;
622 case 't':
623 if (t == "t" || t == "true") return true;
624 break;
625 case 'y':
626 if (t == "y" || t == "yes") return true;
627 break;
628 default:
629 break;
630 }
631 throw GeographicErr("Cannot decode " + t + " as a bool");
632 }
633
634 /**
635 * Convert a Math::real object to a string.
636 *
637 * @param[in] x the value to be converted.
638 * @param[in] p the precision used (default &minus;1).
639 * @exception std::bad_alloc if memory for the string can't be allocated.
640 * @return the string representation.
641 *
642 * If \e p &ge; 0, then the number fixed format is used with p bits of
643 * precision. With p < 0, there is no manipulation of the format. This is
644 * an overload of str<T> which deals with inf and nan.
645 **********************************************************************/
646 template<> inline std::string Utility::str<Math::real>(Math::real x, int p) {
647 using std::isfinite;
648 if (!isfinite(x))
649 return x < 0 ? std::string("-inf") :
650 (x > 0 ? std::string("inf") : std::string("nan"));
651 std::ostringstream s;
652 if (p >= 0) s << std::fixed << std::setprecision(p);
653 s << x; return s.str();
654 }
655
656} // namespace GeographicLib
657
658#if defined(_MSC_VER)
659# pragma warning (pop)
660#endif
661
662#endif // GEOGRAPHICLIB_UTILITY_HPP
Header for GeographicLib::Constants class.
#define GEOGRAPHICLIB_EXPORT
Definition Constants.hpp:67
Exception handling for GeographicLib.
Some utility routines for GeographicLib.
Definition Utility.hpp:35
static void readarray(std::istream &str, std::vector< IntT > &array)
Definition Utility.hpp:450
static void writearray(std::ostream &str, std::vector< IntT > &array)
Definition Utility.hpp:515
static T fractionalyear(const std::string &s)
Definition Utility.hpp:135
static void readarray(std::istream &str, IntT array[], size_t num)
Definition Utility.hpp:333
static void writearray(std::ostream &str, const IntT array[], size_t num)
Definition Utility.hpp:468
static int dow(int y, int m, int d)
Definition Utility.hpp:108
static int dow(int s)
Definition Utility.hpp:117
static T fract(const std::string &s)
Definition Utility.hpp:298
static T val(const std::string &s)
Definition Utility.hpp:225
static T nummatch(const std::string &s)
Definition Utility.hpp:260
static std::string trim(const std::string &s)
Definition Utility.cpp:149
static std::string str(T x, int p=-1)
Definition Utility.hpp:161
Namespace for GeographicLib.