ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
str.cpp
Go to the documentation of this file.
1 
8 #include <fvar.hpp>
9 #include <string.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <math.h>
13 #ifdef __NDPX__
14  #include <dos.h> //itoa
15 #endif
16 #ifndef OPT_LIB
17  #include <cassert>
18  #include <climits>
19 #endif
20 
25 adstring str(double x, int minwidth, int decplaces)
26 {
27  int w = 0;
28  int d = 0;
29  char buffer[50];
30 
31  int maxwidth = 0;
32  if (fabs(x) > 0)
33  maxwidth = int(log10(fabs(x)));
34  else
35  maxwidth = minwidth;
36 
37  if (decplaces < 0) // decplaces not specified
38  {
39  if (minwidth < 8)
40  w = 8;
41  else
42  w = minwidth;
43 
44  if (x < 0)
45  w++;
46 
47  d = minwidth-7;
48  if (d>10)
49  d = 10;
50 
51  //tmp = new adstring(1,w-1);
52  //sprintf(tmp->s,"%*.*e",w,d,x);
53  sprintf(buffer,"%*.*e",w,d,x);
54  }
55  else // decplaces specified
56  {
57  if (decplaces > 11)
58  d = 11;
59  else
60  d = decplaces;
61 
62  w = maxwidth;
63  if (w < minwidth)
64  w = minwidth;
65 
66  w += d;//+3;
67  if (x < 0)
68  w++;
69  //tmp = new adstring(1,w-1);
70 
71  sprintf(buffer,"%*.*f",w,d,x);
72 /*
73  if (d>=0)
74  {
75  //sprintf(tmp->s,"%*.*f",w,d,x);
76  sprintf(buffer,"%*.*f",w,d,x);
77  }
78  else
79  {
80  //sprintf(tmp->s,"%*.f",w,x);
81  sprintf(buffer,"%*.f",w,x);
82  }
83 */
84  }
85  //return (*tmp);
86  adstring tmp(buffer);
87  return (tmp);
88 }
95 void str(const int a, adstring& s)
96 {
97 #if defined(_MSC_VER)
98  char buffer[50];
99  itoa(a, buffer, 10);
100  s = adstring(buffer);
101 #else
102  s = itoa(a,10);
103 #endif
104 }
110 adstring str(const int a)
111 {
112 #if defined(_MSC_VER)
113  char buffer[50];
114  itoa(a, buffer, 10);
115  //adstring* tmp = new adstring(1,strlen(buffer));
116  //*tmp = adstring(buffer);
117  adstring tmp((char*)buffer);
118 #else
119  adstring tmp = itoa(a,10);
120 #endif
121  return tmp;
122 }
123 
124 adstring chr(int c)
125 {
126 #ifndef OPT_LIB
127  assert(0 <= c && c <= CHAR_MAX);
128 #endif
129  return adstring((char)c);
130 }
dvector log10(const dvector &vec)
Returns dvector with the common (base-10) logarithm of vec.
Definition: dvect6.cpp:273
#define x
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
adstring itoa(int n, int d)
Definition: string2.cpp:89
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
adstring str(double x, int minwidth=17, int decplaces=-1)
Convert x to adstring with minimum width and total number of decimal places.
Definition: str.cpp:25
#define w
adstring chr(int c)
Definition: str.cpp:124