number.c 9.04 KB
Newer Older
kollo's avatar
kollo committed
1 2 3 4 5 6 7 8 9 10
/* number.c   Numerische Hilfsfunktionen (c) Markus Hoffmann*/

/* This file is part of X11BASIC, the basic interpreter for Unix/X
 * ============================================================
 * X11BASIC is free software and comes with NO WARRANTY - read the file
 * COPYING for details
 */

#include <stdio.h>
#include <stdlib.h>
11
#include <stdint.h>
kollo's avatar
kollo committed
12
#include <string.h>
kollo's avatar
kollo committed
13
#include <math.h>
kollo's avatar
kollo committed
14

15

kollo's avatar
kollo committed
16 17 18 19
#include "defs.h"
#include "options.h"
#include "x11basic.h"
#include "variablen.h"
kollo's avatar
kollo committed
20 21 22 23 24 25 26
#include "number.h"


static int atohex(char *n) {
  int value=0;
  while(*n) {
    value<<=4;
kollo's avatar
kollo committed
27
    if(v_digit(*n)) value+=(int)(*n-'0');
kollo's avatar
kollo committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    else if(*n>='a' && *n<='f') value+=(int)(*n-'a')+10;
    else if(*n>='A' && *n<='F') value+=(int)(*n-'A')+10;
    n++;
  }
  return(value);
}


static int atobin(char *n) {
  int value=0;
  while(*n) {
    value<<=1;
    if(*n!='0') value++;  
    n++;
  }
  return(value);
}
kollo's avatar
kollo committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
static int atohexc(char *n) {
  int i=0;
  while(*n && (v_digit(*n) || (*n>='a' && *n<='f') || (*n>='A' && *n<='F'))) {i++;n++;}
  return(i);
}
static int atobinc(char *n) {
  int i=0;
  while(*n && (*n=='0' || *n=='1')) {i++;n++;}
  return(i);
}

/* Bestimmt die anzal an Zeichen, welche zu einer Gültigen Zahl
gehören. z.B. für val?()
*/
int myatofc(char *n) {
  if(!n) return(0);
  int i=0;
  while (w_space(*n)) {n++;i++;}  /* Skip leading white space, if any. */
  if(*n=='-' || *n=='+') { n++;i++;} /*  Get sign, if any.  */
   /* try special codings  */
  if(*n=='$') return(i+1+atohexc(++n));
  if(*n=='%') return(i+1+atobinc(++n));
  if(*n=='0' && (n[1]&0xdf)=='X') return(i+2+atohexc(n+2));
kollo's avatar
kollo committed
68 69 70
  if((*n&0xdf)=='E') return(i);  /*should not happen here*/
  if((*n&0xdf)=='I') return(i);  /*should not happen here*/

kollo's avatar
kollo committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  /* Count digits before decimal point or exponent, if any. */
  for(;v_digit(*n); n++) i++;;
  /* Count digits after decimal point, if any. */
  if(*n=='.') {
    n++;i++;
    while(v_digit(*n)) {i++;n++;}
  }
  /* Handle exponent, if any. */
  if((*n&0xdf)=='E') {
    n++;i++;
    /* Get sign of exponent, if any. */
    if(*n=='-' || *n=='+') {i++;n++;} 

    /* Get digits of exponent, if any. */
    for(;v_digit(*n); n++) i++;;
  }
kollo's avatar
kollo committed
87 88 89 90
  if((*n&0xdf)=='I') { /*Check if the last digit is an I*/
     // iscomplex=1;
      n++;i++;
  }
kollo's avatar
kollo committed
91 92 93 94 95 96
  return(i); 
}
/* Bestimmt, ob es sich um eine gültige Zahl handelt und liefert dann
   zurueck:
   1 = INTTYP
   2 = FLOATTYP
kollo's avatar
kollo committed
97 98
   3 = COMPLEXTYP
   5 = ARBINTTYP
kollo's avatar
kollo committed
99 100 101 102 103 104 105
   0 = INVALID
   
*/
int myisatof(char *n) {
  if(!n) return(0);
  int i=0;
  int isfloat=0;
kollo's avatar
kollo committed
106
  int iscomplex=0;
kollo's avatar
kollo committed
107
  int l=strlen(n);
kollo's avatar
kollo committed
108 109
  int mantisse=0;  /*vorauss. Informationsgehalt in bits.*/
  int j;
kollo's avatar
kollo committed
110 111 112
  while (w_space(*n)) {n++;i++;}  /* Skip leading white space, if any. */
  if(*n=='-' || *n=='+') { n++;i++;} /*  Get sign, if any.  */
   /* try special codings  */
kollo's avatar
kollo committed
113 114 115 116 117
  if(*n=='$') {j=atohexc(++n); mantisse+=j*4; i+=1+j;}
  else if(*n=='%') {j=atobinc(++n); mantisse+=j; i+=1+j;}
  else if(*n=='0' && (n[1]&0xdf)=='X') {j=atohexc(n+2); mantisse+=j*4; i+=2+j;}
  else if((*n&0xdf)=='E') ;  /*should not happen here*/
  else if((*n&0xdf)=='I') ;  /*should not happen here*/
kollo's avatar
kollo committed
118 119
  else { 
    /* Count digits before decimal point or exponent, if any. */
kollo's avatar
kollo committed
120 121 122 123
    j=i;
    for(;v_digit(*n); n++) i++;
    j=(i-j)*332/100;  /*Stellen zur Basis 10, 3.32 bits */
    mantisse+=j;
kollo's avatar
kollo committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    /* Count digits after decimal point, if any. */
    if(*n=='.') {
      n++;i++;
      isfloat=1;
      while(v_digit(*n)) {i++;n++;}
    }
    /* Handle exponent, if any. */
    if((*n&0xdf)=='E') {
      isfloat=1;
      n++;i++;
      /* Get sign of exponent, if any. */
      if(*n=='-' || *n=='+') {i++;n++;} 

      /* Get digits of exponent, if any. */
      for(;v_digit(*n); n++) i++;;
    }
kollo's avatar
kollo committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    if((*n&0xdf)=='I') { /*Check if the last digit is an I*/
      iscomplex=1;
      n++;i++;
    }
  }
  /* Anzahl der Digits in der Mantisse zaehlen, um zu entscheiden ob es
  ARBINT oder ARBFLOAT ist*/
    //  printf("<%s> --> %d bits\n",n-i,mantisse);

  
  if(i!=l) return(NOTYP);
  if(iscomplex) return(COMPLEXTYP);
  if(isfloat) return(FLOATTYP);
  if(mantisse<=sizeof(int)*8) return(INTTYP);
  /*spezialfall im Übergangsbereich der Intzahlen
    1000000000 bis 2147483647
    hier müssen wir die Zahl genauer analysieren ob sie noch in INT passt.
    TODO: */
  return(ARBINTTYP); 
}

COMPLEX complex_myatof(char *n) {
  COMPLEX ret;
  ret.i=ret.r=0;
  int l;
  if(n && (l=strlen(n))>0) {
    while(l>0 && w_space(n[l-1]) ) l--;
    if(l>0) {
      if((n[l-1]&0xdf)=='I') ret.i=myatof(n);
      else ret.r=myatof(n);    
    }
kollo's avatar
kollo committed
171
  }
kollo's avatar
kollo committed
172
  return(ret);
kollo's avatar
kollo committed
173
}
kollo's avatar
kollo committed
174

kollo's avatar
kollo committed
175

kollo's avatar
kollo committed
176 177 178 179 180
/* 
Wandelt einen String mit einer (floating-point) Zahl in einen double 
um.

Diese funktion muss stark Geschwindigkeitsoptimiert sein
kollo's avatar
kollo committed
181
TODO: Hier koennte man noch einen Flag zurückliefern, ob es ein real oder imaginaerteil ist.
kollo's avatar
kollo committed
182
*/
kollo's avatar
kollo committed
183
double myatof(char *n) {
kollo's avatar
kollo committed
184 185 186 187 188 189 190 191 192 193
  double sign=1.0;
  while (w_space(*n) ) n++;  /* Skip leading white space, if any. */
  if(*n=='-') { /*  Get sign, if any.  */
    sign=-1.0;
    n++;
  } else if(*n=='+') n++;
  /* try special codings  */
  if(*n=='$') return(sign*(double)atohex(++n));
  if(*n=='%') return(sign*(double)atobin(++n));
  if(*n=='0' && (n[1]&0xdf)=='X') return(sign*(double)atohex(n+2));
kollo's avatar
kollo committed
194

kollo's avatar
kollo committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
  /* Get digits before decimal point or exponent, if any. */
  double value=0.0;
  for(;v_digit(*n); n++) value=value*10.0+(*n-'0');
  /* Get digits after decimal point, if any. */
  if(*n=='.') {
    double pow10 = 10.0;
    n++;
    while(v_digit(*n)) {
      value+=(*n-'0')/pow10;
      pow10*=10.0;
      n++;
    }
  }
  /* Handle exponent, if any. */
  if((*n&0xdf)=='E') {
    int f=0;
    double scale=1.0;
    unsigned int ex=0; 
    n++;

    /* Get sign of exponent, if any. */
    if(*n=='-') {
      f=1;
      n++;
    } else if(*n=='+') n++;
    /* Get digits of exponent, if any. */
    for(;v_digit(*n); n++) ex=ex*10+(*n-'0');
    if(ex>308) ex=308;
    /* Calculate scaling factor. */
    while(ex>= 64) { scale *= 1E64; ex-=64; }
    while(ex>=  8) { scale *= 1E8;  ex-=8; }
    while(ex>   0) { scale *= 10.0; ex--; }

    /* Return signed and scaled floating point result. */
    return sign*(f?(value/scale):(value*scale));
  }
  /* Return signed floating point result. */
  return(sign*value);
kollo's avatar
kollo committed
233 234
}

kollo's avatar
kollo committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
/*Arbitrary int number lesen... */
void arbint_myatof(char *n, ARBINT ret) {
  int sign=1;
// printf("AIMYT: <%s>\n",n);
  while (w_space(*n) ) n++;  /* Skip leading white space, if any. */
  if(*n=='-') { /*  Get sign, if any.  */
    sign=-1;
    n++;
  } else if(*n=='+') n++;
  
  /* try special codings  */
  if(*n=='$') mpz_set_str(ret,++n,16);
  else if(*n=='%') mpz_set_str(ret,++n,2);
  else if(*n=='0' && (n[1]&0xdf)=='X') mpz_set_str(ret,n+2,16);
  else mpz_set_str(ret,n,10);
  if(sign==-1) mpz_neg(ret,ret);
  return;
}
kollo's avatar
kollo committed
253 254 255 256 257 258 259 260
int f_gray(int n) { /* Gray-Code-Konversion */
  unsigned int i=1,a,d;
  if(n>=0) return(n^(n>>1));
  for(a=-n;;i<<=1) {
    a^=(d=a>>i);
    if(d<=1||i==16) return(a);
  }
}
kollo's avatar
kollo committed
261
#if 0
kollo's avatar
kollo committed
262
int f_fak(int k) {
kollo's avatar
kollo committed
263
  register int i,s=1;
kollo's avatar
kollo committed
264 265 266
  for(i=2;i<=k;i++) {s=s*i;}
  return(s);
}
kollo's avatar
kollo committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
#endif
/* Operationen zum COMPLEX Datentyp */

COMPLEX complex_add(COMPLEX a, COMPLEX b) {
  COMPLEX c;
  c.r=a.r+b.r;
  c.i=a.i+b.i;
  return(c);
}
COMPLEX complex_sub(COMPLEX a, COMPLEX b) {
  COMPLEX c;
  c.r=a.r-b.r;
  c.i=a.i-b.i;
  return(c);
}
COMPLEX complex_neg(COMPLEX a) {
  a.r=-a.r;
  a.i=-a.i;
  return(a);
}
double complex_real(COMPLEX a) {return(a.r);}
double complex_imag(COMPLEX a) {return(a.i);}

COMPLEX complex_mul(COMPLEX a, COMPLEX b) {
  COMPLEX c;
  c.r=a.r*b.r-a.i*b.i;
  c.i=a.i*b.r+a.r*b.i;
  return(c);
}
COMPLEX complex_div(COMPLEX a, COMPLEX b) {
  COMPLEX c;
  double tmp=b.r*b.r+b.i*b.i;
  c.r=(a.r*b.r+a.i*b.i)/tmp;
  c.i=(a.i*b.r-a.r*b.i)/tmp;
  return(c);
}
COMPLEX complex_pow(COMPLEX a, COMPLEX b) {
  COMPLEX c;
  double r=sqrt(a.r*a.r+a.i*a.i);
  double t=atan2(a.i,a.r);
  double p=pow(r,b.r)*exp(-b.i*t);
  c.r=p*cos(b.r*t+b.i*log(r));
  c.i=p*sin(b.r*t+b.i*log(r));
  return(c);
}



/*Datentyp zu STRING funktionen */

STRING INTtoSTRING(int n) {
  STRING ret;
  ret.pointer=malloc(100);
  sprintf(ret.pointer,"%d",n);
  ret.len=strlen(ret.pointer);
  return(ret);
}
STRING FLOATtoSTRING(double a) {
  STRING ret;
  ret.pointer=malloc(100);
  sprintf(ret.pointer,"%.13g",a);
  ret.len=strlen(ret.pointer);
  return(ret);
}
STRING COMPLEXtoSTRING(COMPLEX a) {
  STRING ret;
  ret.pointer=malloc(100);
  if(a.i>=0) sprintf(ret.pointer,"(%.13g+%.13gi)",a.r,a.i);
  else sprintf(ret.pointer,"(%.13g%.13gi)",a.r,a.i);
  ret.len=strlen(ret.pointer);
  return(ret);
}


kollo's avatar
kollo committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376


/* 32 Bit Checksumme */

static unsigned long crc_table[256];  /* Table of CRCs of all 8-bit messages. */
static int crc_table_computed = 0;    /* Flag: has the table been computed? Initially false. */

static void make_crc_table(void){    /* Make the table for a fast CRC. */
  unsigned long c;
  int n, k;
  
  for(n=0;n<256;n++) {
    c=(unsigned long)n;
    for(k=0;k<8;k++) {
      if(c&1) c=0xedb88320L^(c>>1);
      else c=c>>1;
    }
    crc_table[n]=c;
  }
  crc_table_computed=1;
}


unsigned long update_crc(unsigned long crc, unsigned char *buf, int len) {
  unsigned long c=crc^0xffffffffL;
  int n;

  if(!crc_table_computed) make_crc_table();
  for(n=0;n<len;n++) c=crc_table[(c^buf[n])&0xff]^(c>>8);
  return c^0xffffffffL;
}