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