Subversion Repositories wimsdev

Rev

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