Subversion Repositories wimsdev

Rev

Rev 12258 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2. *********************************************************************************
  3. * J.M. Evers 3/2012                                                             *
  4. * This is all -very- amateur scriblings... So no copyrights.                    *
  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                                                        *
  8. *********************************************************************************
  9.  
  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>
  20. type = 2 : tex 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)
  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)
  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. *********************************************************************************
  31. CHANGELOG
  32. 19/2/2021
  33. added compile time support for KaTeX installation (in case 'type = 4': MathML --> KaTeX span element)
  34.  
  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...
  37. But in numbers > 1000000000000000 there is still a problem, so take care (!)
  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
  43. *********************************************************************************
  44. 28/9/2013
  45. minor issue:
  46. Added type = 5 : prefix-notation with words (nano,mega,giga...etc)
  47. small correction in roundoff routine when significance > 6 .... pow(10,7) may give problems when stored in (int) integer
  48. *********************************************************************************
  49. 27/9/2013
  50. Correct rounding in stead of truncation...
  51. *********************************************************************************
  52. 18/10/2012 :
  53. Added Mathml output
  54. Added option significance=-1
  55. To be used when there is no significance known ; just tries to print the number in science notation
  56. Using the original amount of digits used in "number" argument
  57. !exec scienceprint 123.445000e+23,-1 --> 1.23445000*10^25
  58. *********************************************************************************
  59. 12/11/2012
  60. Added support for numbers like  12345*10^12
  61. 12345*10^12 --> 12345E12 ---> 1.2345*10^16
  62. *********************************************************************************
  63. 20/6/2012
  64. Corrected significance flaw when using prefixes
  65. Simplified routines
  66. 24  yotta       Y
  67. 21  zetta       Z
  68. 18  exa         E
  69. 15  peta        P
  70. 12  tera        T
  71. 9   giga        G
  72. 6   mega        M
  73. 3   kilo        k
  74. 2   hecto       h
  75. 1   deca        da
  76. -1  deci        d
  77. -2  centi       c
  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
  86. */
  87. #include <stdio.h>
  88. #include <math.h>
  89. #include <string.h>
  90. #include <stdlib.h>
  91. /*#define MICRO "µ"*/
  92. #define MICRO "\xB5"
  93. #define MAX_CONV 256
  94. #define MAX_STRING 32
  95. #define PREFIX_START -24
  96. #define PREFIX_END 24
  97.  
  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 ))){
  109.         old_word = new_word;
  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.  
  126. char *printscience(double value, int sig, int format , int cnt ,int size){
  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" };
  131.     char *sign = NULL;char *prefix = NULL;float pm;double factor;
  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  */
  134.     if(value < 0.0) {pm = -0.5; sign = "-";value = -value;} else {sign = ""; pm = 0.5;}
  135.     if( sig == -1 ){
  136.      /*
  137.      no significance truncations...just science notation 1234 -> 1.234*10^3
  138.      try (!) to use same amount of digits
  139.      */
  140.         sig = size;
  141.         if(format == 3){format = 1;} /* never prefix --> html notation */
  142.     }
  143.     if(value == 0){fprintf(stdout, "%s%.*f", sign, sig-1, value);return NULL;} /* no need to go further */
  144.     if(value>1){
  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.         }
  151.     }
  152.     else /* 0 < value < 1 --> exponent10 < 0 */
  153.     {
  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.         }
  160.     }
  161.     /* 27/9/2013 avoid truncating and do rounding...very expensive */
  162.     factor = pow(10,sig+1);
  163.     value = (round(factor*value + (pm) ))/factor; /* pm = +/- 0.5 */
  164.     if(format == 3 && ((exponent10 < PREFIX_START) || (exponent10 > PREFIX_END))){
  165.         format = 1; /* not in my list of prefixes ; print in html ! */
  166.     }
  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){
  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:
  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
  189. 0.1,1,3 -> 1*10^-1
  190. 0.01,1,3 -> 1*10^-2
  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. */
  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;
  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
  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;
  220. #endif
  221.         case 5: break;
  222.         case 6: fprintf(stdout, "%s%.*f",sign,sig,value);break;
  223.         default: break;
  224.     }
  225.     return NULL;
  226. }
  227.  
  228. int main( int argc , char *argv[]){
  229.  
  230.     if( argc < 2){
  231.         fprintf(stdout,"syntax error : number1,significance1,type1 number2,significance2,type2 ... number_n,significance_n,type_n \n");
  232.         return 0;
  233.     }
  234.  
  235.     double number = 0;
  236.     int significance = 0,type = 0,idx = 0,cnt = 1,size = 0;
  237.     char *input = "\0",*ptr = "\0";
  238.  
  239.     /* test for illegal characters */
  240.     const char *invalid_characters = "\n\"\'!=ABCDFGHIJKLMNOPQRSTUVWXYZabcdfghijklmnopqrstuvwxyz@#$%&()[]{};:~><?/\\|";
  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 ){
  246.         if(cnt > MAX_CONV){fprintf(stdout,"\nerror: number of conversions exceeds limit of %d\n",MAX_CONV);return 0;}
  247.         while (*input){ /* loop through invalid chars. */
  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 ){
  262.                 case 0:
  263.                         /* size only interesting when 'significance=-1'
  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;
  273.                             }
  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"));}
  278.                         number = atof(ptr);
  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];
  293.     }
  294.     fprintf(stdout,"\n");
  295.     return 0;
  296. }
  297.  
  298.  
  299.  
  300.