vnl_matlab_print_scalar.cxx
Go to the documentation of this file.
1 // This is core/vnl/vnl_matlab_print_scalar.cxx
2 
3 #include <cstdio>
4 #include <cstdlib>
5 #include <cstring>
6 #include <complex>
8 
10  char *buf,
12 {
13  std::sprintf(buf, "%4d ", v);
14 }
15 
16 void vnl_matlab_print_scalar(unsigned v,
17  char *buf,
19 {
20  std::sprintf(buf, "%4u ", v);
21 }
22 
24  char *buf,
26 {
27  if (format == vnl_matlab_print_format_default)
28  format = vnl_matlab_print_format_top();
29  switch (format) {
31  if (v == 0.0)
32  std::sprintf(buf, "%8d ", 0);
33  else
34  std::sprintf(buf, "%8.5f ", v);
35  break;
37  if (v == 0.0)
38  std::sprintf(buf, "%6d ", 0);
39  else
40  std::sprintf(buf, "%6.3f ", v);
41  break;
43  std::sprintf(buf, "%11.7e ", v);
44  break;
46  std::sprintf(buf, "%8.4e ", v);
47  break;
48  default:/*vnl_matlab_print_format_default:*/ std::abort();
49  }
50 }
51 
53  char *buf,
55 {
56  if (format == vnl_matlab_print_format_default)
57  format = vnl_matlab_print_format_top();
58  switch (format) {
60  if (v == 0.0)
61  std::sprintf(buf, "%16d ", 0);
62  else
63  std::sprintf(buf, "%16.13f ", v);
64  break;
66  if (v == 0.0)
67  std::sprintf(buf, "%8d ", 0);
68  else
69  std::sprintf(buf, "%8.4f ", v);
70  break;
72  std::sprintf(buf, "%20.14e ", v);
73  break;
75  std::sprintf(buf, "%10.4e ", v);
76  break;
77  default:/*vnl_matlab_print_format_default:*/
78  std::abort();
79  }
80 }
81 
82 void vnl_matlab_print_scalar(long double v,
83  char *buf,
85 {
86  vnl_matlab_print_scalar(double(v), buf, format); // FIXME
87 }
88 
89 void vnl_matlab_print_scalar(std::complex<double> v,
90  char *buf,
92 {
93  if (format == vnl_matlab_print_format_default)
94  format = vnl_matlab_print_format_top();
95  int width = 16;
96  int precision = 12;
97  char conv = 'f';
98 
99  switch (format) {
102  width = 16;
103  precision = 12;
104  break;
107  width = 8;
108  precision = 4;
109  break;
110  default:/*vnl_matlab_print_format_default:*/ std::abort();
111  }
112 
113  switch (format) {
116  conv = 'f';
117  break;
120  conv = 'e';
121  break;
122  default:/*vnl_matlab_print_format_default:*/ std::abort();
123  }
124 
125  double r = std::real(v);
126  double i = std::imag(v);
127 
128  char fmt[1024];
129  // Real part
130  if (r == 0) {
131  std::sprintf(fmt, "%%" "%d" "d ", width);
132  std::sprintf(buf, fmt, 0);
133 
134  } else {
135  std::sprintf(fmt, "%%" "%d" "." "%d" "%c ", width, precision, conv);
136  std::sprintf(buf, fmt, r);
137  }
138 
139  buf += std::strlen(buf);
140 
141  // Imaginary part. Width is reduced as sign is taken care of separately
142  if (i == 0) {
143  std::sprintf(fmt, " %%" "%d" "s ", width-1);
144  std::sprintf(buf, fmt, "");
145  } else {
146  char sign = '+';
147  if (i < 0) {
148  sign = '-';
149  i = -i;
150  }
151  std::sprintf(fmt, "%c%%" "%d.%d%ci ", sign, width-1, precision, conv);
152  std::sprintf(buf, fmt, i);
153  }
154 }
155 
156 void vnl_matlab_print_scalar(std::complex<float> v,
157  char *buf,
159 {
160  if (format == vnl_matlab_print_format_default)
161  format = vnl_matlab_print_format_top();
162  int width = 10;
163  int precision = 6;
164  char conv = 'f';
165 
166  switch (format) {
169  width = 10;
170  precision = 6;
171  break;
174  width = 8;
175  precision = 4;
176  break;
177  default:/*vnl_matlab_print_format_default:*/ std::abort();
178  }
179 
180  switch (format) {
183  conv = 'f';
184  break;
187  conv = 'e';
188  break;
189  default:/*vnl_matlab_print_format_default:*/ std::abort();
190  }
191 
192  float r = std::real(v);
193  float i = std::imag(v);
194 
195  char fmt[1024];
196  // Real part
197  if (r == 0) {
198  std::sprintf(fmt, "%%" "%d" "d ", width);
199  std::sprintf(buf, fmt, 0);
200 
201  } else {
202  std::sprintf(fmt, "%%" "%d" "." "%d" "%c ", width, precision, conv);
203  std::sprintf(buf, fmt, r);
204  }
205 
206  buf += std::strlen(buf);
207 
208  // Imaginary part. Width is reduced as sign is taken care of separately
209  if (i == 0) {
210  std::sprintf(fmt, " %%" "%d" "s ", width-1);
211  std::sprintf(buf, fmt, "");
212  } else {
213  char sign = '+';
214  if (i < 0) {
215  sign = '-';
216  i = -i;
217  }
218  std::sprintf(fmt, "%c%%" "%d.%d%ci ", sign, width-1, precision, conv);
219  std::sprintf(buf, fmt, i);
220  }
221 }
222 
223 void vnl_matlab_print_scalar(std::complex<long double> v,
224  char *buf,
226 {
227  vnl_matlab_print_scalar(std::complex<double>(std::real(v), std::imag(v)), buf, format); // FIXME
228 }
229 
230 
231 template <class T>
232 std::ostream &vnl_matlab_print_scalar(std::ostream &s,
233  T value,
235 {
236  char buf[1024];
237  vnl_matlab_print_scalar(value, buf, format);
238  return s << buf;
239 }
240 
241 #define inst(T) template std::ostream &vnl_matlab_print_scalar(std::ostream &, T, vnl_matlab_print_format)
242 inst(int);
243 inst(float);
244 inst(double);
245 inst(long double);
246 inst(std::complex<float>);
247 inst(std::complex<double>);
248 inst(std::complex<long double>);
#define v
Definition: vnl_vector.h:42
#define inst(T)
void vnl_matlab_print_scalar(int v, char *buf, vnl_matlab_print_format)
vnl_matlab_print_format
pretty-printing matlab formats.
vnl_matlab_print_format vnl_matlab_print_format_top()
get top of stack :.