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