Rev 5950 | Rev 7037 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
5171 | schaersvoo | 1 | /* |
5177 | schaersvoo | 2 | |
5171 | schaersvoo | 3 | ********************************************************************************* |
5177 | schaersvoo | 4 | * J.M. Evers 3/2012 * |
5 | * This is all amateur scriblings... So no copyrights. * |
||
6 | * This source code file, and compiled objects derived from it, * |
||
7 | * can be used and distributed without restriction, including for commercial use * |
||
5196 | schaersvoo | 8 | * No warrenty whatsoever * |
5171 | schaersvoo | 9 | ********************************************************************************* |
6738 | schaersvoo | 10 | 20/6/2012 |
11 | Corrected significance flaw when using prefixes |
||
12 | Simplified routines |
||
13 | Added type = 5 : prefix-notation with words (nano,mega,giga...etc) |
||
5196 | schaersvoo | 14 | |
5950 | schaersvoo | 15 | 12/11/2012 |
16 | Added support for numbers like 12345*10^12 |
||
17 | 12345*10^12 --> 12345E12 ---> 1.2345*10^16 |
||
18 | |||
5801 | schaersvoo | 19 | 18/10/2012 : |
20 | Added Mathml output |
||
21 | Added option significance=-1 |
||
22 | To be used when there is no significance known ; just tries to print the number in science notation |
||
5840 | schaersvoo | 23 | Using the original amount of digits used in "number" argument |
24 | !exec scienceprint 123.445000e+23,-1 --> 1.23445000*10^25 |
||
5801 | schaersvoo | 25 | |
26 | ********************************************************************************* |
||
27 | |||
5177 | schaersvoo | 28 | WIMS usage: |
29 | sci_num = !exec scienceprint number,significance,type |
||
5196 | schaersvoo | 30 | |
5171 | schaersvoo | 31 | number: a number |
5196 | schaersvoo | 32 | significance : desired precision |
5801 | schaersvoo | 33 | type (optional args): calc = 0 / html = 1 / latex = 2 / prefix = 3 / mathml = 4 |
5177 | schaersvoo | 34 | |
5196 | schaersvoo | 35 | default : calc notation : 120000,3 -> 1.20*10^5 |
36 | type = 0 : calc notation : 120000,3,0 -> 1.20*10^5 |
||
37 | type = 1 : html notation : 120000,3,1 -> 1.20×10<sup>5</sup> |
||
38 | type = 2 : latex notation : 120000,3,2 -> 1.20 \times 10^{5} |
||
5171 | schaersvoo | 39 | type = 3 : prefix-notation : 120000,3,3 -> 120.0 k |
5196 | schaersvoo | 40 | type = 3 : if -24 > prefix > 24 -> type = 1 (html) |
5801 | schaersvoo | 41 | type = 4 : mathml notation |
5171 | schaersvoo | 42 | |
43 | multiple conversion: use space between arguments |
||
5177 | schaersvoo | 44 | scienceprint 120000,4 122900,5 120036,6,3 --> 120.0*10^3,122.90*10^3,120.036 k |
5171 | schaersvoo | 45 | |
5196 | schaersvoo | 46 | 24 yotta Y |
47 | 21 zetta Z |
||
48 | 18 exa E |
||
49 | 15 peta P |
||
50 | 12 tera T |
||
51 | 9 giga G |
||
52 | 6 mega M |
||
53 | 3 kilo k |
||
6738 | schaersvoo | 54 | |
5196 | schaersvoo | 55 | 2 hecto h |
56 | 1 deca da |
||
57 | -1 deci d |
||
58 | -2 centi c |
||
6738 | schaersvoo | 59 | |
5196 | schaersvoo | 60 | -3 milli m |
61 | -6 micro µ |
||
62 | -9 nano n |
||
63 | -12 pico p |
||
64 | -15 femto f |
||
65 | -18 atto a |
||
66 | -21 zepto z |
||
67 | -24 yocto y |
||
68 | |||
5171 | schaersvoo | 69 | */ |
70 | |||
71 | #include <stdio.h> |
||
72 | #include <math.h> |
||
73 | #include <string.h> |
||
74 | #include <stdlib.h> |
||
75 | #define MICRO "µ" |
||
5196 | schaersvoo | 76 | #define MAX_CONV 256 |
5950 | schaersvoo | 77 | #define MAX_STRING 32 |
6738 | schaersvoo | 78 | #define PREFIX_START -24 |
79 | #define PREFIX_END 24 |
||
5171 | schaersvoo | 80 | |
5950 | schaersvoo | 81 | char *str_replace ( const char *word, const char *sub_word, const char *rep_word ){ |
82 | if(strlen(word) > MAX_STRING){return NULL;} |
||
83 | char *part_word = NULL; |
||
84 | char *new_word = NULL; |
||
85 | char *old_word = NULL; |
||
86 | char *head = NULL; |
||
87 | /* if either sub_word or rep_word is NULL, duplicate word a let caller handle it */ |
||
88 | if ( sub_word == NULL || rep_word == NULL ) return strdup(word); |
||
89 | new_word = strdup (word); |
||
90 | head = new_word; |
||
91 | while ( (part_word = strstr ( head, sub_word ))){ |
||
92 | old_word = new_word; |
||
93 | new_word = malloc ( strlen ( old_word ) - strlen ( sub_word ) + strlen ( rep_word ) + 1 ); |
||
94 | /*failed to alloc mem, free old word and return NULL */ |
||
95 | if ( new_word == NULL ){ |
||
96 | free (old_word);return NULL; |
||
97 | } |
||
98 | memcpy ( new_word, old_word, part_word - old_word ); |
||
99 | memcpy ( new_word + (part_word - old_word), rep_word, strlen ( rep_word ) ); |
||
100 | memcpy ( new_word + (part_word - old_word) + strlen( rep_word ), part_word + strlen ( sub_word ), strlen ( old_word ) - strlen ( sub_word ) - ( part_word - old_word ) ); |
||
101 | memset ( new_word + strlen ( old_word ) - strlen ( sub_word ) + strlen ( rep_word ) , 0, 1 ); |
||
102 | /* move back head right after the last replacement */ |
||
103 | head = new_word + (part_word - old_word) + strlen( rep_word ); |
||
104 | free (old_word); |
||
105 | } |
||
106 | return new_word; |
||
107 | } |
||
108 | |||
5801 | schaersvoo | 109 | char *printscience(double value, int sig, int format , int cnt ,int size){ |
6738 | schaersvoo | 110 | static char *min[] = {"","m",MICRO,"n","p","f","a","z","y"}; |
111 | static char *plus[] = {"","k", "M", "G", "T", "P", "E", "Z", "Y" }; |
||
112 | static char *min_word[] = {"","milli","micro","nano","pico","femto","atto","zepto","yocto"}; |
||
113 | static char *plus_word[] = {"","kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta" }; |
||
114 | char *sign = NULL;char *prefix = NULL; |
||
115 | int exponent10 = 0; |
||
116 | int use_word = 0;if(format == 5){format = 3; use_word = 1;} /* switch to using words in stead of prefix */ |
||
117 | if(value < 0.0) {sign = "-";value = -value;sig--;} else {sign = "";} if( sig == -1 ){ |
||
118 | /* |
||
119 | no significance truncations...just science notation 1234 -> 1.234*10^3 |
||
120 | try (!) to use same amount of digits |
||
121 | */ |
||
5801 | schaersvoo | 122 | sig = size; |
6738 | schaersvoo | 123 | if(format == 3){format = 1;} /* never prefix --> html notation */ |
124 | } |
||
125 | if(value == 0){fprintf(stdout, "%s%.*f", sign, sig-1, value);return NULL;} /* no need to go further */ |
||
126 | if(value>1){ |
||
127 | while(value >= 10){ |
||
128 | value=value / 10.0; |
||
129 | exponent10++; |
||
130 | /* need to set a limit to number of while loops ! */ |
||
131 | if(exponent10 > 100){fprintf(stdout,"error : number too big (exponent > 100)\n");return 0;} |
||
5196 | schaersvoo | 132 | } |
5177 | schaersvoo | 133 | } |
6738 | schaersvoo | 134 | else /* 0 < value < 1 --> exponent10 < 0 */ |
5177 | schaersvoo | 135 | { |
6738 | schaersvoo | 136 | while(value < 1){ |
137 | value=value*10; |
||
138 | exponent10--; |
||
139 | /* need to set a limit to number of while loops ! */ |
||
140 | if(exponent10 <-100){fprintf(stdout,"error : number too small (exponent < -100)\n");return 0;} |
||
5171 | schaersvoo | 141 | } |
5177 | schaersvoo | 142 | } |
6738 | schaersvoo | 143 | if(format == 3 && ((exponent10 < PREFIX_START) || (exponent10 > PREFIX_END))){ |
144 | format = 1; /* not in my list of prefixes ; print in html ! */ |
||
5196 | schaersvoo | 145 | } |
6738 | schaersvoo | 146 | sig = sig - 1; /* "%.*f" counts the "." */ |
147 | if(cnt > 1){fprintf(stdout,",");}/* more than one conversion to do : make list */ |
||
148 | int idx=0;int exp=0; |
||
149 | if(exponent10 == 0){format = 6;} /* no need for 2*10^0 */ |
||
150 | if(sig < 0){sig = 0;} /* better be safe than sorry... */ |
||
151 | switch(format){ |
||
152 | case 0: fprintf(stdout, "%s%.*f*10^%d", sign, sig, value, exponent10);break; |
||
153 | case 1: fprintf(stdout, "%s%.*f×10<sup>%d</sup>", sign, sig, value, exponent10);break; |
||
154 | case 2: fprintf(stdout, "%s%.*f \\times 10^{%d}", sign, sig, value, exponent10);break; |
||
155 | case 3: |
||
156 | /* |
||
157 | 1,1,3 -> 1 |
||
158 | 10,1,3 -> 1*10^-2 k |
||
159 | 100,1,3 -> 1*10^-1 k |
||
160 | 1000,1,3 -> 1 k |
||
161 | 10000,1,3 -> 1*10^1 k |
||
162 | 100000,1,3 -> 1*10^2 k |
||
163 | 1000000,1,3 -> 1 M |
||
164 | 10000000,1,3 -> 1*10^1 M |
||
165 | 100000000,1,3 -> 1*10^2 M |
||
166 | 1000000000,1,3 -> 1 G |
||
167 | 1,1,3 -> 1 |
||
168 | 0.1,1,3 -> 1*10^-1 |
||
169 | 0.01,1,3 -> 1*10^-2 |
||
170 | 0.001,1,3 -> 1 m |
||
171 | 0.0001,1,3 -> 1*10^-1 m |
||
172 | 0.00001,1,3 -> 1*10^-2 m |
||
173 | 0.000001,1,3 -> 1 µ |
||
174 | 0.0000001,1,3-> 1*10^-1 µ |
||
175 | 0.00000001,1,3-> 1*10^-2 µ |
||
176 | 0.000000001,1,3-> 1 n |
||
177 | */ |
||
178 | exp = exponent10%3; |
||
179 | idx = round(exponent10/3); |
||
180 | if( exponent10 > 0 ){ |
||
181 | if(use_word == 0 ){ prefix = plus[idx]; } else { prefix = plus_word[idx]; } |
||
5196 | schaersvoo | 182 | } |
5206 | schaersvoo | 183 | else |
184 | { |
||
6738 | schaersvoo | 185 | if(use_word == 0){ prefix = min[-1*idx]; } else { prefix = min_word[-1*idx]; } |
5206 | schaersvoo | 186 | } |
6738 | schaersvoo | 187 | if( exp == 0){ |
188 | fprintf(stdout, "%s%.*f %s",sign, sig, value,prefix); |
||
5171 | schaersvoo | 189 | } |
190 | else |
||
191 | { |
||
6738 | schaersvoo | 192 | fprintf(stdout, "%s%.*f×10<sup>%d</sup> %s", sign, sig, value, exp, prefix); |
5171 | schaersvoo | 193 | } |
6738 | schaersvoo | 194 | break; |
195 | case 4: fprintf(stdout, "<math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"inline\"><mstyle id=\"wims_mathml\" mathsize=\"110%%\"><mn>%s%.*f</mn><mo>×</mo><msup><mn>10</mn><mn>%d</mn></msup></mstyle></math>", sign, sig, value, exponent10);break; |
||
196 | case 5: break; |
||
197 | case 6: fprintf(stdout, "%s%.*f",sign,sig,value);break; |
||
198 | default: break; |
||
5801 | schaersvoo | 199 | } |
5177 | schaersvoo | 200 | return NULL; |
201 | } |
||
202 | |||
203 | int main( int argc , char *argv[]){ |
||
5171 | schaersvoo | 204 | |
5177 | schaersvoo | 205 | if( argc < 2){ |
206 | fprintf(stdout,"syntax error : number1,significance1,type1 number2,significance2,type2 ... number_n,significance_n,type_n \n"); |
||
207 | return 0; |
||
208 | } |
||
5801 | schaersvoo | 209 | |
5171 | schaersvoo | 210 | double number = 0; |
5801 | schaersvoo | 211 | int significance = 0,type = 0,idx = 0,cnt = 1,size = 0; |
5171 | schaersvoo | 212 | char *input = "\0",*ptr = "\0"; |
213 | |||
214 | /* test for illegal characters */ |
||
5950 | schaersvoo | 215 | const char *invalid_characters = "\n\"\'!=ABCDFGHIJKLMNOPQRSTUVWXYZabcdfghijklmnopqrstuvwxyz@#$%&()[]{};:~><?/\\|"; |
5171 | schaersvoo | 216 | /* Ee +- are allowed : 12.34e+05 12.34e-08 */ |
217 | |||
218 | /* walk through argument 1 to end, and call function scienceprint(a,b,c) */ |
||
219 | input = argv[cnt]; |
||
220 | while( input != NULL ){ |
||
5196 | schaersvoo | 221 | if(cnt > MAX_CONV){fprintf(stdout,"\nerror: number of conversions exceeds limit of %d\n",MAX_CONV);return 0;} |
5801 | schaersvoo | 222 | while (*input){ /* loop through invalid chars. */ |
5171 | schaersvoo | 223 | if ( strchr(invalid_characters, *input) ){ |
5801 | schaersvoo | 224 | fprintf(stdout,"\nerror : illegal character \"%s\" \n",input); |
5196 | schaersvoo | 225 | return 0; |
5171 | schaersvoo | 226 | } |
227 | input++; |
||
228 | } |
||
5801 | schaersvoo | 229 | /* reset input to actual value */ |
5171 | schaersvoo | 230 | input = argv[cnt]; |
231 | ptr = (char *) strtok(input,","); |
||
232 | idx = 0; |
||
233 | type = 0; |
||
5801 | schaersvoo | 234 | size = 0; |
5171 | schaersvoo | 235 | while( ptr != NULL ){ |
236 | switch( idx ){ |
||
5950 | schaersvoo | 237 | case 0: |
5840 | schaersvoo | 238 | /* size only interesting when 'significance=-1' |
239 | determine number of digits : 1.23445e+23 -> size = 6 |
||
240 | */ |
||
5950 | schaersvoo | 241 | size = strlen(ptr); |
242 | if( strstr(ptr,".") != NULL){size = size - 1 ;} |
||
243 | if( strstr(ptr,"*10^") != NULL){ |
||
244 | ptr = str_replace(ptr,"*10^","E"); |
||
245 | if(ptr == NULL){ |
||
246 | fprintf(stdout,"error : in replacement of 10^ notation\n"); |
||
247 | return 0; |
||
248 | } |
||
249 | size = size - 3; |
||
250 | } |
||
5840 | schaersvoo | 251 | if( strstr(ptr,"E") != NULL){size = size - strlen(strstr(ptr,"E"));} |
252 | if( strstr(ptr,"e") != NULL){size = size - strlen(strstr(ptr,"e"));} |
||
5950 | schaersvoo | 253 | number = atof(ptr); |
5840 | schaersvoo | 254 | break; |
5801 | schaersvoo | 255 | case 1: significance = atoi(ptr); break; |
6738 | schaersvoo | 256 | case 2: type = atoi(ptr); if(type < 0 || type > 5 ){type = 0;} break; |
5171 | schaersvoo | 257 | default: break; |
258 | } |
||
259 | idx++; |
||
260 | ptr = (char *) strtok(NULL,","); |
||
261 | } |
||
5801 | schaersvoo | 262 | /* number and precision are mandatory: default type=0 */ |
5196 | schaersvoo | 263 | if( idx < 2 || idx > 3){fprintf(stdout,"\nsyntax error : number1,significance1,type1 number2,significance2,type2 ... number_n,significance_n,type_n \n");return 0;} |
5801 | schaersvoo | 264 | /* call conversion routine */ |
265 | printscience(number, significance, type , cnt , size); |
||
5171 | schaersvoo | 266 | cnt++; |
267 | input = argv[cnt]; |
||
268 | } |
||
269 | fprintf(stdout,"\n"); |
||
270 | return 0; |
||
271 | } |
||
5950 | schaersvoo | 272 | |
273 | |||
274 |