Rev 7648 | Rev 15712 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 7648 | Rev 12258 | ||
---|---|---|---|
Line 18... | Line 18... | ||
18 | type = 0 : calc notation : 120000,3,0 -> 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> |
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} |
20 | type = 2 : latex notation : 120000,3,2 -> 1.20 \times 10^{5} |
21 | type = 3 : prefix-notation : 120000,3,3 -> 120.0 k |
21 | type = 3 : prefix-notation : 120000,3,3 -> 120.0 k |
22 | type = 3 : if -24 > prefix > 24 -> type = 1 (html) |
22 | type = 3 : if -24 > prefix > 24 -> type = 1 (html) |
23 | type = 4 : mathml notation |
23 | type = 4 : mathml notation |
24 | type = 5 : prefix-notation with words (nano,mega,giga...etc) :120000,3,3 -> 120.0 kilo |
24 | type = 5 : prefix-notation with words (nano,mega,giga...etc) :120000,3,3 -> 120.0 kilo |
25 | 25 | ||
26 | multiple conversion: use space between arguments |
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 |
27 | scienceprint 120000,4 122900,5 120036,6,3 --> 120.0*10^3,122.90*10^3,120.036 k |
28 | 28 | ||
29 | 29 | ||
30 | ********************************************************************************* |
30 | ********************************************************************************* |
31 | 16/10/2013 |
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... |
32 | corrected roundoff in case "significance=-1" changed 'factor' from 'int' to 'float'...there must be a better way to do this... |
33 | But in numbers > 1000000000000000 there is still a problem, so take care (!) |
33 | But in numbers > 1000000000000000 there is still a problem, so take care (!) |
34 | when there are more than 15 digits in the number (this may vary depending on system / implementations, I guess) |
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 |
35 | ./scienceprint 901234567890123 ,-1,0 --> 9.01234567890123*10^14 |
36 | ./scienceprint 9012345678901234 ,-1,0 --> 9.012345678901236*10^15 |
36 | ./scienceprint 9012345678901234 ,-1,0 --> 9.012345678901236*10^15 |
37 | ./scienceprint 901234567890123*10^70 ,-1,0 --> 9.01234567890123*10^84 |
37 | ./scienceprint 901234567890123*10^70 ,-1,0 --> 9.01234567890123*10^84 |
38 | ./scienceprint 9012345678901234*10^70,-1,0 --> 9.012345678901227*10^85 |
38 | ./scienceprint 9012345678901234*10^70,-1,0 --> 9.012345678901227*10^85 |
39 | ********************************************************************************* |
39 | ********************************************************************************* |
40 | 28/9/2013 |
40 | 28/9/2013 |
41 | minor issue: |
41 | minor issue: |
42 | Added type = 5 : prefix-notation with words (nano,mega,giga...etc) |
42 | Added type = 5 : prefix-notation with words (nano,mega,giga...etc) |
43 | small correction in roundoff routine when significance > 6 .... pow(10,7) may give problems when stored in (int) integer |
43 | small correction in roundoff routine when significance > 6 .... pow(10,7) may give problems when stored in (int) integer |
44 | ********************************************************************************* |
44 | ********************************************************************************* |
45 | 27/9/2013 |
45 | 27/9/2013 |
46 | Correct rounding in stead of truncation... |
46 | Correct rounding in stead of truncation... |
47 | ********************************************************************************* |
47 | ********************************************************************************* |
48 | 18/10/2012 : |
48 | 18/10/2012 : |
49 | Added Mathml output |
49 | Added Mathml output |
50 | Added option significance=-1 |
50 | Added option significance=-1 |
51 | To be used when there is no significance known ; just tries to print the number in science notation |
51 | To be used when there is no significance known ; just tries to print the number in science notation |
52 | Using the original amount of digits used in "number" argument |
52 | Using the original amount of digits used in "number" argument |
53 | !exec scienceprint 123.445000e+23,-1 --> 1.23445000*10^25 |
53 | !exec scienceprint 123.445000e+23,-1 --> 1.23445000*10^25 |
54 | ********************************************************************************* |
54 | ********************************************************************************* |
55 | 12/11/2012 |
55 | 12/11/2012 |
56 | Added support for numbers like 12345*10^12 |
56 | Added support for numbers like 12345*10^12 |
57 | 12345*10^12 --> 12345E12 ---> 1.2345*10^16 |
57 | 12345*10^12 --> 12345E12 ---> 1.2345*10^16 |
58 | ********************************************************************************* |
58 | ********************************************************************************* |
59 | 20/6/2012 |
59 | 20/6/2012 |
60 | Corrected significance flaw when using prefixes |
60 | Corrected significance flaw when using prefixes |
61 | Simplified routines |
61 | Simplified routines |
62 | 24 yotta Y |
62 | 24 yotta Y |
63 | 21 zetta Z |
63 | 21 zetta Z |
64 | 18 exa E |
64 | 18 exa E |
65 | 15 peta P |
65 | 15 peta P |
66 | 12 tera T |
66 | 12 tera T |
67 | 9 giga G |
67 | 9 giga G |
68 | 6 mega M |
68 | 6 mega M |
69 | 3 kilo k |
69 | 3 kilo k |
70 | 2 hecto h |
70 | 2 hecto h |
71 | 1 deca da |
71 | 1 deca da |
72 | -1 deci d |
72 | -1 deci d |
73 | -2 centi c |
73 | -2 centi c |
74 | -3 milli m |
74 | -3 milli m |
75 | -6 micro µ |
75 | -6 micro µ |
76 | -9 nano n |
76 | -9 nano n |
77 | -12 pico p |
77 | -12 pico p |
78 | -15 femto f |
78 | -15 femto f |
Line 82... | Line 82... | ||
82 | */ |
82 | */ |
83 | #include <stdio.h> |
83 | #include <stdio.h> |
84 | #include <math.h> |
84 | #include <math.h> |
85 | #include <string.h> |
85 | #include <string.h> |
86 | #include <stdlib.h> |
86 | #include <stdlib.h> |
87 |
|
87 | /*#define MICRO "µ"*/ |
- | 88 | #define MICRO "\xB5" |
|
88 | #define MAX_CONV 256 |
89 | #define MAX_CONV 256 |
89 | #define MAX_STRING 32 |
90 | #define MAX_STRING 32 |
90 | #define PREFIX_START -24 |
91 | #define PREFIX_START -24 |
91 | #define PREFIX_END 24 |
92 | #define PREFIX_END 24 |
92 | 93 | ||
Line 124... | Line 125... | ||
124 | static char *min_word[] = {"","milli","micro","nano","pico","femto","atto","zepto","yocto"}; |
125 | static char *min_word[] = {"","milli","micro","nano","pico","femto","atto","zepto","yocto"}; |
125 | static char *plus_word[] = {"","kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta" }; |
126 | static char *plus_word[] = {"","kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta" }; |
126 | char *sign = NULL;char *prefix = NULL;float pm;double factor; |
127 | char *sign = NULL;char *prefix = NULL;float pm;double factor; |
127 | int exponent10 = 0; |
128 | int exponent10 = 0; |
128 | int use_word = 0;if(format == 5){format = 3; use_word = 1;} /* switch to using words in stead of prefix */ |
129 | int use_word = 0;if(format == 5){format = 3; use_word = 1;} /* switch to using words in stead of prefix */ |
129 | if(value < 0.0) {pm = -0.5; sign = "-";value = -value;} else {sign = ""; pm = 0.5;} |
130 | if(value < 0.0) {pm = -0.5; sign = "-";value = -value;} else {sign = ""; pm = 0.5;} |
130 | if( sig == -1 ){ |
131 | if( sig == -1 ){ |
131 | /* |
132 | /* |
132 | no significance truncations...just science notation 1234 -> 1.234*10^3 |
133 | no significance truncations...just science notation 1234 -> 1.234*10^3 |
133 | try (!) to use same amount of digits |
134 | try (!) to use same amount of digits |
134 | */ |
135 | */ |
135 | sig = size; |
136 | sig = size; |
136 | if(format == 3){format = 1;} /* never prefix --> html notation */ |
137 | if(format == 3){format = 1;} /* never prefix --> html notation */ |
137 | } |
138 | } |
Line 179... | Line 180... | ||
179 | 1000000,1,3 -> 1 M |
180 | 1000000,1,3 -> 1 M |
180 | 10000000,1,3 -> 1*10^1 M |
181 | 10000000,1,3 -> 1*10^1 M |
181 | 100000000,1,3 -> 1*10^2 M |
182 | 100000000,1,3 -> 1*10^2 M |
182 | 1000000000,1,3 -> 1 G |
183 | 1000000000,1,3 -> 1 G |
183 | 1,1,3 -> 1 |
184 | 1,1,3 -> 1 |
184 | 0.1,1,3 -> 1*10^-1 |
185 | 0.1,1,3 -> 1*10^-1 |
185 | 0.01,1,3 -> 1*10^-2 |
186 | 0.01,1,3 -> 1*10^-2 |
186 | 0.001,1,3 -> 1 m |
187 | 0.001,1,3 -> 1 m |
187 | 0.0001,1,3 -> 1*10^-1 m |
188 | 0.0001,1,3 -> 1*10^-1 m |
188 | 0.00001,1,3 -> 1*10^-2 m |
189 | 0.00001,1,3 -> 1*10^-2 m |
189 | 0.000001,1,3 -> 1 µ |
190 | 0.000001,1,3 -> 1 µ |
190 | 0.0000001,1,3-> 1*10^-1 µ |
191 | 0.0000001,1,3-> 1*10^-1 µ |
Line 213... | Line 214... | ||
213 | case 6: fprintf(stdout, "%s%.*f",sign,sig,value);break; |
214 | case 6: fprintf(stdout, "%s%.*f",sign,sig,value);break; |
214 | default: break; |
215 | default: break; |
215 | } |
216 | } |
216 | return NULL; |
217 | return NULL; |
217 | } |
218 | } |
218 | 219 | ||
219 | int main( int argc , char *argv[]){ |
220 | int main( int argc , char *argv[]){ |
220 | 221 | ||
221 | if( argc < 2){ |
222 | if( argc < 2){ |
222 | fprintf(stdout,"syntax error : number1,significance1,type1 number2,significance2,type2 ... number_n,significance_n,type_n \n"); |
223 | fprintf(stdout,"syntax error : number1,significance1,type1 number2,significance2,type2 ... number_n,significance_n,type_n \n"); |
223 | return 0; |
224 | return 0; |
Line 248... | Line 249... | ||
248 | idx = 0; |
249 | idx = 0; |
249 | type = 0; |
250 | type = 0; |
250 | size = 0; |
251 | size = 0; |
251 | while( ptr != NULL ){ |
252 | while( ptr != NULL ){ |
252 | switch( idx ){ |
253 | switch( idx ){ |
253 | case 0: |
254 | case 0: |
254 | /* size only interesting when 'significance=-1' |
255 | /* size only interesting when 'significance=-1' |
255 | determine number of digits : 1.23445e+23 -> size = 6 |
256 | determine number of digits : 1.23445e+23 -> size = 6 |
256 | */ |
257 | */ |
257 | size = strlen(ptr); |
258 | size = strlen(ptr); |
258 | if( strstr(ptr,".") != NULL){size = size - 1 ;} |
259 | if( strstr(ptr,".") != NULL){size = size - 1 ;} |
259 | if( strstr(ptr,"*10^") != NULL){ |
260 | if( strstr(ptr,"*10^") != NULL){ |
260 | ptr = str_replace(ptr,"*10^","E"); |
261 | ptr = str_replace(ptr,"*10^","E"); |
261 | if(ptr == NULL){ |
262 | if(ptr == NULL){ |
262 | fprintf(stdout,"error : in replacement of 10^ notation\n"); |
263 | fprintf(stdout,"error : in replacement of 10^ notation\n"); |
263 | return 0; |
264 | return 0; |
264 | } |
265 | } |
265 | size = size - 3; |
266 | size = size - 3; |
266 | } |
267 | } |
267 | if( strstr(ptr,"E") != NULL){size = size - strlen(strstr(ptr,"E"));} |
268 | if( strstr(ptr,"E") != NULL){size = size - strlen(strstr(ptr,"E"));} |
268 | if( strstr(ptr,"e") != NULL){size = size - strlen(strstr(ptr,"e"));} |
269 | if( strstr(ptr,"e") != NULL){size = size - strlen(strstr(ptr,"e"));} |
269 | number = atof(ptr); |
270 | number = atof(ptr); |
270 | break; |
271 | break; |
271 | case 1: significance = atoi(ptr); break; |
272 | case 1: significance = atoi(ptr); break; |
272 | case 2: type = atoi(ptr); if(type < 0 || type > 5 ){type = 0;} break; |
273 | case 2: type = atoi(ptr); if(type < 0 || type > 5 ){type = 0;} break; |
273 | default: break; |
274 | default: break; |
274 | } |
275 | } |