Subversion Repositories wimsdev

Rev

Rev 7038 | Rev 7111 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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