| 1 |
/* |
| 2 |
* The Regina Rexx Interpreter |
| 3 |
* Copyright (C) 1992-1994 Anders Christensen <[email protected]> |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Library General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Library General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Library General Public |
| 16 |
* License along with this library; if not, write to the Free |
| 17 |
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "rexx.h" |
| 21 |
#include <stdio.h> |
| 22 |
#include <limits.h> |
| 23 |
#include <assert.h> |
| 24 |
#include <string.h> |
| 25 |
#include "regina64.h" |
| 26 |
|
| 27 |
|
| 28 |
#define log_xor(a,b) (( (a)&&(!(b)) ) || ( (!(a)) && (b) )) |
| 29 |
#if !defined(MAX) |
| 30 |
# define MAX(a,b) (((a)>(b))?(a):(b)) |
| 31 |
#endif |
| 32 |
#if !defined(MIN) |
| 33 |
# define MIN(a,b) (((a)<(b))?(a):(b)) |
| 34 |
#endif |
| 35 |
#define IS_AT_LEAST(ptr,now,min) \ |
| 36 |
if (now<min) { if (ptr) FreeTSD(ptr); ptr=(char *)MallocTSD(now=min) ; } ; |
| 37 |
|
| 38 |
|
| 39 |
#define stringize(x) #x |
| 40 |
#define stringize_value(x) stringize(x) |
| 41 |
|
| 42 |
typedef struct { /* mat_tsd: static variables of this module (thread-safe) */ |
| 43 |
#ifdef TRACEMEM |
| 44 |
void * outptr1; |
| 45 |
void * outptr2; |
| 46 |
void * outptr3; |
| 47 |
void * outptr4; |
| 48 |
void * outptr5; |
| 49 |
#endif |
| 50 |
|
| 51 |
num_descr edescr; |
| 52 |
num_descr fdescr; |
| 53 |
num_descr rdescr; |
| 54 |
num_descr sdescr; |
| 55 |
|
| 56 |
|
| 57 |
int add_outsize; /* This values MAY all become one. CHECK THIS! */ |
| 58 |
char * add_out; |
| 59 |
int norm_outsize; |
| 60 |
char * norm_out; |
| 61 |
int div_outsize; |
| 62 |
char * div_out; |
| 63 |
int mul_outsize; |
| 64 |
char * mul_out; |
| 65 |
int max_exponent_len; |
| 66 |
} mat_tsd_t; /* thread-specific but only needed by this module. see |
| 67 |
* init_math |
| 68 |
*/ |
| 69 |
|
| 70 |
/* init_math initializes the module. |
| 71 |
* Currently, we set up the thread specific data and check for environment |
| 72 |
* variables to change debugging behaviour. |
| 73 |
* The function returns 1 on success, 0 if memory is short. |
| 74 |
*/ |
| 75 |
int init_math( tsd_t *TSD ) |
| 76 |
{ |
| 77 |
mat_tsd_t *mt; |
| 78 |
|
| 79 |
if (TSD->mat_tsd != NULL) |
| 80 |
return(1); |
| 81 |
|
| 82 |
if ( ( TSD->mat_tsd = MallocTSD( sizeof(mat_tsd_t) ) ) == NULL ) |
| 83 |
return(0); |
| 84 |
mt = (mat_tsd_t *)TSD->mat_tsd; |
| 85 |
memset( mt, 0, sizeof(mat_tsd_t) ); |
| 86 |
|
| 87 |
mt->max_exponent_len = strlen(stringize_value(MAX_EXPONENT)); |
| 88 |
return(1); |
| 89 |
} |
| 90 |
|
| 91 |
#ifdef TRACEMEM |
| 92 |
void mark_descrs( const tsd_t *TSD ) |
| 93 |
{ |
| 94 |
mat_tsd_t *mt; |
| 95 |
|
| 96 |
mt = (mat_tsd_t *)TSD->mat_tsd; |
| 97 |
if (mt->rdescr.num) markmemory( mt->rdescr.num, TRC_MATH ) ; |
| 98 |
if (mt->sdescr.num) markmemory( mt->sdescr.num, TRC_MATH ) ; |
| 99 |
if (mt->fdescr.num) markmemory( mt->fdescr.num, TRC_MATH ) ; |
| 100 |
if (mt->edescr.num) markmemory( mt->edescr.num, TRC_MATH ) ; |
| 101 |
|
| 102 |
if (mt->outptr1) markmemory( mt->outptr1, TRC_MATH ) ; |
| 103 |
if (mt->outptr2) markmemory( mt->outptr2, TRC_MATH ) ; |
| 104 |
if (mt->outptr3) markmemory( mt->outptr3, TRC_MATH ) ; |
| 105 |
if (mt->outptr4) markmemory( mt->outptr4, TRC_MATH ) ; |
| 106 |
if (mt->outptr5) markmemory( mt->outptr5, TRC_MATH ) ; |
| 107 |
} |
| 108 |
#endif /* TRACEMEM */ |
| 109 |
|
| 110 |
static streng *name_of_node( const tsd_t *TSD, cnodeptr node, |
| 111 |
const num_descr *val ) |
| 112 |
{ |
| 113 |
streng *retval; |
| 114 |
cnodeptr run; |
| 115 |
num_descr num; |
| 116 |
int len; |
| 117 |
|
| 118 |
if (node) |
| 119 |
{ |
| 120 |
switch ( node->type ) |
| 121 |
{ |
| 122 |
case X_STRING: |
| 123 |
case X_CON_SYMBOL: |
| 124 |
case X_SIM_SYMBOL: |
| 125 |
case X_STEM_SYMBOL: |
| 126 |
return Str_dupTSD( node->name ); |
| 127 |
|
| 128 |
case X_HEAD_SYMBOL: |
| 129 |
/* |
| 130 |
* Build the complete name of the variable. |
| 131 |
*/ |
| 132 |
len = Str_len( node->name ); |
| 133 |
for ( run = node->p[0]; run; run = run->p[0] ) |
| 134 |
{ |
| 135 |
len += Str_len( run->name ) + 1; |
| 136 |
} |
| 137 |
retval = Str_makeTSD( len ); |
| 138 |
Str_catTSD( retval, node->name ); |
| 139 |
for ( run = node->p[0]; run; run = run->p[0] ) |
| 140 |
{ |
| 141 |
Str_catTSD( retval, run->name ); |
| 142 |
if ( run->p[0] ) |
| 143 |
retval->value[retval->len++] = '.'; |
| 144 |
} |
| 145 |
return retval; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/* |
| 150 |
* reformat the number with all possible digits to show the user the |
| 151 |
* true value.. |
| 152 |
*/ |
| 153 |
num = *val; |
| 154 |
num.used_digits = ( num.size ) ? num.size : 1; |
| 155 |
retval = str_norm( TSD, &num, NULL ); |
| 156 |
|
| 157 |
return retval; |
| 158 |
} |
| 159 |
|
| 160 |
#define LOSTDIGITS_CHECK(val,maxdigits,node) { \ |
| 161 |
const char *_ptr = (const char *) ((val)->num); \ |
| 162 |
int _size = (val)->size; \ |
| 163 |
int _digits = maxdigits; \ |
| 164 |
while (_size && *_ptr == '0') \ |
| 165 |
{ \ |
| 166 |
_ptr++; \ |
| 167 |
_size--; \ |
| 168 |
} \ |
| 169 |
if (_size > _digits) \ |
| 170 |
{ \ |
| 171 |
_size -= _digits; \ |
| 172 |
_ptr += _digits; \ |
| 173 |
while (_size) \ |
| 174 |
{ \ |
| 175 |
if (*_ptr != '0') \ |
| 176 |
{ \ |
| 177 |
condition_hook( TSD, \ |
| 178 |
SIGNAL_LOSTDIGITS, \ |
| 179 |
0, \ |
| 180 |
0, \ |
| 181 |
-1, \ |
| 182 |
name_of_node( TSD, node, val ), \ |
| 183 |
NULL ); \ |
| 184 |
break; \ |
| 185 |
} \ |
| 186 |
_ptr++; \ |
| 187 |
_size--; \ |
| 188 |
} \ |
| 189 |
} \ |
| 190 |
} |
| 191 |
|
| 192 |
/* |
| 193 |
* ANSI chapter 7, beginning: "...matches that syntax and also has a value |
| 194 |
* that is 'whole', that is has no non-zero fractional part." The syntax |
| 195 |
* is that of a plain number. |
| 196 |
* Thus, 1E1 or 1.00 are allowed. |
| 197 |
* returns 0 on error, 1 on success. *value is set to the value on success. |
| 198 |
*/ |
| 199 |
static int whole_number( const num_descr *input, int *value ) |
| 200 |
{ |
| 201 |
/* number must be integer, and must be small enough */ |
| 202 |
int result,i,digit; |
| 203 |
|
| 204 |
if ( input->size > input->exp ) |
| 205 |
{ |
| 206 |
/* |
| 207 |
* Check for non-zeros in the fractional part of the number. |
| 208 |
*/ |
| 209 |
i = MAX( 0, input->exp ); |
| 210 |
for ( ; i < input->size; i++ ) |
| 211 |
{ |
| 212 |
if ( input->num[i] != '0' ) |
| 213 |
return 0; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
/* |
| 218 |
* The number is valid but may be too large. Keep care. |
| 219 |
*/ |
| 220 |
for ( i = 0, result = 0; i < input->exp; i++ ) |
| 221 |
{ |
| 222 |
if ( result > INT_MAX / 10 ) |
| 223 |
return 0; |
| 224 |
result *= 10; |
| 225 |
if ( i < input->size ) |
| 226 |
{ |
| 227 |
digit = input->num[i] - '0'; |
| 228 |
if ( result > INT_MAX - digit ) |
| 229 |
return 0; |
| 230 |
result += digit; |
| 231 |
} |
| 232 |
} |
| 233 |
if (input->negative) |
| 234 |
result = -result; |
| 235 |
|
| 236 |
*value = result; |
| 237 |
return 1; |
| 238 |
} |
| 239 |
|
| 240 |
/* |
| 241 |
* ANSI chapter 7, beginning: "...matches that syntax and also has a value |
| 242 |
* that is 'whole', that is has no non-zero fractional part." The syntax |
| 243 |
* is that of a plain number. |
| 244 |
* Thus, 1E1 or 1.00 are allowed. |
| 245 |
* returns 0 on error, 1 on success. *value is set to the value on success. |
| 246 |
*/ |
| 247 |
static int whole_rx64_number( const num_descr *input, rx_64 *value ) |
| 248 |
{ |
| 249 |
/* number must be integer, and must be small enough */ |
| 250 |
rx_64 result; |
| 251 |
int i,digit; |
| 252 |
|
| 253 |
if ( input->size > input->exp ) |
| 254 |
{ |
| 255 |
/* |
| 256 |
* Check for non-zeros in the fractional part of the number. |
| 257 |
*/ |
| 258 |
i = MAX( 0, input->exp ); |
| 259 |
for ( ; i < input->size; i++ ) |
| 260 |
{ |
| 261 |
if ( input->num[i] != '0' ) |
| 262 |
return 0; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
/* |
| 267 |
* The number is valid but may be too large. Keep care. |
| 268 |
*/ |
| 269 |
for ( i = 0, result = 0; i < input->exp; i++ ) |
| 270 |
{ |
| 271 |
if ( result > RX_64MAX / 10 ) |
| 272 |
return 0; |
| 273 |
result *= 10; |
| 274 |
if ( i < input->size ) |
| 275 |
{ |
| 276 |
digit = input->num[i] - '0'; |
| 277 |
if ( result > RX_64MAX - digit ) |
| 278 |
return 0; |
| 279 |
result += digit; |
| 280 |
} |
| 281 |
} |
| 282 |
if (input->negative) |
| 283 |
result = -result; |
| 284 |
|
| 285 |
*value = result; |
| 286 |
return 1; |
| 287 |
} |
| 288 |
|
| 289 |
int descr_to_int( const tsd_t *TSD, const num_descr *input, int errnum, int suberrnum, const char *bif, int argno ) |
| 290 |
{ |
| 291 |
int result = 0; |
| 292 |
|
| 293 |
if ( !whole_number( input, &result ) ) |
| 294 |
{ |
| 295 |
volatile char *fs; |
| 296 |
streng *h; |
| 297 |
|
| 298 |
h = name_of_node( TSD, NULL, input ); |
| 299 |
fs = tmpstr_of( (tsd_t *)TSD, h ); |
| 300 |
Free_stringTSD( h ); |
| 301 |
switch( errnum ) |
| 302 |
{ |
| 303 |
case ERR_INCORRECT_CALL: |
| 304 |
exiterror( errnum, suberrnum, bif, argno, fs ); |
| 305 |
break; |
| 306 |
case ERR_INVALID_INTEGER: |
| 307 |
exiterror( errnum, suberrnum, fs ); |
| 308 |
break; |
| 309 |
} |
| 310 |
/* should NOT get here */ |
| 311 |
exiterror( ERR_INVALID_INTEGER, 0 ); |
| 312 |
} |
| 313 |
|
| 314 |
return result; |
| 315 |
} |
| 316 |
|
| 317 |
/* |
| 318 |
* strip leading zeros and translate 0e? into a plain 0. |
| 319 |
*/ |
| 320 |
void str_strip( num_descr *num ) |
| 321 |
{ |
| 322 |
int i=0, j=0; |
| 323 |
|
| 324 |
if (num->size==1) |
| 325 |
{ |
| 326 |
if (num->num[0] == '0') |
| 327 |
{ |
| 328 |
num->negative = 0; |
| 329 |
num->exp = 1; |
| 330 |
} |
| 331 |
return; |
| 332 |
} |
| 333 |
|
| 334 |
for ( i = 0; ( i < num->size - 1 ) && ( num->num[i] == '0'); i++ ) |
| 335 |
/* Keep at least one character */; |
| 336 |
if ( i ) |
| 337 |
{ |
| 338 |
for ( j = 0; j < num->size - i; j++ ) |
| 339 |
{ |
| 340 |
num->num[j] = num->num[j + i]; |
| 341 |
} |
| 342 |
|
| 343 |
num->exp -= i; |
| 344 |
num->size -= i; |
| 345 |
assert( num->size > 0 ); |
| 346 |
} |
| 347 |
|
| 348 |
if ( ( num->size == 1 ) && ( num->num[0] == '0' ) ) |
| 349 |
{ |
| 350 |
num->negative = 0; |
| 351 |
num->exp = 1; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
int getdescr( const tsd_t *TSD, const streng *num, num_descr *descr ) |
| 357 |
/* converts num into a descr and returns 0 if successfully. |
| 358 |
* returns 9 or 11 in case of an error. descr contains nonsense in this case. |
| 359 |
* 9 is returned if the exponent is too big, 11 if num is no number. |
| 360 |
* The newly generated descr is as short as possible: leading and |
| 361 |
* trailing zeros (after a period) will be cut, rounding occurs. |
| 362 |
* We don't use registers and hope the compiler does it better than outselves |
| 363 |
* in the optimization stage, else try in this order: c, inlen, in, out, exp. |
| 364 |
*/ |
| 365 |
{ |
| 366 |
const char *in; /* num->value */ |
| 367 |
int inlen; /* chars left in "in" */ |
| 368 |
char *out; /* descr->num */ |
| 369 |
int outpos; /* position where to write */ |
| 370 |
int outmax; /* descr->max */ |
| 371 |
char c, /* tmp var */ |
| 372 |
lastdigit = 0; /* last digit seen for mantissa, init: error */ |
| 373 |
int pointseen, /* point in mantissa seen? */ |
| 374 |
exp, /* exp from mantissa */ |
| 375 |
exp2, /* exp from "1E1" */ |
| 376 |
expsign; /* sign of the exp in "1E-1", 1 or -1 */ |
| 377 |
|
| 378 |
/* |
| 379 |
* The maximum size of the mantissa is the worst case of a plain number, |
| 380 |
* e.g. 123456789 |
| 381 |
*/ |
| 382 |
outmax = Str_len(num); |
| 383 |
|
| 384 |
IS_AT_LEAST( descr->num, descr->max, outmax ); |
| 385 |
|
| 386 |
/* |
| 387 |
* A new number shall always be printed with the current DIGITS value. |
| 388 |
*/ |
| 389 |
descr->used_digits = TSD->currlevel->currnumsize; |
| 390 |
|
| 391 |
in = num->value; |
| 392 |
inlen = Str_len(num); |
| 393 |
/* skip leading spaces */ |
| 394 |
while (inlen && rx_isspace(*in)) |
| 395 |
{ |
| 396 |
in++; |
| 397 |
inlen--; |
| 398 |
} |
| 399 |
|
| 400 |
if (!inlen) |
| 401 |
return 11 ; |
| 402 |
|
| 403 |
c = *in; |
| 404 |
|
| 405 |
/* check sign */ |
| 406 |
if ((c == '-') || (c == '+')) |
| 407 |
{ |
| 408 |
descr->negative = (c == '-') ; |
| 409 |
in++; /* c eaten */ |
| 410 |
inlen--; |
| 411 |
while (inlen && rx_isspace(*in)) /* skip leading spaces */ |
| 412 |
{ |
| 413 |
in++; |
| 414 |
inlen--; |
| 415 |
} |
| 416 |
|
| 417 |
if (!inlen) |
| 418 |
return 11 ; |
| 419 |
} |
| 420 |
else |
| 421 |
descr->negative = 0 ; |
| 422 |
|
| 423 |
/* cut ending blanks first, a non blank exists (in[0]) at this point */ |
| 424 |
while (rx_isspace(in[inlen-1])) |
| 425 |
inlen--; |
| 426 |
|
| 427 |
while (inlen && (*in == '0')) /* skip leading zeros */ |
| 428 |
{ |
| 429 |
in++; |
| 430 |
inlen--; |
| 431 |
lastdigit = '0'; |
| 432 |
} |
| 433 |
if (!inlen) |
| 434 |
{ /* Fast breakout in case of a plain "0" or an error */ |
| 435 |
|
| 436 |
descr->num[0] = lastdigit; |
| 437 |
descr->exp = 1; |
| 438 |
descr->size = 1; |
| 439 |
if (lastdigit == '0') |
| 440 |
{ |
| 441 |
descr->negative = 0; |
| 442 |
return 0 ; |
| 443 |
} |
| 444 |
return 11 ; |
| 445 |
} |
| 446 |
|
| 447 |
/* Transfer digits and check for points */ |
| 448 |
pointseen = 0; /* never seen */ |
| 449 |
exp = 0; |
| 450 |
out = descr->num; |
| 451 |
outpos = 0; |
| 452 |
while (inlen) |
| 453 |
{ |
| 454 |
if ((c = *in) == '.') |
| 455 |
{ |
| 456 |
if (pointseen) |
| 457 |
return 11 ; |
| 458 |
pointseen = 1; |
| 459 |
in++; |
| 460 |
inlen--; |
| 461 |
continue; |
| 462 |
} |
| 463 |
if (!rx_isdigit(c)) |
| 464 |
break; |
| 465 |
if (outpos < outmax) |
| 466 |
{ |
| 467 |
lastdigit = c; |
| 468 |
if ((c=='0') && (outpos==0)) /* skip zeros in "0.0001" */ |
| 469 |
exp--; /* We must be after a point, see zero parsing above */ |
| 470 |
else |
| 471 |
{ |
| 472 |
out[outpos++] = c; |
| 473 |
if (!pointseen) |
| 474 |
exp++; |
| 475 |
} |
| 476 |
} |
| 477 |
else |
| 478 |
{ |
| 479 |
lastdigit = '0'; |
| 480 |
if (!pointseen) |
| 481 |
exp++; |
| 482 |
} |
| 483 |
in++; |
| 484 |
inlen--; |
| 485 |
} |
| 486 |
/* the mantissa is correct now, check for ugly "0.0000" later */ |
| 487 |
if (inlen) |
| 488 |
{ |
| 489 |
/* c is *in at this point, see above */ |
| 490 |
expsign = 1; |
| 491 |
if ((c != 'e') && (c != 'E')) |
| 492 |
return 11 ; |
| 493 |
if (--inlen == 0) /* at least one digit must follow */ |
| 494 |
return 11 ; |
| 495 |
in++; |
| 496 |
|
| 497 |
c = *in; |
| 498 |
if ((c == '+') || (c == '-')) |
| 499 |
{ |
| 500 |
if (c == '-') |
| 501 |
expsign = -1; |
| 502 |
if (--inlen == 0) /* at least one digit must follow */ |
| 503 |
return 11 ; |
| 504 |
in++; |
| 505 |
} |
| 506 |
exp2 = 0; |
| 507 |
while (inlen--) |
| 508 |
{ |
| 509 |
c = *in++; |
| 510 |
if (!rx_isdigit(c)) |
| 511 |
return 11 ; |
| 512 |
/* a rough test first, assume a mantissa with length < MAX_EXPONENT */ |
| 513 |
if ( exp2 > MAX_EXPONENT / 10 ) |
| 514 |
return 9; |
| 515 |
exp2 = exp2*10 + (c - '0'); |
| 516 |
if ( expsign * (exp + expsign * exp2) - 1 > MAX_EXPONENT ) |
| 517 |
return 9; |
| 518 |
} |
| 519 |
if (expsign < 0) |
| 520 |
exp -= exp2; |
| 521 |
else |
| 522 |
exp += exp2; |
| 523 |
} |
| 524 |
if (outpos == 0) /* no digit or 0.000 with or without exp */ |
| 525 |
{ |
| 526 |
if (!lastdigit) |
| 527 |
return 11 ; |
| 528 |
out[outpos++] = '0'; |
| 529 |
exp = 1; |
| 530 |
descr->negative = 0; |
| 531 |
} |
| 532 |
descr->exp = exp; |
| 533 |
descr->size = outpos; |
| 534 |
assert(descr->size <= outmax); |
| 535 |
return(0); |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
/* |
| 540 |
* Rounds descr to size digits. If stop_on_cut is set, a LOSTDIGITS condition |
| 541 |
* is fired if anything other than zeros are truncated. |
| 542 |
*/ |
| 543 |
static void descr_round( num_descr *descr, int size, tsd_t *stop_on_cut ) |
| 544 |
{ |
| 545 |
int i; |
| 546 |
|
| 547 |
/* |
| 548 |
* We don't touch descr->used_digits here. If the caller really needs it, |
| 549 |
* it must be done at that level. Rounding itself isn't an operation |
| 550 |
* creating a number in the terms of Rexx in opposite to TRUNC() or the |
| 551 |
* normal mathematical operations. |
| 552 |
*/ |
| 553 |
|
| 554 |
/* |
| 555 |
* Can't do illegal operations. |
| 556 |
*/ |
| 557 |
assert( size > 0 ); |
| 558 |
|
| 559 |
/* |
| 560 |
* Increment size by the number of leading zeros existing. |
| 561 |
*/ |
| 562 |
for ( i = 0; i < descr->size; i++ ) |
| 563 |
{ |
| 564 |
if ( descr->num[i] == '0' ) |
| 565 |
size++; |
| 566 |
else |
| 567 |
break; |
| 568 |
} |
| 569 |
size += i; |
| 570 |
|
| 571 |
/* |
| 572 |
* Do we have to round? |
| 573 |
*/ |
| 574 |
if ( descr->size <= size ) |
| 575 |
return; |
| 576 |
|
| 577 |
if ( stop_on_cut ) |
| 578 |
{ |
| 579 |
for ( i = size; i < descr->size; i++ ) |
| 580 |
{ |
| 581 |
if ( descr->num[i] != '0' ) |
| 582 |
{ |
| 583 |
condition_hook( stop_on_cut, |
| 584 |
SIGNAL_LOSTDIGITS, |
| 585 |
0, |
| 586 |
0, |
| 587 |
-1, |
| 588 |
name_of_node( stop_on_cut, NULL, descr ), |
| 589 |
NULL ); |
| 590 |
break; |
| 591 |
} |
| 592 |
} |
| 593 |
if ( i >= descr->size ) |
| 594 |
{ |
| 595 |
descr->size = size; |
| 596 |
return; |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
descr->size = size; |
| 601 |
/* |
| 602 |
* Is it possibly just a truncation? |
| 603 |
*/ |
| 604 |
if ( descr->num[size] < '5' ) |
| 605 |
{ |
| 606 |
return; |
| 607 |
} |
| 608 |
|
| 609 |
/* |
| 610 |
* increment next digit, and loop if that was a '9' |
| 611 |
*/ |
| 612 |
for ( i = size - 1; ; ) |
| 613 |
{ |
| 614 |
if ( descr->num[i] != '9' ) |
| 615 |
{ |
| 616 |
descr->num[i]++; |
| 617 |
break; |
| 618 |
} |
| 619 |
|
| 620 |
descr->num[i--] = '0'; |
| 621 |
|
| 622 |
if ( i == -1 ) |
| 623 |
{ |
| 624 |
/* |
| 625 |
* "Carry", we have to increment the exponent. The complete mantissa |
| 626 |
* consists of zeros. We have to set it to "1000...". |
| 627 |
*/ |
| 628 |
#ifndef NDEBUG |
| 629 |
/* |
| 630 |
* Just check a few things ... I don't like surprises |
| 631 |
*/ |
| 632 |
for ( i = 0; i < size; i++ ) |
| 633 |
assert( descr->num[i] == '0' ); |
| 634 |
#endif |
| 635 |
descr->exp++; |
| 636 |
descr->num[0] = '1'; |
| 637 |
break; |
| 638 |
} |
| 639 |
} |
| 640 |
return; |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
void str_round( num_descr *descr, int size ) |
| 645 |
{ |
| 646 |
descr_round( descr, size, NULL ); |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
void str_round_lostdigits( tsd_t *TSD, num_descr *descr, int size ) |
| 651 |
{ |
| 652 |
descr_round( descr, size, TSD ); |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
void descr_copy( const tsd_t *TSD, const num_descr *f, num_descr *s ) |
| 657 |
{ |
| 658 |
/* |
| 659 |
* Check for the special case that these are identical, then we don't |
| 660 |
* have to do any copying, so just return. |
| 661 |
*/ |
| 662 |
if (f==s) |
| 663 |
return ; |
| 664 |
|
| 665 |
s->negative = f->negative ; |
| 666 |
s->exp = f->exp ; |
| 667 |
s->size = f->size ; |
| 668 |
s->used_digits = f->used_digits; |
| 669 |
|
| 670 |
IS_AT_LEAST( s->num, s->max, f->size ) ; |
| 671 |
memcpy( s->num, f->num, f->size ) ; |
| 672 |
} |
| 673 |
|
| 674 |
/* |
| 675 |
* string_add2 computes |
| 676 |
* |
| 677 |
* r=f+s |
| 678 |
* |
| 679 |
* with the current digits() setting of ccns (e.g. TSD->currlevel->currnumsize) |
| 680 |
* Keep in mind that f or s may be identical to r. |
| 681 |
* |
| 682 |
* Function rewritten completely on 03.07.2005 by FGC. The former one was |
| 683 |
* incompatible with the standard. This approach follows the ANSI standard's |
| 684 |
* code example. |
| 685 |
*/ |
| 686 |
static void string_add2( tsd_t *TSD, const num_descr *f, const num_descr *s, |
| 687 |
num_descr *r, int ccns ) |
| 688 |
{ |
| 689 |
mat_tsd_t *mt; /* mt->add_out is used */ |
| 690 |
int neg; /* negate the result if set, and also: f WAS negative*/ |
| 691 |
int sneg_factor; /* -1: s is negative, 1: s is positive */ |
| 692 |
int carry; /* carry flag */ |
| 693 |
int loan; /* loan flag */ |
| 694 |
const char *fnum; /* mantissa start of f */ |
| 695 |
const char *snum; /* mantissa start of s */ |
| 696 |
char *fpoint; /* least significant mantissa position of f */ |
| 697 |
const char *spoint; /* least significant mantissa position of s */ |
| 698 |
/* There is no more digits available if point < num */ |
| 699 |
int h, h2, h3; /* helper */ |
| 700 |
const num_descr *swp;/* helper */ |
| 701 |
int c; /* helper */ |
| 702 |
static const char none[2] =""; /* just to keep pointers valid */ |
| 703 |
|
| 704 |
/* |
| 705 |
* In opposite to ANSI we don't have to consider NUMERIC FUZZ. The |
| 706 |
* comparisons are done using string_test. |
| 707 |
*/ |
| 708 |
|
| 709 |
/* |
| 710 |
* ANSI results f if s==0 (strict comparison!) and s if f==0 with respect |
| 711 |
* to the operator which is "+" in string_add2. |
| 712 |
*/ |
| 713 |
if ((s->size == 1) && (s->num[0] == '0')) |
| 714 |
{ |
| 715 |
descr_copy( TSD, f, r ); |
| 716 |
return; |
| 717 |
} |
| 718 |
if ((f->size == 1) && (f->num[0] == '0')) |
| 719 |
{ |
| 720 |
descr_copy( TSD, s, r ); |
| 721 |
return; |
| 722 |
} |
| 723 |
|
| 724 |
/* |
| 725 |
* The other shortcut isn't mentioned in ANSI where the exponents differ |
| 726 |
* so significantly that one operand isn't used at all. So just continue |
| 727 |
* to try computing 1+1e1000. |
| 728 |
*/ |
| 729 |
|
| 730 |
mt = (mat_tsd_t *)TSD->mat_tsd; |
| 731 |
/* |
| 732 |
* We use a temporary buffer for the result. We don't know the magnitude of |
| 733 |
* the result in advance (99.99 may be rounded to 100), so we need 2 more |
| 734 |
* digits: one for the digits after ccns which is calculated to determine |
| 735 |
* a first rounding and one for the overflow. |
| 736 |
* Thus, the most significant number which is a template of the result |
| 737 |
* has a virtual mantissa position of 1 because position 0 is reserved for |
| 738 |
* the overflow. |
| 739 |
*/ |
| 740 |
IS_AT_LEAST( mt->add_out, mt->add_outsize, ccns+2 ); |
| 741 |
#ifdef TRACEMEM |
| 742 |
mt->outptr5 = mt->add_out; |
| 743 |
#endif |
| 744 |
|
| 745 |
if (f->exp < s->exp) |
| 746 |
{ |
| 747 |
/* |
| 748 |
* The number with the most significant exponent needs to be the |
| 749 |
* first one to make things simpler. |
| 750 |
*/ |
| 751 |
swp = f; |
| 752 |
f = s; |
| 753 |
s = swp; |
| 754 |
} |
| 755 |
|
| 756 |
/* |
| 757 |
* It is much easier to reduce the various situations by making the first |
| 758 |
* number positive: |
| 759 |
* -x op y is equivalent to -(x op -y) |
| 760 |
*/ |
| 761 |
if (f->negative) |
| 762 |
{ |
| 763 |
/* always use a positive number as the left operator */ |
| 764 |
neg = 1; |
| 765 |
sneg_factor = s->negative ? 1 : -1; |
| 766 |
} |
| 767 |
else |
| 768 |
{ |
| 769 |
neg = 0; |
| 770 |
sneg_factor = s->negative ? -1 : 1; |
| 771 |
} |
| 772 |
|
| 773 |
fnum = f->num; |
| 774 |
snum = s->num; |
| 775 |
/* |
| 776 |
* The most significant number is the base of the result. The other number |
| 777 |
* is aligned to the most significant. Example where the second is the |
| 778 |
* most significant number: |
| 779 |
* |
| 780 |
* fffff.ff |
| 781 |
* ss.ssssss |
| 782 |
*/ |
| 783 |
|
| 784 |
h = f->exp - s->exp; |
| 785 |
r->exp = f->exp; /* save now even if s==r */ |
| 786 |
/* |
| 787 |
* s may need an adjustement. |
| 788 |
*/ |
| 789 |
h2 = MIN( ccns + 1, f->size ); /* h2 = used mantissa length of f */ |
| 790 |
h3 = MIN( ccns + 1 - h, s->size ); /* h3 = used mantissa length of s */ |
| 791 |
if ( h3 < 0 ) |
| 792 |
{ |
| 793 |
/* |
| 794 |
* spoint = snum + h3 - 1 is NOT allowed. h3 may be so small that |
| 795 |
* spoint becomes really invalid and may cause a segment violation. |
| 796 |
*/ |
| 797 |
spoint = none; |
| 798 |
snum = spoint + 1; |
| 799 |
} |
| 800 |
else |
| 801 |
{ |
| 802 |
spoint = snum + h3 - 1; |
| 803 |
} |
| 804 |
r->size = MAX( h2, h + h3 ); |
| 805 |
|
| 806 |
/* |
| 807 |
* We conpute "r = f; r += s;" This is much easier to handle than everything |
| 808 |
* else. |
| 809 |
*/ |
| 810 |
|
| 811 |
mt->add_out[0] = '0'; |
| 812 |
memcpy( mt->add_out + 1, fnum, h2 ); |
| 813 |
memset( mt->add_out + h2 + 1, '0', r->size - h2 ); |
| 814 |
|
| 815 |
/* |
| 816 |
* r += s; |
| 817 |
* |
| 818 |
* Get the fpoint to that position that needs to be added by spoint. |
| 819 |
* This is a valid position because we filled up with 0 already. |
| 820 |
*/ |
| 821 |
fnum = mt->add_out; /* NOT +1, keep it on the starting 0 */ |
| 822 |
fpoint = (char *) fnum + h + h3; /* NOT -1, because fnum is decremented */ |
| 823 |
carry = loan = 0; |
| 824 |
|
| 825 |
while ( spoint >= snum ) |
| 826 |
{ |
| 827 |
c = *fpoint + sneg_factor * (*spoint - '0') + carry - loan; |
| 828 |
if ((loan = (c < '0')) != 0) |
| 829 |
{ |
| 830 |
c += 10; |
| 831 |
} |
| 832 |
if ((carry = (c > '9')) != 0) |
| 833 |
{ |
| 834 |
c -= 10; |
| 835 |
} |
| 836 |
spoint--; |
| 837 |
*fpoint-- = (char)c; |
| 838 |
} |
| 839 |
while ( fpoint > fnum ) |
| 840 |
{ |
| 841 |
c = *fpoint + carry - loan; |
| 842 |
if ((loan = (c < '0')) != 0) |
| 843 |
{ |
| 844 |
c += 10; |
| 845 |
} |
| 846 |
if ((carry = (c > '9')) != 0) |
| 847 |
{ |
| 848 |
c -= 10; |
| 849 |
} |
| 850 |
*fpoint-- = (char)c; |
| 851 |
} |
| 852 |
if ( !loan ) |
| 853 |
{ |
| 854 |
if ( carry ) |
| 855 |
{ |
| 856 |
*fpoint = '1'; |
| 857 |
r->exp++; |
| 858 |
r->size++; |
| 859 |
} |
| 860 |
else |
| 861 |
{ |
| 862 |
fpoint++; |
| 863 |
} |
| 864 |
} |
| 865 |
else |
| 866 |
{ |
| 867 |
fpoint++; |
| 868 |
/* |
| 869 |
* Having a loan means we have a negative result. Reverse the result with |
| 870 |
* r = -r; |
| 871 |
*/ |
| 872 |
neg = !neg; |
| 873 |
for ( h = r->size - 1, carry = 10; h >= 0; h-- ) |
| 874 |
{ |
| 875 |
if ( ( fpoint[h] = (char) (carry - (fpoint[h] - '0') + '0') ) > '9' ) |
| 876 |
{ |
| 877 |
fpoint[h] = '0'; |
| 878 |
carry = 10; |
| 879 |
} |
| 880 |
else |
| 881 |
carry = 9; |
| 882 |
} |
| 883 |
} |
| 884 |
|
| 885 |
/* |
| 886 |
* Added so far. Now accomplish the rounding following ANSI rules. |
| 887 |
* |
| 888 |
* We can increase the result's accurracy by jumping over one leading |
| 889 |
* zero if available first, but this breaks ANSI. |
| 890 |
*/ |
| 891 |
|
| 892 |
if ( r->size > ccns ) |
| 893 |
{ |
| 894 |
r->size = ccns; |
| 895 |
if ( fpoint[ccns] > '4' ) |
| 896 |
{ |
| 897 |
/* |
| 898 |
* Increment mantissa regardless of the true sign. |
| 899 |
*/ |
| 900 |
fnum = fpoint; |
| 901 |
fpoint += ccns - 1; |
| 902 |
carry = 1; |
| 903 |
while ( fpoint >= fnum ) |
| 904 |
{ |
| 905 |
if ( ++(*fpoint) <= '9' ) |
| 906 |
{ |
| 907 |
carry = 0; |
| 908 |
fpoint = (char *) fnum - 1; |
| 909 |
break; |
| 910 |
} |
| 911 |
*fpoint = '0'; |
| 912 |
fpoint--; |
| 913 |
} |
| 914 |
|
| 915 |
if ( carry ) |
| 916 |
{ |
| 917 |
r->exp++; |
| 918 |
*fpoint = '1'; |
| 919 |
} |
| 920 |
else |
| 921 |
{ |
| 922 |
fpoint++; |
| 923 |
} |
| 924 |
} |
| 925 |
} |
| 926 |
IS_AT_LEAST( r->num, r->max, r->size ) ; |
| 927 |
memcpy( r->num, fpoint, r->size ); |
| 928 |
r->negative = neg; |
| 929 |
str_strip( r ) ; |
| 930 |
} |
| 931 |
|
| 932 |
void string_add( tsd_t *TSD, const num_descr *f, const num_descr *s, |
| 933 |
num_descr *r, cnodeptr left, cnodeptr right ) |
| 934 |
{ |
| 935 |
int ccns = TSD->currlevel->currnumsize ; |
| 936 |
|
| 937 |
LOSTDIGITS_CHECK( f, ccns, left ); |
| 938 |
LOSTDIGITS_CHECK( s, ccns, right ); |
| 939 |
|
| 940 |
string_add2( TSD, f, s, r, ccns ); |
| 941 |
|
| 942 |
r->used_digits = ccns; |
| 943 |
} |
| 944 |
|
| 945 |
streng *str_format(tsd_t *TSD, const streng *input, int Before, |
| 946 |
int After, int Expp, int Expt) |
| 947 |
/* According to ANSI X3J18-199X, 9.4.2, this function performs the BIF "format" |
| 948 |
* with extensions made by Brian. |
| 949 |
* I rewrote the complete function to allow comparing of this function code |
| 950 |
* to that one made in Rexx originally. |
| 951 |
* input is the first arg to "format" and may not be a number. |
| 952 |
* Before, After, Expp and Expt are the other args to this function and are |
| 953 |
* -1 if they are missing value. |
| 954 |
* FGC |
| 955 |
*/ |
| 956 |
{ |
| 957 |
#define Enlarge(Num,atleast) if (Num.size + (atleast) > Num.max) { \ |
| 958 |
char *newnum = (char *)MallocTSD(Num.size + (atleast) + 5); \ |
| 959 |
Num.max = Num.size + (atleast) + 5; \ |
| 960 |
memcpy( newnum, Num.num, Num.size ); \ |
| 961 |
FreeTSD(Num.num); \ |
| 962 |
Num.num = newnum; \ |
| 963 |
} |
| 964 |
char *buf; |
| 965 |
size_t bufsize; |
| 966 |
size_t bufpos; |
| 967 |
int ShowExp,Exponent,ExponentLen = 0,Afters,Sign,Point,OrigExpp,h; |
| 968 |
streng *retval; |
| 969 |
char Expart[80]; /* enough even on a 256-bit-machine for an int */ |
| 970 |
mat_tsd_t *mt; |
| 971 |
int StrictAnsi; |
| 972 |
|
| 973 |
mt = (mat_tsd_t *)TSD->mat_tsd; |
| 974 |
|
| 975 |
/* |
| 976 |
* Convert the input to a number and check if it is a number at all. |
| 977 |
*/ |
| 978 |
if ( ( h = getdescr( TSD, input, &mt->fdescr ) ) != 0 ) |
| 979 |
{ |
| 980 |
if ( h == 9 ) |
| 981 |
exiterror( ERR_INCORRECT_CALL, h, "FORMAT", 1, mt->max_exponent_len, tmpstr_of( TSD, input ) ); |
| 982 |
else |
| 983 |
exiterror( ERR_INCORRECT_CALL, h, "FORMAT", 1, tmpstr_of( TSD, input ) ); |
| 984 |
} |
| 985 |
|
| 986 |
StrictAnsi = get_options_flag( TSD->currlevel, EXT_STRICT_ANSI ); |
| 987 |
/* |
| 988 |
* Round the number according to NUMERIC DIGITS. This is rule 9.2.1. |
| 989 |
* It is mentioned at several places in 9.4.1 (FORMAT). |
| 990 |
* FGC: This is bullshit if you want to have format() formatting numbers |
| 991 |
* with a higher precision than DIGITS. I've put it into STRICT mode. |
| 992 |
* Regina's normal mode allows any numbers to be formatted. The |
| 993 |
* default formatting rounds to DIGITS, though. |
| 994 |
*/ |
| 995 |
if (StrictAnsi) |
| 996 |
{ |
| 997 |
str_round_lostdigits( TSD, &mt->fdescr, TSD->currlevel->currnumsize ); |
| 998 |
} |
| 999 |
|
| 1000 |
/* |
| 1001 |
* We have done the "call CheckArgs" of the ANSI function. |
| 1002 |
*/ |
| 1003 |
|
| 1004 |
/* |
| 1005 |
* In the simplest case the first is the only argument. |
| 1006 |
*/ |
| 1007 |
if ( ( Before == -1 ) && ( After == -1 ) && ( Expp == -1 ) && ( Expt == -1 ) ) |
| 1008 |
return str_norm( TSD, &mt->fdescr, NULL ); |
| 1009 |
|
| 1010 |
if (Expt == -1) |
| 1011 |
Expt = TSD->currlevel->currnumsize; |
| 1012 |
|
| 1013 |
/* |
| 1014 |
* The number is already set up but check twice that we don't have leading |
| 1015 |
* zeros: |
| 1016 |
*/ |
| 1017 |
str_strip( &mt->fdescr ); |
| 1018 |
|
| 1019 |
/* |
| 1020 |
* Trailing zeros are confusing, too: |
| 1021 |
*/ |
| 1022 |
while ( ( mt->fdescr.size > 1 ) |
| 1023 |
&& ( mt->fdescr.num[mt->fdescr.size - 1] == '0' ) ) |
| 1024 |
mt->fdescr.size--; |
| 1025 |
|
| 1026 |
Sign = ( mt->fdescr.negative ) ? 1 : 0; |
| 1027 |
|
| 1028 |
/* |
| 1029 |
* Now compute the Exponent str_norm would use to format the number. |
| 1030 |
* Don't keep care for ENGINEERING. Note that this equals to the result |
| 1031 |
* We can determine the value of ShowExp en passent. This shortens our |
| 1032 |
* approach to ANSI's algorithm significantly. |
| 1033 |
*/ |
| 1034 |
ShowExp = 0; |
| 1035 |
Exponent = 0; |
| 1036 |
if ( ( ( Expp != 0 ) && |
| 1037 |
( ( mt->fdescr.exp < -5 ) || ( mt->fdescr.exp > Expt ) ) ) || |
| 1038 |
( ( Expt == 0 ) && !StrictAnsi /* fixes bug 562668 */ ) ) |
| 1039 |
{ |
| 1040 |
ShowExp = 1; |
| 1041 |
Exponent = mt->fdescr.exp - 1; |
| 1042 |
} |
| 1043 |
|
| 1044 |
/* The number is normalized, now. |
| 1045 |
* Usage of the variables: |
| 1046 |
* mt->fdescr.num: Mantissa in the ANSI-standard and defined as usual, zeros |
| 1047 |
* may be padded at the end but never at the start because: |
| 1048 |
* mt->fdescr.exp: true exponent for the mantissa. The point is just before |
| 1049 |
* the mantissa. |
| 1050 |
* Exponent: Used Exponent for the mantissa, e.g.: |
| 1051 |
* mt->fdescr.num=1,mt->fdescr.exp=2 = 0.1E2 = 10E0 |
| 1052 |
* In this case Exponent may be 0 to reflect the exponent we |
| 1053 |
* should display. |
| 1054 |
* Point: Defined in the standard but not used. It is obviously |
| 1055 |
* equal to (mt->fdescr.exp-Exponent) where Point must |
| 1056 |
* be inserted before. |
| 1057 |
* examples with both mt->fdescr.num=Mantissa="101" and mt->fdescr.exp=-2: |
| 1058 |
* Exponent=0: output may be "0.00101" |
| 1059 |
* Exponent=-3: output may be "1.01E-3" |
| 1060 |
*/ |
| 1061 |
|
| 1062 |
/* |
| 1063 |
* The fourth and fifth arguments allow for exponential notation. |
| 1064 |
* |
| 1065 |
* Decide whether exponential form to be used, setting ShowExp. |
| 1066 |
* (Done above). |
| 1067 |
* |
| 1068 |
* These tests have to be on the number before any rounding since |
| 1069 |
* decision on whether to have exponent affects what digits surround |
| 1070 |
* the decimal point. |
| 1071 |
* |
| 1072 |
* Sign, Mantissa(mt->fdescr.num) and Exponent now reflect the Number. |
| 1073 |
* Keep in mind that the Mantissa of a num_descr is always normalized to |
| 1074 |
* a value smaller than 1. Thus, mt->fdescr(num=1,exp=1) means 0.1E1=1) |
| 1075 |
* |
| 1076 |
* ShowExp now indicates whether to show an exponent. |
| 1077 |
*/ |
| 1078 |
if ( ShowExp ) |
| 1079 |
{ |
| 1080 |
h = Exponent % 3; |
| 1081 |
if ( ( TSD->currlevel->numform == NUM_FORM_ENG ) && h ) |
| 1082 |
{ |
| 1083 |
/* |
| 1084 |
* Integer division may return values < 0 |
| 1085 |
*/ |
| 1086 |
if ( h < 0 ) |
| 1087 |
h += 3; |
| 1088 |
Exponent -= h; |
| 1089 |
if ( StrictAnsi ) |
| 1090 |
{ |
| 1091 |
/* |
| 1092 |
* As a side effect, ANSI adds zeros automatically. This must be |
| 1093 |
* honoured if after isn't given. |
| 1094 |
*/ |
| 1095 |
Enlarge( mt->fdescr, h ); |
| 1096 |
memset( mt->fdescr.num + mt->fdescr.size, '0', h ); |
| 1097 |
mt->fdescr.size += h; |
| 1098 |
} |
| 1099 |
} |
| 1100 |
} |
| 1101 |
|
| 1102 |
/* |
| 1103 |
* Deal with right of decimal point first since that can affect the |
| 1104 |
* left. Ensure the requested number of digits there. |
| 1105 |
* Afters = length(Mantissa) - Point, thus; |
| 1106 |
*/ |
| 1107 |
Afters = mt->fdescr.size - ( mt->fdescr.exp - Exponent ); |
| 1108 |
if ( After == -1 ) |
| 1109 |
After = Afters; /* Note default. */ |
| 1110 |
|
| 1111 |
/* |
| 1112 |
* The following happens due to our excessive trimming of zeros. |
| 1113 |
*/ |
| 1114 |
if ( After < 0 ) |
| 1115 |
After = 0; |
| 1116 |
|
| 1117 |
/* Make Afters match the requested After */ |
| 1118 |
if ( Afters < After ) |
| 1119 |
{ |
| 1120 |
/* |
| 1121 |
* We have to add (After - Afters) zeros. This can be done more |
| 1122 |
* efficiently later. |
| 1123 |
*/ |
| 1124 |
} |
| 1125 |
else if ( Afters > After ) |
| 1126 |
{ |
| 1127 |
/* |
| 1128 |
* Don't forget the most needed thing. We need it later to determine |
| 1129 |
* the number of zeros being added as 0. |
| 1130 |
*/ |
| 1131 |
Afters = After; |
| 1132 |
|
| 1133 |
/* |
| 1134 |
* Round by adding 5 at the right place. |
| 1135 |
* Regina uses a different algorithm. |
| 1136 |
*/ |
| 1137 |
|
| 1138 |
h = mt->fdescr.exp - Exponent + After; /* aka Point + After */ |
| 1139 |
|
| 1140 |
mt->fdescr.size = h; |
| 1141 |
|
| 1142 |
if ( ( h < 0 ) || ( ( h == 0 ) && ( mt->fdescr.num[0] < '5' ) ) ) |
| 1143 |
{ |
| 1144 |
/* |
| 1145 |
* Round to zero. We may not have any usable characters in the |
| 1146 |
* mantissa, so create one. |
| 1147 |
*/ |
| 1148 |
mt->fdescr.num[0] = '0'; |
| 1149 |
mt->fdescr.size = 1; |
| 1150 |
Sign = 0; |
| 1151 |
} |
| 1152 |
else if ( mt->fdescr.num[h] >= '5' ) |
| 1153 |
{ |
| 1154 |
for ( h--; h >= 0; h-- ) |
| 1155 |
{ |
| 1156 |
if ( ++mt->fdescr.num[h] <= '9' ) |
| 1157 |
break; |
| 1158 |
mt->fdescr.num[h] = '0'; |
| 1159 |
} |
| 1160 |
|
| 1161 |
/* |
| 1162 |
* We have a carry one in front if h < 0. |
| 1163 |
* In this case we have to re-adjust the Exponent which is pretty |
| 1164 |
* difficult in ENGINEERING notation. |
| 1165 |
*/ |
| 1166 |
if ( h < 0 ) |
| 1167 |
{ |
| 1168 |
Enlarge( mt->fdescr, 1 ); |
| 1169 |
memmove( mt->fdescr.num + 1, mt->fdescr.num, mt->fdescr.size ); |
| 1170 |
mt->fdescr.size++; |
| 1171 |
mt->fdescr.num[0] = '1'; |
| 1172 |
mt->fdescr.exp++; |
| 1173 |
|
| 1174 |
/* The hard part follows */ |
| 1175 |
if ( mt->fdescr.exp - Exponent > Expt ) |
| 1176 |
{ |
| 1177 |
if ( StrictAnsi ) |
| 1178 |
{ |
| 1179 |
ShowExp = 1; |
| 1180 |
} |
| 1181 |
else |
| 1182 |
{ |
| 1183 |
if ( Expp != 0 ) |
| 1184 |
ShowExp = 1; |
| 1185 |
} |
| 1186 |
} |
| 1187 |
if (ShowExp) |
| 1188 |
{ |
| 1189 |
Exponent = mt->fdescr.exp - 1; |
| 1190 |
h = Exponent % 3; |
| 1191 |
if ( ( TSD->currlevel->numform == NUM_FORM_ENG ) && h ) |
| 1192 |
{ |
| 1193 |
/* |
| 1194 |
* Integer division may return values < 0 |
| 1195 |
*/ |
| 1196 |
if ( h < 0 ) |
| 1197 |
h += 3; |
| 1198 |
Exponent -= h; |
| 1199 |
} |
| 1200 |
} |
| 1201 |
} |
| 1202 |
} |
| 1203 |
else |
| 1204 |
{ |
| 1205 |
/* |
| 1206 |
* This can leave the result zero. The remaining zero-characters |
| 1207 |
* shall persist, but the sign may change. |
| 1208 |
*/ |
| 1209 |
for ( h--; h >= 0; h-- ) |
| 1210 |
{ |
| 1211 |
if ( mt->fdescr.num[h] != '0' ) |
| 1212 |
break ; |
| 1213 |
} |
| 1214 |
if ( h < 0 ) |
| 1215 |
{ |
| 1216 |
Sign = 0; |
| 1217 |
} |
| 1218 |
} |
| 1219 |
} |
| 1220 |
/* |
| 1221 |
* Rounded |
| 1222 |
* That's all for now with the right part |
| 1223 |
*/ |
| 1224 |
|
| 1225 |
/* |
| 1226 |
* Now deal with the part of the result before the decimal point. |
| 1227 |
* Point doesn't change never more. |
| 1228 |
*/ |
| 1229 |
Point = mt->fdescr.exp - Exponent; |
| 1230 |
h = Point; |
| 1231 |
|
| 1232 |
/* |
| 1233 |
* missing front of the number? |
| 1234 |
* assume 1 char for "0" of "0.xxx" |
| 1235 |
*/ |
| 1236 |
if ( h <= 0 ) |
| 1237 |
h = 1; |
| 1238 |
if ( Before == -1 ) |
| 1239 |
Before = h + Sign; |
| 1240 |
/* |
| 1241 |
* Make Point match Before |
| 1242 |
*/ |
| 1243 |
if ( h > Before - Sign ) |
| 1244 |
{ |
| 1245 |
exiterror( ERR_INCORRECT_CALL, 38, "FORMAT", 2, tmpstr_of( TSD, input ) ); |
| 1246 |
} |
| 1247 |
/* |
| 1248 |
* We don't fill up leading zeros as documented in the standard. Useless! |
| 1249 |
*/ |
| 1250 |
|
| 1251 |
/* |
| 1252 |
* We check the length of the exponent field, first. This allows to |
| 1253 |
* allocate a sufficient string for the complete number. |
| 1254 |
*/ |
| 1255 |
OrigExpp = Expp; |
| 1256 |
if ( ShowExp ) |
| 1257 |
{ |
| 1258 |
/* |
| 1259 |
* Format the exponent. |
| 1260 |
*/ |
| 1261 |
sprintf( Expart, "%+d", Exponent ); |
| 1262 |
ExponentLen = strlen( Expart ) - 1; |
| 1263 |
if ( Expp == -1 ) |
| 1264 |
Expp = ExponentLen; |
| 1265 |
|
| 1266 |
if ( ExponentLen > Expp ) |
| 1267 |
{ |
| 1268 |
exiterror( ERR_INCORRECT_CALL, 38, "FORMAT", 4, tmpstr_of( TSD, input ) ); |
| 1269 |
} |
| 1270 |
} |
| 1271 |
else |
| 1272 |
{ |
| 1273 |
/* |
| 1274 |
* no exponent |
| 1275 |
*/ |
| 1276 |
Expp = 0; |
| 1277 |
} |
| 1278 |
|
| 1279 |
bufsize = Before + After + Expp + 4; /* Point, "E+", term. zero */ |
| 1280 |
buf = (char *)MallocTSD(bufsize); |
| 1281 |
|
| 1282 |
/* |
| 1283 |
* Now do the formatting, it's a little bit complicated, since the parts |
| 1284 |
* of the number may not exist (partially). |
| 1285 |
* |
| 1286 |
* Format the part before the point |
| 1287 |
*/ |
| 1288 |
if ( Point <= 0 ) |
| 1289 |
{ |
| 1290 |
/* |
| 1291 |
* denormalized number |
| 1292 |
*/ |
| 1293 |
assert( Before >= 1 + Sign ); |
| 1294 |
memset( buf, ' ', Before - 1 ); |
| 1295 |
buf[Before - 1] = '0'; |
| 1296 |
if ( Sign ) |
| 1297 |
buf[Before - 2] = '-'; |
| 1298 |
} |
| 1299 |
else |
| 1300 |
{ |
| 1301 |
memset( buf, ' ', Before - Point ); |
| 1302 |
if ( ( h = Point ) > mt->fdescr.size ) |
| 1303 |
h = mt->fdescr.size; |
| 1304 |
memcpy( buf + Before - Point, mt->fdescr.num, h ); |
| 1305 |
memset( buf + Before - Point + h, '0', Point - h ); |
| 1306 |
if ( Sign ) |
| 1307 |
buf[Before - Point - 1] = '-'; |
| 1308 |
} |
| 1309 |
bufpos = Before; |
| 1310 |
|
| 1311 |
/* |
| 1312 |
* Process the part after the decimal point |
| 1313 |
*/ |
| 1314 |
if ( After > 0 ) |
| 1315 |
{ |
| 1316 |
buf[bufpos++] = '.'; |
| 1317 |
if (Point < 0) |
| 1318 |
{ |
| 1319 |
/* |
| 1320 |
* Denormalized mantissa, we must fill up with zeros |
| 1321 |
*/ |
| 1322 |
h = -Point; |
| 1323 |
if ( h > After ) |
| 1324 |
h = After; /* beware of an overrun */ |
| 1325 |
memset( buf + bufpos, '0', h ); |
| 1326 |
if ( After - h <= mt->fdescr.size ) |
| 1327 |
{ |
| 1328 |
memcpy( buf + bufpos + h, mt->fdescr.num, After - h ); |
| 1329 |
} |
| 1330 |
else |
| 1331 |
{ |
| 1332 |
memcpy( buf + bufpos + h, mt->fdescr.num, mt->fdescr.size ); |
| 1333 |
memset( buf + bufpos + h + mt->fdescr.size, |
| 1334 |
'0', |
| 1335 |
After - h - mt->fdescr.size ); |
| 1336 |
} |
| 1337 |
} |
| 1338 |
else |
| 1339 |
{ |
| 1340 |
if ( After + Point <= mt->fdescr.size ) |
| 1341 |
{ |
| 1342 |
memcpy( buf + bufpos, mt->fdescr.num + Point, After ); |
| 1343 |
} |
| 1344 |
else |
| 1345 |
{ |
| 1346 |
/* |
| 1347 |
* number of After characters in the mantissa? |
| 1348 |
*/ |
| 1349 |
if ( ( h = mt->fdescr.size - Point ) < 0 ) |
| 1350 |
h = 0; |
| 1351 |
|
| 1352 |
memcpy( buf + bufpos, mt->fdescr.num + Point, h ); |
| 1353 |
memset( buf + bufpos + h, '0', After - h ); |
| 1354 |
} |
| 1355 |
} |
| 1356 |
bufpos += After; |
| 1357 |
} |
| 1358 |
|
| 1359 |
/* Finally process the exponent. ExponentBuffer contents the exponent |
| 1360 |
* without the sign. |
| 1361 |
*/ |
| 1362 |
if ( ShowExp ) |
| 1363 |
{ |
| 1364 |
if ( Exponent == 0 ) |
| 1365 |
{ |
| 1366 |
if ( OrigExpp != -1 ) |
| 1367 |
{ |
| 1368 |
memset( buf + bufpos, ' ', Expp + 2 ); |
| 1369 |
bufpos += Expp + 2; |
| 1370 |
} |
| 1371 |
} |
| 1372 |
else |
| 1373 |
{ |
| 1374 |
buf[bufpos++] = 'E'; |
| 1375 |
buf[bufpos++] = Expart[0]; |
| 1376 |
memset( buf + bufpos, '0', Expp - ExponentLen ); |
| 1377 |
memcpy( buf + bufpos + Expp - ExponentLen, Expart + 1, ExponentLen ); |
| 1378 |
bufpos += Expp; |
| 1379 |
} |
| 1380 |
} |
| 1381 |
|
| 1382 |
assert( bufpos < bufsize ); |
| 1383 |
buf[bufpos] = '\0'; |
| 1384 |
|
| 1385 |
retval = Str_creTSD( buf ); |
| 1386 |
FreeTSD( buf ); |
| 1387 |
return retval; |
| 1388 |
#undef Enlarge |
| 1389 |
} |
| 1390 |
|
| 1391 |
/* |
| 1392 |
* str_norm does the "PostOp" operation of the ANSI standard. It throws |
| 1393 |
* away leading zeros and does some rounding with DIGITS of the time the |
| 1394 |
* number was generated. try (if non-NULL) is used to print the number and is |
| 1395 |
* returned. Never use try again after the call with the exception of |
| 1396 |
* "x = str_norm(?,?,x)". |
| 1397 |
* |
| 1398 |
* The return value is the printable number. |
| 1399 |
* |
| 1400 |
* The value "in" may be rounded and reformatted. |
| 1401 |
*/ |
| 1402 |
streng *str_norm( const tsd_t *TSD, num_descr *in, streng *trystr ) |
| 1403 |
{ |
| 1404 |
streng *result; |
| 1405 |
int i; |
| 1406 |
int size,exp,ccns,Point; |
| 1407 |
mat_tsd_t *mt; |
| 1408 |
|
| 1409 |
mt = (mat_tsd_t *)TSD->mat_tsd; |
| 1410 |
|
| 1411 |
ccns = in->used_digits; |
| 1412 |
/* |
| 1413 |
* We use ccns for the allocation of the string's content. Chop this value |
| 1414 |
* is case of number which doesn't need billions of digits. |
| 1415 |
*/ |
| 1416 |
if ( ( exp = in->exp ) < 0 ) |
| 1417 |
exp = -exp; |
| 1418 |
if ( ccns > in->size + exp + 10 ) |
| 1419 |
ccns = in->size + exp + 10 ; |
| 1420 |
/* |
| 1421 |
* The longest number produced from a num_descr is (with DIGITS=i) |
| 1422 |
* -1.2...iE-MAX_EXPONENT |
| 1423 |
* and its length is DIGITS + length(MAX_EXPONENT) + strlen(-.E-\0) |
| 1424 |
*/ |
| 1425 |
IS_AT_LEAST( mt->norm_out, mt->norm_outsize, ccns + mt->max_exponent_len + 6 ); |
| 1426 |
#ifdef TRACEMEM |
| 1427 |
mt->outptr2 = mt->norm_out; |
| 1428 |
#endif |
| 1429 |
|
| 1430 |
/* |
| 1431 |
* remove effect of leading zeros in the descriptor |
| 1432 |
*/ |
| 1433 |
for ( i = 0; i < in->size; i++ ) |
| 1434 |
{ |
| 1435 |
if ( in->num[i] != '0' ) |
| 1436 |
break; |
| 1437 |
} |
| 1438 |
if ( i ) |
| 1439 |
{ |
| 1440 |
memmove( in->num, in->num + i, in->size - i ); |
| 1441 |
in->exp -= i; |
| 1442 |
in->size -= i; |
| 1443 |
} |
| 1444 |
|
| 1445 |
/* |
| 1446 |
* We may have a number without mantissa. Even a rounding with DIGITS==1 |
| 1447 |
* will always produce a non-zero number. We can therefore do the test |
| 1448 |
* before every other and return "0" in case of a mantissa with zeros. |
| 1449 |
*/ |
| 1450 |
if ( in->size == 0 ) |
| 1451 |
{ |
| 1452 |
in->size = 1; |
| 1453 |
in->exp = 1; |
| 1454 |
in->negative = 0; |
| 1455 |
in->num[0] = '0'; |
| 1456 |
if ( trystr ) |
| 1457 |
{ |
| 1458 |
if ( trystr->max ) |
| 1459 |
{ |
| 1460 |
trystr->value[0] = '0'; |
| 1461 |
trystr->len = 1; |
| 1462 |
} |
| 1463 |
else |
| 1464 |
{ |
| 1465 |
Free_stringTSD( trystr ); |
| 1466 |
trystr = Str_creTSD( "0" ); |
| 1467 |
} |
| 1468 |
} |
| 1469 |
else |
| 1470 |
trystr = Str_creTSD( "0" ); |
| 1471 |
#if NOT_YET_TESTED |
| 1472 |
/* free memory to fix memory leak; Bug #455 */ |
| 1473 |
if (mt->norm_out) |
| 1474 |
{ |
| 1475 |
FreeTSD(mt->norm_out); |
| 1476 |
mt->norm_out = NULL; |
| 1477 |
mt->norm_outsize = 0; |
| 1478 |
} |
| 1479 |
#endif |
| 1480 |
|
| 1481 |
return trystr; |
| 1482 |
} |
| 1483 |
|
| 1484 |
/* |
| 1485 |
* Do the rounding needed for DIGITS. It may be to late here for doing this. |
| 1486 |
* The user may have changed DIGITS between the operation and this function. |
| 1487 |
*/ |
| 1488 |
|
| 1489 |
i = ccns; |
| 1490 |
if ( in->size > i ) |
| 1491 |
{ |
| 1492 |
in->size = i; |
| 1493 |
if ( in->num[i] >= '5' ) |
| 1494 |
{ |
| 1495 |
for ( i--; i >= 0; i-- ) |
| 1496 |
{ |
| 1497 |
if ( ++in->num[i] <= '9' ) |
| 1498 |
{ |
| 1499 |
break; |
| 1500 |
} |
| 1501 |
in->num[i] = '0'; |
| 1502 |
} |
| 1503 |
if ( i < 0 ) |
| 1504 |
{ |
| 1505 |
/* |
| 1506 |
* "Carry" |
| 1507 |
*/ |
| 1508 |
memmove( in->num + 1, in->num, in->size - 1 ); |
| 1509 |
in->num[0] = '1'; |
| 1510 |
in->exp++; |
| 1511 |
} |
| 1512 |
} |
| 1513 |
/* |
| 1514 |
* This may have produced leading zeros. |
| 1515 |
*/ |
| 1516 |
} |
| 1517 |
|
| 1518 |
/* |
| 1519 |
* Truncation of trailing zeros must be done by the operations themself. |
| 1520 |
* We are not allowed to cut them away, even after a decimal point. |
| 1521 |
*/ |
| 1522 |
|
| 1523 |
exp = in->exp - 1; |
| 1524 |
|
| 1525 |
/* |
| 1526 |
* Compute the exponent used to display. exp==0 -> don't show an exponent. |
| 1527 |
* Respect the ENGINEERING format. |
| 1528 |
* TRL states that: |
| 1529 |
* We use Exponential format to display a number if the number of places |
| 1530 |
* before the decimal is greater than DIGITS or if the number of places |
| 1531 |
* after the decimal is greater than 2 * DIGITS (TRL2 page 136) |
| 1532 |
* if ( ( exp >= ccns ) || ( abs(exp) > (2*ccns) ) ) |
| 1533 |
* |
| 1534 |
* ANSI states that: |
| 1535 |
* We use Exponential format to display a number if the number of places |
| 1536 |
* before the decimal is greater than DIGITS or if the number of places |
| 1537 |
* after the decimal is greater than 5 (ANSI page 52) |
| 1538 |
* if ( ( exp >= ccns ) || ( abs(exp) > (2*ccns) ) ) |
| 1539 |
*/ |
| 1540 |
if ( ( exp < -6 ) || ( exp >= ccns ) ) /* ANSI rules for displaying exponent */ |
| 1541 |
{ |
| 1542 |
i = exp % 3; |
| 1543 |
if ( ( TSD->currlevel->numform == NUM_FORM_ENG ) && i ) |
| 1544 |
{ |
| 1545 |
/* |
| 1546 |
* Integer division may return values < 0. |
| 1547 |
*/ |
| 1548 |
if ( i < 0 ) |
| 1549 |
i += 3; |
| 1550 |
exp -= i; |
| 1551 |
} |
| 1552 |
if ( ( MAX_EXPONENT < exp ) || ( -MAX_EXPONENT > exp ) ) |
| 1553 |
{ |
| 1554 |
exiterror( ERR_ARITH_OVERFLOW, 0 ) ; |
| 1555 |
return NULL ; |
| 1556 |
} |
| 1557 |
} |
| 1558 |
else |
| 1559 |
{ |
| 1560 |
exp = 0; |
| 1561 |
} |
| 1562 |
|
| 1563 |
/* |
| 1564 |
* Point points to the first char in the mantissa which is right of the |
| 1565 |
* decimal point. |
| 1566 |
*/ |
| 1567 |
Point = in->exp - exp; |
| 1568 |
|
| 1569 |
size = 0; |
| 1570 |
if ( in->negative ) |
| 1571 |
mt->norm_out[size++] = '-'; |
| 1572 |
|
| 1573 |
/* |
| 1574 |
* Process the part BEFORE the point. |
| 1575 |
*/ |
| 1576 |
if ( Point <= 0 ) |
| 1577 |
{ |
| 1578 |
/* |
| 1579 |
* Something like "0.1". We have to provide an integer part. |
| 1580 |
*/ |
| 1581 |
mt->norm_out[size++] = '0'; |
| 1582 |
} |
| 1583 |
else if ( Point <= in->size ) |
| 1584 |
{ |
| 1585 |
/* |
| 1586 |
* Integer part exists and lays in the matissa completely. |
| 1587 |
*/ |
| 1588 |
memcpy( mt->norm_out + size, in->num, Point ); |
| 1589 |
size += Point; |
| 1590 |
} |
| 1591 |
else |
| 1592 |
{ |
| 1593 |
/* |
| 1594 |
* Integer part exists but is partially represented only, something |
| 1595 |
* like "1e3" without trailing zeros. |
| 1596 |
*/ |
| 1597 |
memcpy( mt->norm_out + size, in->num, in->size ); |
| 1598 |
size += in->size; |
| 1599 |
memset( mt->norm_out + size, '0', Point - in->size ); |
| 1600 |
size += Point - in->size; |
| 1601 |
} |
| 1602 |
|
| 1603 |
/* |
| 1604 |
* Process the part AFTER the point. |
| 1605 |
*/ |
| 1606 |
if ( Point < in->size ) |
| 1607 |
{ |
| 1608 |
/* |
| 1609 |
* We have to show something as a fractional part. |
| 1610 |
*/ |
| 1611 |
mt->norm_out[size++] = '.'; |
| 1612 |
|
| 1613 |
if ( Point < 0 ) |
| 1614 |
{ |
| 1615 |
/* |
| 1616 |
* Something like 1E-3, leading zeros are missing. |
| 1617 |
*/ |
| 1618 |
memset( mt->norm_out + size, '0', -Point ); |
| 1619 |
size += -Point; |
| 1620 |
memcpy( mt->norm_out + size, in->num, in->size ); |
| 1621 |
size += in->size; |
| 1622 |
} |
| 1623 |
else |
| 1624 |
{ |
| 1625 |
/* |
| 1626 |
* Something of the fractional part is there as induced by the |
| 1627 |
* outer "if". |
| 1628 |
*/ |
| 1629 |
memcpy( mt->norm_out + size, in->num + Point, in->size - Point ); |
| 1630 |
size += in->size - Point; |
| 1631 |
} |
| 1632 |
} |
| 1633 |
|
| 1634 |
/* |
| 1635 |
* We can add the exponent and that's it. |
| 1636 |
*/ |
| 1637 |
if ( exp != 0 ) |
| 1638 |
{ |
| 1639 |
size += sprintf( mt->norm_out + size, "E%+d", exp ); |
| 1640 |
/* |
| 1641 |
* implicitely adds a \0 at the end. |
| 1642 |
*/ |
| 1643 |
} |
| 1644 |
assert( size + 1 <= mt->norm_outsize ); |
| 1645 |
|
| 1646 |
if ( trystr ) |
| 1647 |
{ |
| 1648 |
if ( trystr->max < size ) |
| 1649 |
{ |
| 1650 |
Free_stringTSD( trystr ); |
| 1651 |
trystr = NULL; |
| 1652 |
} |
| 1653 |
} |
| 1654 |
if ( trystr ) |
| 1655 |
result = trystr; |
| 1656 |
else |
| 1657 |
result = Str_makeTSD( size ); |
| 1658 |
result->len = size; |
| 1659 |
memcpy( result->value, mt->norm_out, size ); |
| 1660 |
#if NOT_YET_TESTED |
| 1661 |
/* free memory to fix memory leak; Bug #455 */ |
| 1662 |
if (mt->norm_out) |
| 1663 |
{ |
| 1664 |
FreeTSD(mt->norm_out); |
| 1665 |
mt->norm_out = NULL; |
| 1666 |
mt->norm_outsize = 0; |
| 1667 |
} |
| 1668 |
#endif |
| 1669 |
return result ; |
| 1670 |
} |
| 1671 |
|
| 1672 |
int string_test( const tsd_t *TSD, const num_descr *first, |
| 1673 |
const num_descr *second ) |
| 1674 |
{ |
| 1675 |
int i=0, top=0, fnul=0, snul=0 ; |
| 1676 |
char fchar=' ', schar=' ' ; |
| 1677 |
int ccns = TSD->currlevel->currnumsize; |
| 1678 |
|
| 1679 |
if ( first->negative != second->negative ) /* have different signs */ |
| 1680 |
return ( first->negative ? -1 : 1 ) ; |
| 1681 |
|
| 1682 |
fnul = ( first->size==1 && first->exp==1 && first->num[0]=='0') ; |
| 1683 |
snul = ( second->size==1 && second->exp==1 && second->num[0]=='0') ; |
| 1684 |
if (fnul || snul) |
| 1685 |
{ |
| 1686 |
if (fnul && snul) return 0 ; |
| 1687 |
if (fnul) return (second->negative ? 1 : -1 ) ; |
| 1688 |
else return (first->negative ? -1 : 1 ) ; |
| 1689 |
} |
| 1690 |
|
| 1691 |
if ( first->exp != second->exp ) /* have different order */ |
| 1692 |
return (log_xor( first->negative, first->exp>second->exp ) ? 1 : -1 ) ; |
| 1693 |
|
| 1694 |
/* same order and sign, have to compare ccns - TSD->currlevel->numfuzz first */ |
| 1695 |
top = MIN( ccns - TSD->currlevel->numfuzz, MAX( first->size, second->size )) ; |
| 1696 |
for ( i=0; i<top; i++ ) |
| 1697 |
{ |
| 1698 |
fchar = (char) ((first->size > i) ? first->num[i] : '0') ; |
| 1699 |
schar = (char) ((second->size > i) ? second->num[i] : '0') ; |
| 1700 |
if ( fchar != schar ) |
| 1701 |
return log_xor( first->negative, fchar>schar ) ? 1 : -1 ; |
| 1702 |
} |
| 1703 |
|
| 1704 |
/* hmmm, last resort: can the numbers be rounded to make a difference */ |
| 1705 |
fchar = (char) ((first->size > i) ? first->num[i] : '0') ; |
| 1706 |
schar = (char) ((second->size > i) ? second->num[i] : '0') ; |
| 1707 |
if (((fchar>'4') && (schar>'4')) || ((fchar<'5') && (schar<'5'))) |
| 1708 |
return 0 ; /* equality! */ |
| 1709 |
|
| 1710 |
/* now, one is rounded upwards, the other downwards */ |
| 1711 |
return log_xor( first->negative, fchar>'5' ) ? 1 : -1 ; |
| 1712 |
} |
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
num_descr *string_incr( tsd_t *TSD, num_descr *input, cnodeptr node ) |
| 1717 |
{ |
| 1718 |
int last,ccns=TSD->currlevel->currnumsize; |
| 1719 |
char *cptr=NULL ; |
| 1720 |
|
| 1721 |
assert( input->size > 0 ) ; |
| 1722 |
|
| 1723 |
if (input->size != input->exp || input->exp >= ccns) |
| 1724 |
{ |
| 1725 |
static const num_descr one = { "1", 0, 1, 1, 1, -1 } ; |
| 1726 |
|
| 1727 |
string_add( TSD, input, (num_descr *) &one, input, node, NULL ) ; |
| 1728 |
str_round(input,ccns) ; |
| 1729 |
return input ; |
| 1730 |
} |
| 1731 |
|
| 1732 |
cptr = input->num ; |
| 1733 |
last = input->size; |
| 1734 |
|
| 1735 |
|
| 1736 |
LOSTDIGITS_CHECK( input, ccns, node ); |
| 1737 |
/* |
| 1738 |
* No LOSTDIGITS check for "1". If this fails, everything fails... |
| 1739 |
*/ |
| 1740 |
last--; |
| 1741 |
|
| 1742 |
for (;;) |
| 1743 |
{ |
| 1744 |
if (input->negative) |
| 1745 |
{ |
| 1746 |
if (cptr[last] > '1') |
| 1747 |
{ |
| 1748 |
cptr[last]-- ; |
| 1749 |
input->used_digits = ccns; |
| 1750 |
return input ; |
| 1751 |
} |
| 1752 |
else if (cptr[last]=='1') |
| 1753 |
{ |
| 1754 |
cptr[last]-- ; |
| 1755 |
if (last==0) |
| 1756 |
str_strip( input ) ; |
| 1757 |
input->used_digits = ccns; |
| 1758 |
return input ; |
| 1759 |
} |
| 1760 |
else |
| 1761 |
{ |
| 1762 |
assert( cptr[last] == '0' ) ; |
| 1763 |
assert( last ) ; |
| 1764 |
cptr[last--] = '9' ; |
| 1765 |
} |
| 1766 |
} |
| 1767 |
else |
| 1768 |
{ |
| 1769 |
if (cptr[last] < '9') |
| 1770 |
{ |
| 1771 |
cptr[last]++ ; |
| 1772 |
input->used_digits = ccns; |
| 1773 |
return input ; |
| 1774 |
} |
| 1775 |
else |
| 1776 |
{ |
| 1777 |
assert( cptr[last] == '9' ) ; |
| 1778 |
cptr[last--] = '0' ; |
| 1779 |
} |
| 1780 |
} |
| 1781 |
|
| 1782 |
if (last<0) |
| 1783 |
{ |
| 1784 |
if (input->size >= input->max) |
| 1785 |
{ |
| 1786 |
char *newnum ; |
| 1787 |
|
| 1788 |
assert( input->size == input->max ) ; |
| 1789 |
newnum = (char *)MallocTSD( input->max * 2 + 2 ) ; |
| 1790 |
memcpy( newnum+1, input->num, input->size ) ; |
| 1791 |
newnum[0] = '0' ; |
| 1792 |
input->size++ ; |
| 1793 |
input->exp++ ; |
| 1794 |
input->max = input->max*2 + 2 ; |
| 1795 |
FreeTSD( input->num ) ; |
| 1796 |
cptr = input->num = newnum ; |
| 1797 |
} |
| 1798 |
else |
| 1799 |
{ |
| 1800 |
memmove( input->num+1, input->num, input->size ) ; |
| 1801 |
input->size++ ; |
| 1802 |
input->exp++ ; |
| 1803 |
input->num[0] = '0' ; |
| 1804 |
} |
| 1805 |
last++ ; |
| 1806 |
} |
| 1807 |
} |
| 1808 |
} |
| 1809 |
|
| 1810 |
|
| 1811 |
/* |
| 1812 |
* tests for an ANSI compatible whole number. Look at myiswnumber() |
| 1813 |
* for a description. |
| 1814 |
*/ |
| 1815 |
static int test_whole( const tsd_t *TSD, const num_descr *input, |
| 1816 |
int noDigitsLimit ) |
| 1817 |
{ |
| 1818 |
int i; |
| 1819 |
|
| 1820 |
if ( input->size > input->exp ) |
| 1821 |
{ |
| 1822 |
/* |
| 1823 |
* Check for non-zeros in the fractional part of the number. |
| 1824 |
*/ |
| 1825 |
i = MAX( 0, input->exp ); |
| 1826 |
for ( ; i < input->size; i++ ) |
| 1827 |
{ |
| 1828 |
if ( input->num[i] != '0' ) |
| 1829 |
return 0; |
| 1830 |
} |
| 1831 |
} |
| 1832 |
|
| 1833 |
if ( !noDigitsLimit ) |
| 1834 |
{ |
| 1835 |
for (i = 0; i < input->size; i++) |
| 1836 |
{ |
| 1837 |
if (input->num[i] != '0') |
| 1838 |
break; |
| 1839 |
} |
| 1840 |
if (i < input->size) |
| 1841 |
{ |
| 1842 |
/* not a 0 */ |
| 1843 |
if (input->exp - i > TSD->currlevel->currnumsize) |
| 1844 |
return 0; |
| 1845 |
} |
| 1846 |
} |
| 1847 |
return 1; |
| 1848 |
} |
| 1849 |
|
| 1850 |
|
| 1851 |
/* |
| 1852 |
* Division in the typical manner we learn in school hopefully. |
| 1853 |
* |
| 1854 |
* type is DIVTYPE_NORMAL for floating point division, DIVTYPE_INTEGER for |
| 1855 |
* division without remainer, DIVTYPE_REMAINER if the remainer is interested in |
| 1856 |
* and DIVTYPE_BOTH if both the integer part and the remainer shall be |
| 1857 |
* returned. |
| 1858 |
* |
| 1859 |
* We compute f/s with a NUMERIC DIGITS value of ccns. |
| 1860 |
* |
| 1861 |
* The return value is put into *r, *r2 holds the remainer if DIVTYPE_BOTH |
| 1862 |
* is set. |
| 1863 |
* |
| 1864 |
* We throw an error on non-floating point division if the COMPLETE integer |
| 1865 |
* part of the division can't be represented without rounding. |
| 1866 |
*/ |
| 1867 |
static void string_div2( tsd_t *TSD, const num_descr *f, const num_descr *s, |
| 1868 |
num_descr *r, num_descr *r2, int type, int ccns ) |
| 1869 |
{ |
| 1870 |
int ssize,tstart,tcnt,finished=0,tend; |
| 1871 |
int i,cont,outp,test,loan; |
| 1872 |
int origneg,origexp; |
| 1873 |
mat_tsd_t *mt; |
| 1874 |
|
| 1875 |
mt = (mat_tsd_t *)TSD->mat_tsd; |
| 1876 |
|
| 1877 |
IS_AT_LEAST( mt->div_out, mt->div_outsize, (ccns+1) * 2 + 1 ); |
| 1878 |
IS_AT_LEAST( r->num, r->max, ccns+1 ); |
| 1879 |
#ifdef TRACEMEM |
| 1880 |
mt->outptr3 = mt->div_out; |
| 1881 |
#endif |
| 1882 |
|
| 1883 |
/* |
| 1884 |
* We don't want to strip leading zeros here! |
| 1885 |
*/ |
| 1886 |
assert( ( ( f->size > 1 ) && ( f->num[0] != '0' ) ) || ( f->size == 1 ) ); |
| 1887 |
assert( ( s->size != 0 ) && ( s->num[0] != '0' ) ); |
| 1888 |
|
| 1889 |
/* |
| 1890 |
* ssize is the count of the used digits from s's mantissa. |
| 1891 |
*/ |
| 1892 |
ssize = MIN( s->size, ccns+1 ); |
| 1893 |
|
| 1894 |
/* |
| 1895 |
* Compute the trivial parts of the result. |
| 1896 |
* Imagine xxxxx : yy = zzzz, probably with zeros. |
| 1897 |
*/ |
| 1898 |
r->exp = 1 + f->exp - s->exp; |
| 1899 |
r->negative = log_xor( f->negative, s->negative ); |
| 1900 |
|
| 1901 |
/* |
| 1902 |
* Initialize the pointers. |
| 1903 |
* tstart, tend, tcnt |
| 1904 |
*/ |
| 1905 |
tstart = 0; |
| 1906 |
tend = tstart + MIN( f->size, ccns+1 ); |
| 1907 |
|
| 1908 |
/* |
| 1909 |
* First, fill div_out with f as the residual. Fill up with zeros. |
| 1910 |
*/ |
| 1911 |
for ( tcnt = tstart; tcnt < ssize; tcnt++ ) |
| 1912 |
mt->div_out[tcnt] = (char) ( ( tcnt < tend ) ? f->num[tcnt] : '0' ); |
| 1913 |
|
| 1914 |
/* |
| 1915 |
* Imagine xxxxx : yy again. If the first length(yy) digits of xxxxx |
| 1916 |
* are smaller than yy, we have to set the first digit of z to 0. For |
| 1917 |
* entering the main algorithm, we do the step here decrementing the |
| 1918 |
* result's exponent, which if mathematically the same. |
| 1919 |
* e.g. 12345 : 23 = 0zzz |
| 1920 |
*/ |
| 1921 |
|
| 1922 |
for ( i = 0; i < ssize; i++ ) |
| 1923 |
{ |
| 1924 |
if ( mt->div_out[i] > s->num[i] ) |
| 1925 |
break; |
| 1926 |
else if ( mt->div_out[i] < s->num[i] ) |
| 1927 |
{ |
| 1928 |
/* |
| 1929 |
* Fetch next digit of f for the next iteration, remember the school. |
| 1930 |
*/ |
| 1931 |
mt->div_out[tcnt] = (char) ( ( tcnt < tend) ? f->num[tcnt] : '0' ); |
| 1932 |
tcnt++; |
| 1933 |
r->exp--; |
| 1934 |
break; |
| 1935 |
} |
| 1936 |
} |
| 1937 |
|
| 1938 |
/* |
| 1939 |
* Situation: s->num[0..ssize-1] contains the divisor, and the array |
| 1940 |
* div_out[tstart==0..tcnt-1] hold the (first part of the) dividend. The |
| 1941 |
* array f->num[tcnt..tend-1] (which may be empty) holds the last |
| 1942 |
* part of the dividend. |
| 1943 |
* |
| 1944 |
* We compute (the first part of) div_out : s |
| 1945 |
* |
| 1946 |
* Iterate through each digit of div_out, fetching the next digit from |
| 1947 |
* f if available. |
| 1948 |
*/ |
| 1949 |
for ( outp = 0; outp < ccns+1 && !finished; outp++ ) |
| 1950 |
{ |
| 1951 |
/* |
| 1952 |
* Assume 0 as the result for the next digit. We may increment it below |
| 1953 |
* some times. |
| 1954 |
*/ |
| 1955 |
r->num[outp] = '0'; |
| 1956 |
if ( ( tcnt - tstart > ssize ) && ( mt->div_out[tstart] == '0' ) ) |
| 1957 |
tstart++; |
| 1958 |
|
| 1959 |
/* |
| 1960 |
* Stop the iteration if this is integer division, and we have hit the |
| 1961 |
* decimal point. |
| 1962 |
*/ |
| 1963 |
if ( ( type != DIVTYPE_NORMAL ) && ( outp >= r->exp ) ) |
| 1964 |
{ |
| 1965 |
finished = 1 ; |
| 1966 |
continue ; |
| 1967 |
} |
| 1968 |
|
| 1969 |
/* |
| 1970 |
* Try to subtract as many times as possible, that is, compute the |
| 1971 |
* next digit of the result. Our example in the second step: |
| 1972 |
* 12 345 : 23 = 0 zzz (before iteration) |
| 1973 |
* 123 45 : 23 = 05 zz (first iteration) |
| 1974 |
* 00 84 5 : 23 = 053 z (84 contains 3 times 23) |
| 1975 |
*/ |
| 1976 |
for ( cont = 1; cont; ) |
| 1977 |
{ |
| 1978 |
/* |
| 1979 |
* If the current operation works on equal sized numbers (e.g. |
| 1980 |
* second iteration), we have to compare if we can do the next |
| 1981 |
* subtraction. This isn't necessary if (tcnt-tstart) > ssize, which |
| 1982 |
* means the partial dividend (123 in first iteration) is longer |
| 1983 |
* than the divisor (23, only two chars). xx always is smaller than |
| 1984 |
* yyy if they don't start with 0. |
| 1985 |
*/ |
| 1986 |
if ( tcnt - tstart == ssize ) |
| 1987 |
{ |
| 1988 |
for ( i = 0; i < ssize; i++ ) |
| 1989 |
{ |
| 1990 |
test = mt->div_out[tstart + i] - s->num[i]; |
| 1991 |
if ( test < 0 ) |
| 1992 |
cont = 0; |
| 1993 |
if ( test != 0 ) |
| 1994 |
break; |
| 1995 |
} |
| 1996 |
} |
| 1997 |
|
| 1998 |
/* |
| 1999 |
* If we can continue, subtract it. |
| 2000 |
*/ |
| 2001 |
loan = 0; |
| 2002 |
if ( cont ) |
| 2003 |
{ |
| 2004 |
r->num[outp]++; |
| 2005 |
for ( i = 0; i < ssize; i++ ) |
| 2006 |
{ |
| 2007 |
char h = (char) ( s->num[ssize-1-i] - '0' + loan ); |
| 2008 |
mt->div_out[tcnt-1-i] = (char) ( mt->div_out[tcnt-1-i] - h ); |
| 2009 |
if ( ( loan = (mt->div_out[tcnt-1-i] < '0' ) ) != 0 ) |
| 2010 |
mt->div_out[tcnt-1-i] += 10; |
| 2011 |
} |
| 2012 |
if ( loan ) |
| 2013 |
{ |
| 2014 |
/* |
| 2015 |
* decrement it and check for '0' |
| 2016 |
*/ |
| 2017 |
mt->div_out[tstart] -= 1; |
| 2018 |
if ( ( tcnt - tstart > ssize ) && |
| 2019 |
( mt->div_out[tstart] == '0' ) ) |
| 2020 |
tstart++; |
| 2021 |
} |
| 2022 |
|
| 2023 |
} |
| 2024 |
} /* for each possible subtraction */ |
| 2025 |
|
| 2026 |
if ( ( tcnt - tstart > ssize ) && ( mt->div_out[tstart] == '0' ) ) |
| 2027 |
tstart++; |
| 2028 |
|
| 2029 |
/* |
| 2030 |
* Do we have anything left of the dividend? This is only meaningful if |
| 2031 |
* all digits in the original divident have been processed, it is |
| 2032 |
* also safe to assume that divident and divisor have equal sizes. |
| 2033 |
*/ |
| 2034 |
|
| 2035 |
assert( tcnt-tstart == ssize ); |
| 2036 |
mt->div_out[tcnt] = (char) ( ( tcnt < tend ) ? f->num[tcnt] : '0' ); |
| 2037 |
if ( ++tcnt > tend ) |
| 2038 |
{ |
| 2039 |
finished = 1; |
| 2040 |
for ( i = tstart; i < tcnt; i++ ) |
| 2041 |
{ |
| 2042 |
if ( mt->div_out[i] != '0' ) |
| 2043 |
{ |
| 2044 |
finished = 0; |
| 2045 |
break; |
| 2046 |
} |
| 2047 |
} |
| 2048 |
} |
| 2049 |
|
| 2050 |
} /* for each digit wanted in the result */ |
| 2051 |
|
| 2052 |
if ( type != DIVTYPE_NORMAL ) |
| 2053 |
{ |
| 2054 |
/* |
| 2055 |
* fixes bug 687399 |
| 2056 |
* |
| 2057 |
* Perform a validity check. We may got a remainder bigger than |
| 2058 |
* the residual. It indicates a rounded integer part value. |
| 2059 |
* The residual in div_out[tstart..tcnt-1] counted from div_out[0] is |
| 2060 |
* f->exp based. |
| 2061 |
* Find the first non-zero in the residiual and continue then. |
| 2062 |
*/ |
| 2063 |
finished = 1; |
| 2064 |
test = MIN( MAX( tend, tcnt ) - tstart, ccns + 1 ); |
| 2065 |
for ( i = 0; i < test; i++ ) |
| 2066 |
{ |
| 2067 |
char h; |
| 2068 |
h = (char) ( ( i < tcnt - tstart ) ? mt->div_out[tstart+i] : |
| 2069 |
f->num[tstart+i] ); |
| 2070 |
if ( h != '0' ) |
| 2071 |
break; |
| 2072 |
} |
| 2073 |
|
| 2074 |
/* |
| 2075 |
* s begins withs a non-zero as the digit at tstart+i. Only compare the |
| 2076 |
* numbers if the residual may be greater than s. |
| 2077 |
*/ |
| 2078 |
if ( ( f->exp - tstart - i >= s->exp ) && ( i < test ) ) |
| 2079 |
{ |
| 2080 |
if ( f->exp - tstart - i > s->exp ) |
| 2081 |
{ |
| 2082 |
/* |
| 2083 |
* The residual has a higher exponent. We have definitely an error. |
| 2084 |
*/ |
| 2085 |
finished = 0; |
| 2086 |
} |
| 2087 |
else |
| 2088 |
{ |
| 2089 |
/* |
| 2090 |
* This fits many situations. The exponent is the same, we have |
| 2091 |
* to compare the digits of the number. |
| 2092 |
*/ |
| 2093 |
int j; |
| 2094 |
|
| 2095 |
test = MIN( test - i, ssize ); |
| 2096 |
for ( j = 0; j < test; j++, i++ ) |
| 2097 |
{ |
| 2098 |
int h; |
| 2099 |
h = ( i < tcnt - tstart ) ? mt->div_out[tstart+i] : |
| 2100 |
f->num[tstart+i]; |
| 2101 |
h -= s->num[j]; |
| 2102 |
if ( h > 0 ) |
| 2103 |
finished = 0; |
| 2104 |
if ( h != 0 ) |
| 2105 |
break; |
| 2106 |
} |
| 2107 |
/* |
| 2108 |
* We still can have an error. Imagine a residual of 22 and a |
| 2109 |
* divisor of 2e1. |
| 2110 |
*/ |
| 2111 |
if ( ( j >= test ) && ( ssize > test ) && ( test > 0 ) ) |
| 2112 |
finished = 0; |
| 2113 |
} |
| 2114 |
} |
| 2115 |
|
| 2116 |
/* |
| 2117 |
* We perform the operation with DIGITS+1 precision for a later |
| 2118 |
* rounding and to prevent math errors. We have to check if rounding |
| 2119 |
* would occur later. |
| 2120 |
*/ |
| 2121 |
if ( finished ) |
| 2122 |
{ |
| 2123 |
if ( ( outp > ccns ) && ( r->num[ccns] != '0' ) ) |
| 2124 |
finished = 0; |
| 2125 |
/* |
| 2126 |
* FGC: I'm not sure whether the following test supersedes the |
| 2127 |
* the testing of the mantissa above. It should, but who can |
| 2128 |
* prove? |
| 2129 |
*/ |
| 2130 |
r->size = outp; |
| 2131 |
if ( !test_whole( TSD, r, 0 ) ) |
| 2132 |
finished = 0; |
| 2133 |
} |
| 2134 |
if ( !finished ) |
| 2135 |
{ |
| 2136 |
volatile char *fs, *ss; |
| 2137 |
streng *h; |
| 2138 |
|
| 2139 |
h = name_of_node( TSD, NULL, f ); |
| 2140 |
fs = tmpstr_of( TSD, h ); |
| 2141 |
Free_stringTSD( h ); |
| 2142 |
h = name_of_node( TSD, NULL, s ); |
| 2143 |
ss = tmpstr_of( TSD, h ); |
| 2144 |
Free_stringTSD( h ); |
| 2145 |
exiterror( ERR_INVALID_INTEGER, |
| 2146 |
( type == DIVTYPE_REMAINDER ) ? 12 : 11, |
| 2147 |
fs, ss, ccns); |
| 2148 |
} |
| 2149 |
} |
| 2150 |
|
| 2151 |
origexp = f->exp; |
| 2152 |
origneg = f->negative; |
| 2153 |
|
| 2154 |
if ( type == DIVTYPE_BOTH ) |
| 2155 |
{ |
| 2156 |
/* |
| 2157 |
* Return both answers |
| 2158 |
*/ |
| 2159 |
IS_AT_LEAST( r2->num, r2->max, outp ); |
| 2160 |
|
| 2161 |
memcpy( r2->num, r->num, outp ); |
| 2162 |
r2->negative = r->negative; |
| 2163 |
r2->size = r->size; |
| 2164 |
r2->exp = r->exp; |
| 2165 |
|
| 2166 |
for ( r2->size = outp; ( r2->size > r2->exp ) && ( r2->size > 1 ); |
| 2167 |
r2->size-- ) |
| 2168 |
{ |
| 2169 |
if ( r2->num[r2->size-1] != '0' ) |
| 2170 |
break; |
| 2171 |
} |
| 2172 |
} |
| 2173 |
|
| 2174 |
if ( ( type == DIVTYPE_REMAINDER ) || ( type == DIVTYPE_BOTH ) ) |
| 2175 |
{ |
| 2176 |
/* |
| 2177 |
* We are really interested in the remainder, so swap things |
| 2178 |
*/ |
| 2179 |
for ( i = 0; i < MIN( MAX( tend, tcnt ) - tstart, ccns + 1 ); i++ ) |
| 2180 |
r->num[i] = (char) ( ( i < tcnt - tstart ) ? mt->div_out[tstart+i] : |
| 2181 |
f->num[tstart+i] ); |
| 2182 |
|
| 2183 |
r->size = outp = i; |
| 2184 |
r->exp = origexp - tstart; |
| 2185 |
r->negative = origneg; |
| 2186 |
} |
| 2187 |
|
| 2188 |
/* |
| 2189 |
* Then, at the end, we have to strip of trailing zeros that come |
| 2190 |
* after the decimal point, first do we have any decimals? |
| 2191 |
*/ |
| 2192 |
for ( r->size = outp; ( r->size > r->exp ) && ( r->size > 1 ); r->size-- ) |
| 2193 |
{ |
| 2194 |
if ( r->num[r->size - 1] != '0' ) |
| 2195 |
break; |
| 2196 |
} |
| 2197 |
/* free memory to fix memory leak; Bug #455 */ |
| 2198 |
if (mt->div_out) |
| 2199 |
{ |
| 2200 |
FreeTSD(mt->div_out); |
| 2201 |
mt->div_out = NULL; |
| 2202 |
mt->div_outsize = 0; |
| 2203 |
} |
| 2204 |
} |
| 2205 |
|
| 2206 |
void string_div( tsd_t *TSD, const num_descr *f, const num_descr *s, |
| 2207 |
num_descr *r, num_descr *r2, int type, cnodeptr left, |
| 2208 |
cnodeptr right ) |
| 2209 |
{ |
| 2210 |
int ccns = TSD->currlevel->currnumsize; |
| 2211 |
|
| 2212 |
LOSTDIGITS_CHECK( f, ccns, left ); |
| 2213 |
LOSTDIGITS_CHECK( s, ccns, right ); |
| 2214 |
|
| 2215 |
string_div2( TSD, f, s, r, r2, type, ccns ); |
| 2216 |
|
| 2217 |
if ( r != NULL ) |
| 2218 |
r->used_digits = ccns; |
| 2219 |
if ( r2 != NULL ) |
| 2220 |
r2->used_digits = ccns; |
| 2221 |
} |
| 2222 |
|
| 2223 |
/* The multiplication table for two single-digits numbers */ |
| 2224 |
static const char mult[10][10][3] = { |
| 2225 |
{ "00", "00", "00", "00", "00", "00", "00", "00", "00", "00" }, |
| 2226 |
{ "00", "01", "02", "03", "04", "05", "06", "07", "08", "09" }, |
| 2227 |
{ "00", "02", "04", "06", "08", "10", "12", "14", "16", "18" }, |
| 2228 |
{ "00", "03", "06", "09", "12", "15", "18", "21", "24", "27" }, |
| 2229 |
{ "00", "04", "08", "12", "16", "20", "24", "28", "32", "36" }, |
| 2230 |
{ "00", "05", "10", "15", "20", "25", "30", "35", "40", "45" }, |
| 2231 |
{ "00", "06", "12", "18", "24", "30", "36", "42", "48", "54" }, |
| 2232 |
{ "00", "07", "14", "21", "28", "35", "42", "49", "56", "63" }, |
| 2233 |
{ "00", "08", "16", "24", "32", "40", "48", "56", "64", "72" }, |
| 2234 |
{ "00", "09", "18", "27", "36", "45", "54", "63", "72", "81" }, |
| 2235 |
} ; |
| 2236 |
|
| 2237 |
|
| 2238 |
static void string_mul2( tsd_t *TSD, const num_descr *f, const num_descr *s, |
| 2239 |
num_descr *r, int ccns ) |
| 2240 |
{ |
| 2241 |
char *outp; |
| 2242 |
const char *answer; |
| 2243 |
int i,sskip,fskip,sstart,fstart,base,offset,carry,j; |
| 2244 |
mat_tsd_t *mt; |
| 2245 |
|
| 2246 |
mt = (mat_tsd_t *)TSD->mat_tsd; |
| 2247 |
|
| 2248 |
IS_AT_LEAST( mt->mul_out, mt->mul_outsize, 2*(ccns+1) ) ; |
| 2249 |
#ifdef TRACEMEM |
| 2250 |
mt->outptr4 = mt->mul_out ; |
| 2251 |
#endif |
| 2252 |
|
| 2253 |
for (i=0; i<2*(ccns+1); mt->mul_out[i++]='0') ; |
| 2254 |
outp = &mt->mul_out[2*(ccns+1)-1] ; |
| 2255 |
|
| 2256 |
for (sskip=0; (sskip<s->size) && (s->num[sskip]=='0'); sskip++ ) ; |
| 2257 |
sstart = MIN( sskip+ccns, s->size-1 ) ; |
| 2258 |
|
| 2259 |
for (fskip=0; (fskip<f->size) && (f->num[fskip]=='0'); fskip++ ) ; |
| 2260 |
fstart = MIN( fskip+ccns, f->size-1 ) ; |
| 2261 |
|
| 2262 |
base = 2*(ccns+1)-1 ; |
| 2263 |
offset = carry = 0 ; |
| 2264 |
/* |
| 2265 |
* Use a maximum of DIGITS+1 significant digits on input for each operand. |
| 2266 |
*/ |
| 2267 |
for ( i=sstart; i>=sskip; i-- ) |
| 2268 |
{ |
| 2269 |
offset = carry = 0 ; |
| 2270 |
assert( base >= 0 ) ; |
| 2271 |
for ( j=fstart; j>=fskip; j-- ) |
| 2272 |
{ |
| 2273 |
answer = mult[f->num[j]-'0'][s->num[i]-'0'] ; |
| 2274 |
assert( base-offset >= 0 ) ; |
| 2275 |
/* Stupid MSVC likes this only: */ |
| 2276 |
mt->mul_out[base-offset] = (char) (mt->mul_out[base-offset] + |
| 2277 |
answer[1] - '0' + carry) ; |
| 2278 |
carry = answer[0] - '0' ; |
| 2279 |
while ( mt->mul_out[base-offset] > '9' ) |
| 2280 |
{ |
| 2281 |
mt->mul_out[base-offset] -= 10 ; |
| 2282 |
carry++ ; |
| 2283 |
} |
| 2284 |
offset++ ; |
| 2285 |
} |
| 2286 |
if (base-offset >= 0) |
| 2287 |
mt->mul_out[base-offset++] = (char) (carry + '0') ; |
| 2288 |
else |
| 2289 |
exiterror( ERR_INTERPRETER_FAILURE, 1, __FILE__, __LINE__, "" ) ; |
| 2290 |
|
| 2291 |
base-- ; |
| 2292 |
} |
| 2293 |
|
| 2294 |
IS_AT_LEAST( r->num, r->max, /*2*(ccns+1)*/ |
| 2295 |
outp - mt->mul_out-base+offset ) ; |
| 2296 |
j = 0 ; |
| 2297 |
for (i=base-offset+2; (i<=outp - mt->mul_out); i++ ) |
| 2298 |
r->num[j++] = mt->mul_out[i] ; |
| 2299 |
|
| 2300 |
if (j==0) |
| 2301 |
{ |
| 2302 |
r->num[j++] = '0' ; |
| 2303 |
r->exp = 1 ; |
| 2304 |
} |
| 2305 |
else |
| 2306 |
r->exp = s->exp + f->exp ; |
| 2307 |
|
| 2308 |
r->negative = log_xor( f->negative, s->negative ) ; |
| 2309 |
r->size = j ; |
| 2310 |
str_round( r, ccns ) ; |
| 2311 |
/* free memory to fix memory leak; Bug #455 */ |
| 2312 |
if (mt->mul_out) |
| 2313 |
{ |
| 2314 |
FreeTSD(mt->mul_out); |
| 2315 |
mt->mul_out = NULL; |
| 2316 |
mt->mul_outsize = 0; |
| 2317 |
} |
| 2318 |
} |
| 2319 |
|
| 2320 |
void string_mul( tsd_t *TSD, const num_descr *f, const num_descr *s, |
| 2321 |
num_descr *r, cnodeptr left, cnodeptr right ) |
| 2322 |
{ |
| 2323 |
int ccns = TSD->currlevel->currnumsize; |
| 2324 |
|
| 2325 |
LOSTDIGITS_CHECK( f, ccns, left ); |
| 2326 |
LOSTDIGITS_CHECK( s, ccns, right ); |
| 2327 |
|
| 2328 |
string_mul2( TSD, f, s, r, ccns ); |
| 2329 |
|
| 2330 |
r->used_digits = ccns; |
| 2331 |
} |
| 2332 |
|
| 2333 |
static void descr_strip( const tsd_t *TSD, const num_descr *from, num_descr *to ) |
| 2334 |
{ |
| 2335 |
int i=0, j=0 ; |
| 2336 |
|
| 2337 |
IS_AT_LEAST( to->num, to->max, TSD->currlevel->currnumsize+1 ) ; |
| 2338 |
|
| 2339 |
to->negative = from->negative ; |
| 2340 |
for (i=0; (i<from->size) && (from->num[i]=='0'); i++ ) ; |
| 2341 |
to->exp = from->exp - i ; |
| 2342 |
for (j=0; j+i<from->size; j++ ) |
| 2343 |
to->num[j] = from->num[i+j] ; |
| 2344 |
|
| 2345 |
if ((to->exp-1 > MAX_EXPONENT) || ( -MAX_EXPONENT > to->exp+1)) |
| 2346 |
exiterror( ERR_ARITH_OVERFLOW, 0 ) ; |
| 2347 |
|
| 2348 |
to->size = j ; |
| 2349 |
to->used_digits = from->used_digits; |
| 2350 |
} |
| 2351 |
|
| 2352 |
|
| 2353 |
|
| 2354 |
void string_pow( tsd_t *TSD, const num_descr *num, num_descr *acc, |
| 2355 |
num_descr *res, cnodeptr lname, cnodeptr rname ) |
| 2356 |
{ |
| 2357 |
static const num_descr one = { "1", 0, 1, 1, 2, -1 } ; |
| 2358 |
int ineg=0, pow, cnt,power ; |
| 2359 |
int ccns = TSD->currlevel->currnumsize; |
| 2360 |
|
| 2361 |
IS_AT_LEAST( res->num, res->max, ccns+1 ) ; |
| 2362 |
|
| 2363 |
LOSTDIGITS_CHECK( num, ccns, lname ); |
| 2364 |
LOSTDIGITS_CHECK( acc, ccns, rname ); |
| 2365 |
power = descr_to_int( TSD, acc, ERR_INVALID_INTEGER, 8, NULL, 0 ) ; |
| 2366 |
|
| 2367 |
IS_AT_LEAST( acc->num, acc->max, ccns+1 ) ; |
| 2368 |
acc->exp = 1 ; |
| 2369 |
acc->size = 1 ; |
| 2370 |
acc->negative = 0 ; |
| 2371 |
acc->num[0] = '1' ; |
| 2372 |
|
| 2373 |
if (power < 0) |
| 2374 |
{ |
| 2375 |
power = -power ; |
| 2376 |
ineg = 1 ; |
| 2377 |
} |
| 2378 |
pow = power; |
| 2379 |
|
| 2380 |
for (cnt=0; pow; cnt++ ) |
| 2381 |
pow = pow>>1 ; |
| 2382 |
|
| 2383 |
for ( ;cnt ; ) |
| 2384 |
{ |
| 2385 |
if (power & (1<<(cnt-1))) |
| 2386 |
{ |
| 2387 |
/* multiply acc with *f, and put answer into acc */ |
| 2388 |
string_mul2( TSD, acc, num, res, ccns ) ; |
| 2389 |
assert( acc->size <= acc->max && res->size <= res->max ) ; |
| 2390 |
descr_strip( TSD, res, acc ) ; |
| 2391 |
assert( acc->size <= acc->max && res->size <= res->max ) ; |
| 2392 |
} |
| 2393 |
|
| 2394 |
if ((--cnt)==0) |
| 2395 |
break ; /* horrible example of dataflow */ |
| 2396 |
|
| 2397 |
/* then, square the contents of acc */ |
| 2398 |
string_mul2( TSD, acc, acc, res, ccns ) ; |
| 2399 |
assert( acc->size <= acc->max && res->size <= res->max ) ; |
| 2400 |
descr_strip( TSD, res, acc ) ; |
| 2401 |
assert( acc->size <= acc->max && res->size <= res->max ) ; |
| 2402 |
} |
| 2403 |
|
| 2404 |
if (ineg) |
| 2405 |
/* may hang if acc==zero ? */ |
| 2406 |
string_div2( TSD, &one, acc, res, NULL, DIVTYPE_NORMAL, ccns ) ; |
| 2407 |
else |
| 2408 |
descr_strip( TSD, acc, res ) ; |
| 2409 |
assert( acc->size <= acc->max && res->size <= res->max ) ; |
| 2410 |
acc->used_digits = ccns; |
| 2411 |
} |
| 2412 |
|
| 2413 |
|
| 2414 |
/* ========= interface routines to the arithmetic routines ========== */ |
| 2415 |
|
| 2416 |
int descr_sign( const void *descr ) |
| 2417 |
{ |
| 2418 |
return( ((num_descr*)descr)->negative ? -1 : 1 ) ; |
| 2419 |
} |
| 2420 |
|
| 2421 |
|
| 2422 |
void free_a_descr( const tsd_t *TSD, num_descr *in ) |
| 2423 |
{ |
| 2424 |
assert( in->size <= in->max ) ; |
| 2425 |
|
| 2426 |
if ( in->num ) |
| 2427 |
FreeTSD( in->num ) ; |
| 2428 |
|
| 2429 |
FreeTSD( in ) ; |
| 2430 |
} |
| 2431 |
|
| 2432 |
|
| 2433 |
num_descr *get_a_descr( tsd_t *TSD, const char *bif, int argno, |
| 2434 |
const streng *num ) |
| 2435 |
{ |
| 2436 |
mat_tsd_t *mt; |
| 2437 |
num_descr *descr=NULL ; |
| 2438 |
int i; |
| 2439 |
descr = (num_descr *)MallocTSD( sizeof(num_descr) ) ; |
| 2440 |
descr->max = 0 ; |
| 2441 |
descr->num = NULL ; |
| 2442 |
|
| 2443 |
if ( ( i = getdescr( TSD, num, descr ) ) != 0 ) |
| 2444 |
{ |
| 2445 |
mt = (mat_tsd_t *)TSD->mat_tsd; |
| 2446 |
|
| 2447 |
FreeTSD( descr ); |
| 2448 |
if ( bif == NULL ) |
| 2449 |
exiterror( ERR_BAD_ARITHMETIC, 0 ); |
| 2450 |
|
| 2451 |
if ( i == 9 ) |
| 2452 |
exiterror( ERR_INCORRECT_CALL, i, bif, argno, mt->max_exponent_len, tmpstr_of( TSD, num ) ); |
| 2453 |
else |
| 2454 |
exiterror( ERR_INCORRECT_CALL, i, bif, argno, tmpstr_of( TSD, num ) ); |
| 2455 |
} |
| 2456 |
|
| 2457 |
return descr ; |
| 2458 |
} |
| 2459 |
|
| 2460 |
|
| 2461 |
int str_true( const tsd_t *TSD, const streng *input ) |
| 2462 |
{ |
| 2463 |
if (input->len != 1) |
| 2464 |
exiterror( ERR_UNLOGICAL_VALUE, 0 ) ; |
| 2465 |
|
| 2466 |
switch (input->value[0]) |
| 2467 |
{ |
| 2468 |
case '1': |
| 2469 |
return 1 ; |
| 2470 |
case '0': |
| 2471 |
return 0 ; |
| 2472 |
default: |
| 2473 |
exiterror( ERR_UNLOGICAL_VALUE, 0 ) ; |
| 2474 |
} |
| 2475 |
|
| 2476 |
/* Too keep the compiler happy */ |
| 2477 |
return 1 ; |
| 2478 |
} |
| 2479 |
|
| 2480 |
|
| 2481 |
streng *str_abs( tsd_t *TSD, const streng *input ) |
| 2482 |
{ |
| 2483 |
mat_tsd_t *mt; |
| 2484 |
streng *retval; |
| 2485 |
int i; |
| 2486 |
|
| 2487 |
mt = (mat_tsd_t *)TSD->mat_tsd; |
| 2488 |
if ( ( i = getdescr( TSD, input, &mt->fdescr ) ) != 0 ) |
| 2489 |
{ |
| 2490 |
if ( i == 9 ) |
| 2491 |
exiterror( ERR_INCORRECT_CALL, i, "ABS", 1, mt->max_exponent_len, tmpstr_of( TSD, input ) ); |
| 2492 |
else |
| 2493 |
exiterror( ERR_INCORRECT_CALL, i, "ABS", 1, tmpstr_of( TSD, input ) ); |
| 2494 |
} |
| 2495 |
|
| 2496 |
if ( get_options_flag( TSD->currlevel, EXT_STRICT_ANSI ) ) |
| 2497 |
{ |
| 2498 |
str_round_lostdigits( TSD, &mt->fdescr, TSD->currlevel->currnumsize ); |
| 2499 |
mt->fdescr.negative = 0; |
| 2500 |
return str_norm( TSD, &mt->fdescr, NULL ); |
| 2501 |
} |
| 2502 |
|
| 2503 |
mt->fdescr.negative = 0; |
| 2504 |
mt->fdescr.used_digits = mt->fdescr.size; |
| 2505 |
retval = str_norm( TSD, &mt->fdescr, NULL ); |
| 2506 |
return retval; |
| 2507 |
} |
| 2508 |
|
| 2509 |
|
| 2510 |
streng *str_sign( tsd_t *TSD, const streng *input ) |
| 2511 |
{ |
| 2512 |
mat_tsd_t *mt; |
| 2513 |
char *mant; |
| 2514 |
int i; |
| 2515 |
|
| 2516 |
mt = (mat_tsd_t *)TSD->mat_tsd; |
| 2517 |
if ( ( i = getdescr( TSD, input, &mt->fdescr ) ) != 0 ) |
| 2518 |
{ |
| 2519 |
if ( i == 9 ) |
| 2520 |
exiterror( ERR_INCORRECT_CALL, i, "SIGN", 1, mt->max_exponent_len, tmpstr_of( TSD, input ) ); |
| 2521 |
else |
| 2522 |
exiterror( ERR_INCORRECT_CALL, i, "SIGN", 1, tmpstr_of( TSD, input ) ); |
| 2523 |
} |
| 2524 |
|
| 2525 |
if ( get_options_flag( TSD->currlevel, EXT_STRICT_ANSI ) ) |
| 2526 |
{ |
| 2527 |
str_round_lostdigits( TSD, &mt->fdescr, TSD->currlevel->currnumsize ); |
| 2528 |
} |
| 2529 |
|
| 2530 |
mant = mt->fdescr.num; |
| 2531 |
for ( i = 0; i < mt->fdescr.size; i++ ) |
| 2532 |
{ |
| 2533 |
if ( mant[i] != '0' ) |
| 2534 |
{ |
| 2535 |
if ( mt->fdescr.negative ) |
| 2536 |
{ |
| 2537 |
return Str_creTSD( "-1" ); |
| 2538 |
} |
| 2539 |
else |
| 2540 |
{ |
| 2541 |
return Str_creTSD( "1" ); |
| 2542 |
} |
| 2543 |
} |
| 2544 |
} |
| 2545 |
return Str_creTSD( "0" ); |
| 2546 |
} |
| 2547 |
|
| 2548 |
|
| 2549 |
streng *str_trunc( tsd_t *TSD, const streng *number, int deci ) |
| 2550 |
{ |
| 2551 |
int i=0, j=0, k=0, size=0, top=0 ; |
| 2552 |
streng *result=NULL ; |
| 2553 |
mat_tsd_t *mt; |
| 2554 |
|
| 2555 |
mt = (mat_tsd_t *)TSD->mat_tsd; |
| 2556 |
|
| 2557 |
/* first, convert number to internal representation */ |
| 2558 |
if ( ( i = getdescr( TSD, number, &mt->fdescr ) ) != 0 ) |
| 2559 |
{ |
| 2560 |
if ( i == 9 ) |
| 2561 |
exiterror( ERR_INCORRECT_CALL, i, "TRUNC", 1, mt->max_exponent_len, tmpstr_of( TSD, number ) ); |
| 2562 |
else |
| 2563 |
exiterror( ERR_INCORRECT_CALL, i, "TRUNC", 1, tmpstr_of( TSD, number ) ); |
| 2564 |
} |
| 2565 |
|
| 2566 |
/* get rid of possible excessive precision */ |
| 2567 |
if ( get_options_flag( TSD->currlevel, EXT_STRICT_ANSI ) ) |
| 2568 |
{ |
| 2569 |
str_round_lostdigits( TSD, &mt->fdescr, TSD->currlevel->currnumsize ); |
| 2570 |
} |
| 2571 |
|
| 2572 |
/* who big must the result string be? */ |
| 2573 |
if ((i=mt->fdescr.exp) > 0 ) |
| 2574 |
size = mt->fdescr.exp + deci ; |
| 2575 |
else |
| 2576 |
size = deci ; |
| 2577 |
|
| 2578 |
/* |
| 2579 |
* Adrian Sutherland <[email protected]> |
| 2580 |
* Changed the following line from '+ 2' to '+ 3', |
| 2581 |
* because I was getting core dumps ... I think that we need this |
| 2582 |
* because negative numbers BIGGER THAN -1 need a sign, a zero and |
| 2583 |
* a decimal point ... A. |
| 2584 |
*/ |
| 2585 |
result = Str_makeTSD( size + 3 ) ; /* allow for sign and decimal point */ |
| 2586 |
j = 0 ; |
| 2587 |
|
| 2588 |
if (mt->fdescr.negative) |
| 2589 |
result->value[j++] = '-' ; |
| 2590 |
|
| 2591 |
/* first fill in the known numerals of the integer part */ |
| 2592 |
top = MIN( mt->fdescr.exp, mt->fdescr.size ) ; |
| 2593 |
for (i=0; i<top; i++) |
| 2594 |
result->value[j++] = mt->fdescr.num[i] ; |
| 2595 |
|
| 2596 |
/* pad out with '0' in the integer part, if necessary */ |
| 2597 |
for (k=i; k<mt->fdescr.exp; k++) |
| 2598 |
result->value[j++] = '0' ; |
| 2599 |
|
| 2600 |
if (k==0) |
| 2601 |
result->value[j++] = '0' ; |
| 2602 |
|
| 2603 |
k = 0 ; |
| 2604 |
if (deci>0) |
| 2605 |
{ |
| 2606 |
result->value[j++] = '.' ; |
| 2607 |
|
| 2608 |
/* pad with zeros between decimal point and number */ |
| 2609 |
for (k=0; k>mt->fdescr.exp; k--) |
| 2610 |
result->value[j++] = '0' ; |
| 2611 |
} |
| 2612 |
|
| 2613 |
/* fill in with the decimals, if any */ |
| 2614 |
top = MIN( mt->fdescr.size-mt->fdescr.exp, deci ) + i + k ; |
| 2615 |
for (; i<top; i++ ) |
| 2616 |
result->value[j++] = mt->fdescr.num[i] ; |
| 2617 |
|
| 2618 |
/* pad with zeros if necessary */ |
| 2619 |
for (; i<deci+MIN(mt->fdescr.exp,mt->fdescr.size); i++ ) |
| 2620 |
result->value[j++] = '0' ; |
| 2621 |
|
| 2622 |
result->len = j ; |
| 2623 |
assert( (result->len <= result->max) && (result->len<=size+2) ) ; |
| 2624 |
|
| 2625 |
return( result ) ; |
| 2626 |
} |
| 2627 |
|
| 2628 |
|
| 2629 |
|
| 2630 |
/* ------------------------------------------------------------------ |
| 2631 |
* This function converts a packed binary string to a decimal integer. |
| 2632 |
* It is equivalent of interpreting the binary string as a number of |
| 2633 |
* base 256, and converting it to base 10 (the actual algorithm uses |
| 2634 |
* a number of base 2, padded to a multiple of 8 digits). Negative |
| 2635 |
* numbers are interpreted as two's complement. |
| 2636 |
* |
| 2637 |
* First parameter is the packed binary string; second parameter is |
| 2638 |
* the number of initial characters to skip (i.e. the position of the |
| 2639 |
* most significant byte in 'input'; the third parameter is a boolean |
| 2640 |
* telling if this number is signed or not. |
| 2641 |
* |
| 2642 |
* The significance of the 'too_large' variable: If the number has |
| 2643 |
* leading zeros, that is not an error, so the 'fdescr' might be set |
| 2644 |
* to values larger than it can hold. However, the error occurs only |
| 2645 |
* if that value is used. Therefore, if 'fdescr' becomes bigger than |
| 2646 |
* the max whole number, 'too_large' is set. If attempts are made to |
| 2647 |
* use 'fdescr' while 'too_large' is set, an error occurs. |
| 2648 |
* |
| 2649 |
* Note that this algoritm requires that string_mul and string_add |
| 2650 |
* does not change anything in their first two parameters. |
| 2651 |
* |
| 2652 |
* The 'input' variable is assumed to have at least one digit, so don't |
| 2653 |
* call this function with a null string. Maybe the compiler could |
| 2654 |
* optimize this function better if [esf]descr were locals? |
| 2655 |
* |
| 2656 |
* In case of errors we throw SYNTAX(40,35). |
| 2657 |
*/ |
| 2658 |
|
| 2659 |
streng *str_digitize( tsd_t *TSD, streng *input, int start, int sign, |
| 2660 |
const char *bif, int removeStringOnError ) |
| 2661 |
{ |
| 2662 |
int cur_byte=0 ; /* current byte in 'input' */ |
| 2663 |
int cur_bit=0 ; /* current bit in 'input' */ |
| 2664 |
int too_large=0 ; /* error flag (see above) */ |
| 2665 |
int i, ccns; |
| 2666 |
int user_ccns = TSD->currlevel->currnumsize; |
| 2667 |
mat_tsd_t *mt; |
| 2668 |
|
| 2669 |
mt = (mat_tsd_t *)TSD->mat_tsd; |
| 2670 |
|
| 2671 |
/* do we have anything to work on? */ |
| 2672 |
assert( start < Str_len(input) ); |
| 2673 |
|
| 2674 |
ccns = 3 * Str_len(input); |
| 2675 |
|
| 2676 |
/* ensure that temporary number descriptors has enough space */ |
| 2677 |
IS_AT_LEAST( mt->fdescr.num, mt->fdescr.max, ccns+2 ) ; |
| 2678 |
IS_AT_LEAST( mt->edescr.num, mt->edescr.max, ccns+2 ) ; |
| 2679 |
IS_AT_LEAST( mt->sdescr.num, mt->sdescr.max, ccns+2 ) ; |
| 2680 |
|
| 2681 |
/* |
| 2682 |
* Initialize the temporary number descriptors: 'fdescr', 'sdescr' |
| 2683 |
* and 'edescr'. They will be initialized to 0, 1 and 2 respectively. |
| 2684 |
* They are used for: |
| 2685 |
* |
| 2686 |
* fdescr: contains the value of the current bit of the current |
| 2687 |
* byte, e.g the third last bit in the last byte will |
| 2688 |
* have the value '0100'b (=4). This value is multiplied |
| 2689 |
* with two at each iteration of the inner loop. Is |
| 2690 |
* initialized to the value '1', and will have the same |
| 2691 |
* sign as 'input'. |
| 2692 |
* |
| 2693 |
* sdescr: contains '2', to make doubling of 'fdescr' easy |
| 2694 |
* |
| 2695 |
* edescr: contains the answer, initially set to '0' if 'input' |
| 2696 |
* is positive, or '-1' if 'input' is negative. The |
| 2697 |
* descriptor 'fdescr' is added to (or implicitly |
| 2698 |
* subtracted from) this number. |
| 2699 |
*/ |
| 2700 |
mt->fdescr.size = mt->sdescr.size = mt->edescr.size = 1 ; |
| 2701 |
mt->fdescr.negative = mt->sdescr.negative = mt->edescr.negative = 0 ; |
| 2702 |
mt->fdescr.exp = mt->sdescr.exp = mt->edescr.exp = 1 ; |
| 2703 |
|
| 2704 |
mt->edescr.num[0] = '0' ; /* the resulting number */ |
| 2705 |
mt->fdescr.num[0] = '1' ; /* the value of each binary digit */ |
| 2706 |
mt->sdescr.num[0] = '2' ; /* the number to multiply 'fdescr' in */ |
| 2707 |
|
| 2708 |
/* |
| 2709 |
* If 'input' is signed, but positive, treat as if it was unsigned. |
| 2710 |
* 'sign' is then effectively a boolean stating whether 'input' is |
| 2711 |
* a negative number. In that case, 'edescr' should be set to '-1'. |
| 2712 |
* Also, 'fdescr' is set to negative, so that it is subtracted from |
| 2713 |
* 'edescr' when given to string_add(). |
| 2714 |
*/ |
| 2715 |
if (sign) |
| 2716 |
{ |
| 2717 |
if (input->value[start] & 0x80) |
| 2718 |
{ |
| 2719 |
mt->edescr.num[0] = '1' ; |
| 2720 |
mt->edescr.negative = 1 ; |
| 2721 |
mt->fdescr.negative = 1 ; |
| 2722 |
} |
| 2723 |
else |
| 2724 |
sign = 0 ; |
| 2725 |
} |
| 2726 |
|
| 2727 |
/* |
| 2728 |
* Each iteration of the outer loop will process a byte in 'input', |
| 2729 |
* starting with the last (least significant) byte. Each iteration |
| 2730 |
* of the inner loop will process one bit in the byte currently |
| 2731 |
* processed by the outer loop. |
| 2732 |
*/ |
| 2733 |
for (cur_byte=Str_len(input)-1; cur_byte>=start; cur_byte--) |
| 2734 |
{ |
| 2735 |
for (cur_bit=0; cur_bit<8; cur_bit++) |
| 2736 |
{ |
| 2737 |
/* |
| 2738 |
* does the precision hold? if not, set flag |
| 2739 |
* The error can be considered to be a severe error. We should |
| 2740 |
* always have "enough" precision. See ccns above. |
| 2741 |
*/ |
| 2742 |
if (mt->fdescr.size > ccns) |
| 2743 |
too_large = 1 ; |
| 2744 |
|
| 2745 |
/* |
| 2746 |
* If the current bit (the j'th bit in the i'th byte) is set |
| 2747 |
* and input is positive; or if current bit is not set and |
| 2748 |
* input is negative, then increase the value of the result. |
| 2749 |
* This is not really a bitwise xor, but a logical xor, but |
| 2750 |
* the values are always 1 or 0, so it doesn't matter. |
| 2751 |
*/ |
| 2752 |
if ((sign) ^ ((input->value[cur_byte] >> cur_bit) & 1)) |
| 2753 |
{ |
| 2754 |
if (too_large) |
| 2755 |
exiterror( ERR_INVALID_INTEGER, 0 ) ; |
| 2756 |
|
| 2757 |
string_add2( TSD, &mt->edescr, &mt->fdescr, &mt->edescr, ccns ); |
| 2758 |
} |
| 2759 |
|
| 2760 |
/* |
| 2761 |
* Str_ip away any leading zeros. If this is not done, the |
| 2762 |
* accuracy of the operation will deter, since string_add() |
| 2763 |
* return answer with leading zero, and the accumulative |
| 2764 |
* effect of this would make 'edescr' zero after a few |
| 2765 |
* iterations of the loop. |
| 2766 |
*/ |
| 2767 |
str_strip( &mt->edescr ) ; |
| 2768 |
|
| 2769 |
/* |
| 2770 |
* Increase the value of 'fdescr', so that it corresponds with |
| 2771 |
* the significance of the current bit in 'input'. But don't |
| 2772 |
* do this if 'fdescr' isn't capable of holding that number. |
| 2773 |
*/ |
| 2774 |
if (!too_large) |
| 2775 |
{ |
| 2776 |
string_mul2( TSD, &mt->fdescr, &mt->sdescr, &mt->fdescr, ccns ); |
| 2777 |
str_strip( &mt->fdescr ) ; |
| 2778 |
} |
| 2779 |
} |
| 2780 |
} |
| 2781 |
|
| 2782 |
/* |
| 2783 |
* normalize answer and return to caller. Always show all digits if we |
| 2784 |
* don't have to support STRICT_ANSI. In ANSI we have to throw a SYNTAX |
| 2785 |
* in case of number overflow. |
| 2786 |
*/ |
| 2787 |
if ( get_options_flag( TSD->currlevel, EXT_STRICT_ANSI ) ) |
| 2788 |
{ |
| 2789 |
for (i = 0; i < mt->edescr.size; i++) |
| 2790 |
{ |
| 2791 |
if (mt->edescr.num[i] != '0') |
| 2792 |
break; |
| 2793 |
} |
| 2794 |
if (i < mt->edescr.size) |
| 2795 |
{ |
| 2796 |
/* not a 0 */ |
| 2797 |
if (mt->edescr.exp - i > user_ccns) |
| 2798 |
{ |
| 2799 |
volatile char *msg; |
| 2800 |
|
| 2801 |
if ( removeStringOnError ) |
| 2802 |
Free_stringTSD( input ); |
| 2803 |
mt->edescr.used_digits = mt->edescr.size; |
| 2804 |
input = str_norm( TSD, &mt->edescr, NULL ); |
| 2805 |
msg = tmpstr_of( TSD, input ); |
| 2806 |
Free_stringTSD( input ); |
| 2807 |
/* fixes bug 1112956 */ |
| 2808 |
exiterror( ERR_INCORRECT_CALL, 35, bif, msg ); |
| 2809 |
} |
| 2810 |
} |
| 2811 |
mt->edescr.used_digits = user_ccns; |
| 2812 |
} |
| 2813 |
else |
| 2814 |
{ |
| 2815 |
int s,e; |
| 2816 |
|
| 2817 |
for ( s = 0; s < mt->edescr.size; s++ ) |
| 2818 |
{ |
| 2819 |
if ( mt->edescr.num[s] != '0' ) |
| 2820 |
break; |
| 2821 |
} |
| 2822 |
for ( e = mt->edescr.size - 1; e > s; e-- ) |
| 2823 |
{ |
| 2824 |
if ( mt->edescr.num[e] != '0' ) |
| 2825 |
break; |
| 2826 |
} |
| 2827 |
e -= s - 1; |
| 2828 |
if ( e < 1 ) |
| 2829 |
e = 1; |
| 2830 |
mt->edescr.used_digits = ( e < user_ccns ) ? user_ccns : e; |
| 2831 |
} |
| 2832 |
return str_norm( TSD, &mt->edescr, NULL ); |
| 2833 |
} |
| 2834 |
|
| 2835 |
streng *str_binerize( tsd_t *TSD, num_descr *num, int length, int errnum, int suberrnum, const char *bif, int argno ) |
| 2836 |
{ |
| 2837 |
int i,ccns; |
| 2838 |
streng *result; |
| 2839 |
|
| 2840 |
/* |
| 2841 |
* We are going to need two number in this algoritm, so we can |
| 2842 |
* just as well make them right away. We could initialize these on |
| 2843 |
* the first invocation of this routine, and thereby saving some |
| 2844 |
* space, but that would 1) take CPU on every invocation; 2) it |
| 2845 |
* would probably cost just as much space in the text segment. |
| 2846 |
* (Would have to set NUMERIC DIGIT to at least 4 before calling |
| 2847 |
* getdescr with these.) |
| 2848 |
*/ |
| 2849 |
static const num_descr minus_one = { "1", 1, 1, 1, 2, -1 } ; |
| 2850 |
static const num_descr byte = { "256", 0, 3, 3, 4, -1 } ; |
| 2851 |
|
| 2852 |
mat_tsd_t *mt; |
| 2853 |
|
| 2854 |
mt = (mat_tsd_t *)TSD->mat_tsd; |
| 2855 |
|
| 2856 |
ccns = ( num->exp < 3 ) ? 3 : num->exp; |
| 2857 |
|
| 2858 |
assert( num == &mt->edescr ); |
| 2859 |
|
| 2860 |
/* |
| 2861 |
* If the length is zero, a special case applies, the return value |
| 2862 |
* is a nullstring. |
| 2863 |
*/ |
| 2864 |
if ( length == 0 ) |
| 2865 |
result = nullstringptr(); |
| 2866 |
|
| 2867 |
/* |
| 2868 |
* Here comes the real work. To ease the implementation it is |
| 2869 |
* divided into two parts based on whether or not length is |
| 2870 |
* specified. |
| 2871 |
*/ |
| 2872 |
else if ( length == -1 ) |
| 2873 |
{ |
| 2874 |
/* |
| 2875 |
* First, let's estimate the size of the output string that |
| 2876 |
* we need. A crude (over)estimate is one char for every second |
| 2877 |
* decimal digits. Also set length, just to chache the value. |
| 2878 |
* (btw: isn't that MAX( ,0) unneeded? Since number don't have |
| 2879 |
* a decimal part, and since it must have a integer part (else |
| 2880 |
* it would be zero, and then trapped above.) |
| 2881 |
*/ |
| 2882 |
assert( num->exp > 0 ); |
| 2883 |
result = Str_makeTSD( ( length = ( MAX( num->exp, 0 ) ) / 2 ) + 1 ); |
| 2884 |
|
| 2885 |
/* |
| 2886 |
* Let's loop from the least significant part of edescr. For each |
| 2887 |
* iteration we divide num by 256, stopping when edescr is |
| 2888 |
* zero. |
| 2889 |
*/ |
| 2890 |
for ( i = length; ; i-- ) |
| 2891 |
{ |
| 2892 |
/* |
| 2893 |
* Perform the integer divition, edescr gets the quotient, |
| 2894 |
* while fdescr get the remainder. Afterwards, perform some |
| 2895 |
* makeup on the numbers (that might not be needed?) |
| 2896 |
*/ |
| 2897 |
string_div2( TSD, num, &byte, &mt->fdescr, num, DIVTYPE_BOTH, ccns ); |
| 2898 |
str_strip( num ); |
| 2899 |
str_strip( &mt->fdescr ); |
| 2900 |
|
| 2901 |
/* |
| 2902 |
* Now, fdescr has the remainder, stuff it into the result string |
| 2903 |
* before it escapes :-) (don't we have to cast lvalue here?) |
| 2904 |
* Afterwards, check to see if there are more digits to extract. |
| 2905 |
*/ |
| 2906 |
result->value[i] = (char) descr_to_int( TSD, &mt->fdescr, errnum, suberrnum, bif, argno ); |
| 2907 |
if ( ( num->num[0] == '0' ) && ( num->size == 1 ) ) |
| 2908 |
break; |
| 2909 |
} |
| 2910 |
|
| 2911 |
/* |
| 2912 |
* That's it, now we just have to align the answer and set the |
| 2913 |
* correct length. Have to use memmove() since strings may |
| 2914 |
* overlap. |
| 2915 |
*/ |
| 2916 |
memmove( result->value, &result->value[i], length + 1 - i ); |
| 2917 |
result->len = length + 1 - i; |
| 2918 |
} |
| 2919 |
else |
| 2920 |
{ |
| 2921 |
/* |
| 2922 |
* We do have a specified length for the number. At least that |
| 2923 |
* makes it easy to deside how large the result string should be. |
| 2924 |
*/ |
| 2925 |
result = Str_makeTSD( length ); |
| 2926 |
|
| 2927 |
/* |
| 2928 |
* In the loop, iterate once for each divition of 256, but stop |
| 2929 |
* only when we have reached the start of the result string. |
| 2930 |
* Below, edescr gets the quotient and fdescr gets the remainder. |
| 2931 |
*/ |
| 2932 |
for ( i = length - 1; i >= 0; i-- ) |
| 2933 |
{ |
| 2934 |
/* may hang if acc==zero ? */ |
| 2935 |
string_div2( TSD, num, &byte, &mt->fdescr, num, DIVTYPE_BOTH, ccns ); |
| 2936 |
str_strip( num ); |
| 2937 |
str_strip( &mt->fdescr ); |
| 2938 |
|
| 2939 |
/* |
| 2940 |
* If the remainder is negative (i.e. quotient is negative too) |
| 2941 |
* then add 256 to the remainder, to bring it into the range of |
| 2942 |
* an unsigned char. To compensate for that, subtract one from |
| 2943 |
* the quotient. Store the remainder. |
| 2944 |
*/ |
| 2945 |
if ( mt->fdescr.negative ) |
| 2946 |
{ |
| 2947 |
/* the following two lines are not needed, but it does not |
| 2948 |
work without them. */ |
| 2949 |
if ( ( num->size == 1 ) && ( num->num[0] == '0' ) ) |
| 2950 |
num->exp = 1; |
| 2951 |
|
| 2952 |
string_add2( TSD, num, &minus_one, num, ccns ); |
| 2953 |
str_strip( num ); |
| 2954 |
string_add2( TSD, &mt->fdescr, &byte, &mt->fdescr, ccns ); |
| 2955 |
} |
| 2956 |
result->value[i] = (char) descr_to_int( TSD, &mt->fdescr, errnum, suberrnum, bif, argno ); |
| 2957 |
} |
| 2958 |
/* |
| 2959 |
* That's it, store the length |
| 2960 |
*/ |
| 2961 |
result->len = length; |
| 2962 |
} |
| 2963 |
|
| 2964 |
/* |
| 2965 |
* We're finished ... hope it works ... |
| 2966 |
*/ |
| 2967 |
return result; |
| 2968 |
} |
| 2969 |
|
| 2970 |
|
| 2971 |
streng *str_normalize( const tsd_t *TSD, const streng *number ) |
| 2972 |
{ |
| 2973 |
mat_tsd_t *mt; |
| 2974 |
int err; |
| 2975 |
|
| 2976 |
mt = (mat_tsd_t *)TSD->mat_tsd; |
| 2977 |
if ( ( err = getdescr( TSD, number, &mt->fdescr ) ) != 0 ) |
| 2978 |
exiterror( ERR_BAD_ARITHMETIC, 0 ) ; |
| 2979 |
|
| 2980 |
return str_norm( TSD, &mt->fdescr, NULL ) ; |
| 2981 |
} |
| 2982 |
|
| 2983 |
|
| 2984 |
|
| 2985 |
num_descr *is_a_descr( const tsd_t *TSD, const streng *number ) |
| 2986 |
{ |
| 2987 |
num_descr *newnum=NULL ; |
| 2988 |
mat_tsd_t *mt; |
| 2989 |
|
| 2990 |
mt = (mat_tsd_t *)TSD->mat_tsd; |
| 2991 |
|
| 2992 |
if ( getdescr( TSD, number, &mt->fdescr ) != 0 ) |
| 2993 |
return NULL ; |
| 2994 |
|
| 2995 |
newnum = (num_descr *)MallocTSD( sizeof( num_descr )) ; |
| 2996 |
newnum->max = 0 ; |
| 2997 |
newnum->num = NULL ; |
| 2998 |
|
| 2999 |
descr_copy( TSD, &mt->fdescr, newnum ) ; |
| 3000 |
return newnum ; |
| 3001 |
} |
| 3002 |
|
| 3003 |
/* |
| 3004 |
* ANSI chapter 7, beginning: "...matches that syntax and also has a value |
| 3005 |
* that is 'whole', that is has no non-zero fractional part." The syntax |
| 3006 |
* is that of a plain number. |
| 3007 |
* Thus, 1E1 or 1.00 are allowed. |
| 3008 |
* This function returns 1 if number is a valid whole number, 0 else. |
| 3009 |
* |
| 3010 |
* The value is loaded into mat_tsd_t.edescr. A pointer to this is |
| 3011 |
* returned in *num. |
| 3012 |
* |
| 3013 |
* Added 08.03.2005 (tt.mm.yyyy), FGC: Due to some misinterpretation by my own |
| 3014 |
* this routine must not round, instead it must check for ANSI WHOLENUM. |
| 3015 |
* This means that the number must be representable without loss in the |
| 3016 |
* interval [-(10**digits()-1), 10**digits()-1]. |
| 3017 |
* noDigitsLimit raises digits() virtually to endless. Don't use it in |
| 3018 |
* ANSI compatible environments. |
| 3019 |
*/ |
| 3020 |
int myiswnumber( tsd_t *TSD, const streng *number, num_descr **num, |
| 3021 |
int noDigitsLimit ) |
| 3022 |
{ |
| 3023 |
num_descr *input; |
| 3024 |
mat_tsd_t *mt; |
| 3025 |
|
| 3026 |
mt = (mat_tsd_t *)TSD->mat_tsd; |
| 3027 |
|
| 3028 |
if ( getdescr( TSD, number, &mt->edescr ) ) |
| 3029 |
return 0; |
| 3030 |
input = &mt->edescr; |
| 3031 |
if ( num != NULL ) |
| 3032 |
*num = input; |
| 3033 |
|
| 3034 |
return test_whole( TSD, input, noDigitsLimit ); |
| 3035 |
} |
| 3036 |
|
| 3037 |
|
| 3038 |
/* |
| 3039 |
* Converts number to an integer. Sets *error to 1 on error (0 otherwise) |
| 3040 |
* |
| 3041 |
* ANSI chapter 7, beginning: "...matches that syntax and also has a value |
| 3042 |
* that is 'whole', that is has no non-zero fractional part." The syntax |
| 3043 |
* is that of a plain number. |
| 3044 |
* Thus, 1E1 or 1.00 are allowed. |
| 3045 |
*/ |
| 3046 |
int streng_to_int( const tsd_t *TSD, const streng *number, int *error ) |
| 3047 |
{ |
| 3048 |
int result = 0; |
| 3049 |
mat_tsd_t *mt; |
| 3050 |
|
| 3051 |
mt = (mat_tsd_t *)TSD->mat_tsd; |
| 3052 |
|
| 3053 |
if ( ( *error = getdescr( TSD, number, &mt->fdescr ) ) != 0 ) |
| 3054 |
return 0; |
| 3055 |
|
| 3056 |
if ( ( *error = !whole_number( &mt->fdescr, &result ) ) != 0 ) |
| 3057 |
return 0; |
| 3058 |
|
| 3059 |
return result; |
| 3060 |
} |
| 3061 |
|
| 3062 |
/* |
| 3063 |
* Converts number to a 64bit integer. Sets *error to 1 on error (0 otherwise) |
| 3064 |
* |
| 3065 |
* ANSI chapter 7, beginning: "...matches that syntax and also has a value |
| 3066 |
* that is 'whole', that is has no non-zero fractional part." The syntax |
| 3067 |
* is that of a plain number. |
| 3068 |
* Thus, 1E1 or 1.00 are allowed. |
| 3069 |
*/ |
| 3070 |
rx_64 streng_to_rx64( const tsd_t *TSD, const streng *number, int *error ) |
| 3071 |
{ |
| 3072 |
rx_64 result = 0; |
| 3073 |
mat_tsd_t *mt; |
| 3074 |
|
| 3075 |
mt = (mat_tsd_t *)TSD->mat_tsd; |
| 3076 |
|
| 3077 |
if ( ( *error = getdescr( TSD, number, &mt->fdescr ) ) != 0 ) |
| 3078 |
return 0; |
| 3079 |
|
| 3080 |
if ( ( *error = !whole_rx64_number( &mt->fdescr, &result ) ) != 0 ) |
| 3081 |
return 0; |
| 3082 |
|
| 3083 |
return result; |
| 3084 |
} |
| 3085 |
|
| 3086 |
int myisnumber( const tsd_t *TSD, const streng *string ) |
| 3087 |
{ |
| 3088 |
mat_tsd_t *mt; |
| 3089 |
|
| 3090 |
mt = (mat_tsd_t *)TSD->mat_tsd; |
| 3091 |
|
| 3092 |
return getdescr( TSD, string, &mt->edescr ) == 0; |
| 3093 |
} |
| 3094 |
|