annotate utils.c @ 230:9e240adb3053

Fix N estimate to properly round
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 27 Oct 2014 18:02:32 +0100
parents be87c8e78266
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
144
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
1 #include <ansi_c.h>
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
2
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
3 const char * thousands(char *buffer, int size, char *fmt, double val)
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
4 {
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
5 /* compute how many separators we need */
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
6 #pragma DisableFunctionRuntimeChecking log10
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
7 int nsep = log10(fabs(val));
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
8 nsep = (nsep > 0 ? nsep / 3 : 0);
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
9 /* format value */
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
10 int len = snprintf(buffer, size, fmt, val);
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
11 char *src = buffer + len;
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
12 char *dst = src + nsep;
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
13 /* copy till decimal separator */
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
14 while (*src != '.')
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
15 *dst-- = *src--;
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
16 /* the next char is the decimal separator */
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
17 int n = -1;
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
18 /* copy till src and dst point to the same location */
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
19 while (src != dst) {
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
20 *dst-- = *src--;
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
21 /* insert separator */
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
22 if (isdigit(*src) && (++n) && ((n % 3) == 0))
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
23 *dst-- = ' ';
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
24 }
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
25 return buffer;
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
26 }
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
27
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
28
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
29 double Peta(double x)
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
30 {
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
31 return 1.0e15 * x;
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
32 }
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
33
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
34 double Tera(double x)
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
35 {
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
36 return 1.0e12 * x;
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
37 }
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
38
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
39 double Giga(double x)
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
40 {
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
41 return 1.0e9 * x;
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
42 }
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
43
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
44 double Mega(double x)
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
45 {
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
46 return 1.0e6 * x;
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
47 }
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
48
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
49 double kilo(double x)
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
50 {
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
51 return 1.0e3 * x;
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
52 }
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
53
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
54 double milli(double x)
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
55 {
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
56 return 1.0e-3 * x;
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
57 }
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
58
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
59 double micro(double x)
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
60 {
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
61 return 1.0e-6 * x;
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
62 }
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
63
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
64 double nano(double x)
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
65 {
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
66 return 1.0e-9 * x;
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
67 }
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
68
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
69 double pico(double x)
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
70 {
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
71 return 1.e-12 * x;
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
72 }
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
73
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
74 double femto(double x)
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
75 {
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
76 return 1.0e-15 * x;
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
77 }
be87c8e78266 Code cleanup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
78