Rev 7076 | Rev 7648 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 7076 | Rev 7111 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | /* |
1 | /* |
2 | - | ||
3 | ********************************************************************************* |
2 | ********************************************************************************* |
4 | * J.M. Evers 3/2012 * |
3 | * J.M. Evers 3/2012 * |
5 | * This is all amateur scriblings.. |
4 | * This is all -very- amateur scriblings... So no copyrights. * |
6 | * This source code file, and compiled objects derived from it, * |
5 | * This source code file, and compiled objects derived from it, * |
7 | * can be used and distributed without restriction, including for commercial use * |
6 | * can be used and distributed without restriction, including for commercial use * |
8 | * No warrenty whatsoever * |
7 | * No warrenty whatsoever * |
9 | ********************************************************************************* |
8 | ********************************************************************************* |
- | 9 | ||
10 |
|
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... |
|
11 |
|
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 |
|
- | 18 | ||
- | 19 | 28/9/2013 |
|
12 |
|
20 | minor issue: |
13 |
|
21 | small correction in roundoff routine when significance > 6 .... pow(10,7) may give problems when stored in (int) integer |
14 | 22 | ||
15 |
|
23 | 27/9/2013 |
16 |
|
24 | Correct rounding in stead of truncation... |
17 | 12345*10^12 --> 12345E12 ---> 1.2345*10^16 |
- | |
18 | 25 | ||
19 | 18/10/2012 : |
26 | 18/10/2012 : |
20 | Added Mathml output |
27 | Added Mathml output |
21 | Added option significance=-1 |
28 | Added option significance=-1 |
22 | To be used when there is no significance known ; just tries to print the number in science notation |
29 | To be used when there is no significance known ; just tries to print the number in science notation |
23 | Using the original amount of digits used in "number" argument |
30 | Using the original amount of digits used in "number" argument |
24 | !exec scienceprint 123.445000e+23,-1 --> 1.23445000*10^25 |
31 | !exec scienceprint 123.445000e+23,-1 --> 1.23445000*10^25 |
25 | 32 | ||
26 |
|
33 | 12/11/2012 |
27 |
|
34 | Added support for numbers like 12345*10^12 |
- | 35 | 12345*10^12 --> 12345E12 ---> 1.2345*10^16 |
|
- | 36 | ||
28 |
|
37 | 20/6/2012 |
- | 38 | Corrected significance flaw when using prefixes |
|
29 |
|
39 | Simplified routines |
30 |
|
40 | Added type = 5 : prefix-notation with words (nano,mega,giga...etc) |
- | 41 | ||
- | 42 | ||
- | 43 | ||
- | 44 | ||
31 | 45 | ||
32 | ********************************************************************************* |
46 | ********************************************************************************* |
33 | 47 | ||
34 | WIMS usage: |
48 | WIMS usage: |
35 | sci_num = !exec scienceprint number,significance,type |
49 | sci_num = !exec scienceprint number,significance,type |
Line 115... | Line 129... | ||
115 | char *printscience(double value, int sig, int format , int cnt ,int size){ |
129 | char *printscience(double value, int sig, int format , int cnt ,int size){ |
116 | static char *min[] = {"","m",MICRO,"n","p","f","a","z","y"}; |
130 | static char *min[] = {"","m",MICRO,"n","p","f","a","z","y"}; |
117 | static char *plus[] = {"","k", "M", "G", "T", "P", "E", "Z", "Y" }; |
131 | static char *plus[] = {"","k", "M", "G", "T", "P", "E", "Z", "Y" }; |
118 | static char *min_word[] = {"","milli","micro","nano","pico","femto","atto","zepto","yocto"}; |
132 | static char *min_word[] = {"","milli","micro","nano","pico","femto","atto","zepto","yocto"}; |
119 | static char *plus_word[] = {"","kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta" }; |
133 | static char *plus_word[] = {"","kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta" }; |
120 | char *sign = NULL;char *prefix = NULL; |
134 | char *sign = NULL;char *prefix = NULL;float pm;double factor; |
121 | int exponent10 = 0; |
135 | int exponent10 = 0; |
122 | int use_word = 0;if(format == 5){format = 3; use_word = 1;} /* switch to using words in stead of prefix */ |
136 | int use_word = 0;if(format == 5){format = 3; use_word = 1;} /* switch to using words in stead of prefix */ |
123 | if(value < 0.0) {pm = -0.5; sign = "-";value = -value;} else {sign = ""; pm = 0.5;} |
137 | if(value < 0.0) {pm = -0.5; sign = "-";value = -value;} else {sign = ""; pm = 0.5;} |
124 | if( sig == -1 ){ |
138 | if( sig == -1 ){ |
125 | /* |
139 | /* |
Line 145... | Line 159... | ||
145 | exponent10--; |
159 | exponent10--; |
146 | /* need to set a limit to number of while loops ! */ |
160 | /* need to set a limit to number of while loops ! */ |
147 | if(exponent10 <-100){fprintf(stdout,"error : number too small (exponent < -100)\n");return 0;} |
161 | if(exponent10 <-100){fprintf(stdout,"error : number too small (exponent < -100)\n");return 0;} |
148 | } |
162 | } |
149 | } |
163 | } |
150 | /* 27/9/2013 avoid truncating and do rounding... |
164 | /* 27/9/2013 avoid truncating and do rounding...very expensive */ |
151 |
|
165 | factor = pow(10,sig+1); |
152 | value = (round(factor*value + (pm) ))/factor; /* pm = +/- 0.5 */ |
166 | value = (round(factor*value + (pm) ))/factor; /* pm = +/- 0.5 */ |
153 | if(format == 3 && ((exponent10 < PREFIX_START) || (exponent10 > PREFIX_END))){ |
167 | if(format == 3 && ((exponent10 < PREFIX_START) || (exponent10 > PREFIX_END))){ |
154 | format = 1; /* not in my list of prefixes ; print in html ! */ |
168 | format = 1; /* not in my list of prefixes ; print in html ! */ |
155 | } |
169 | } |
156 | sig = sig - 1; /* "%.*f" counts the "." */ |
170 | sig = sig - 1; /* "%.*f" counts the "." */ |