1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
/*
Numdiff - compare putatively similar files,
ignoring small numeric differences
Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Ivano Primi <ivprimi@libero.it>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include<stdlib.h>
#include<ctype.h> /* for isspace() */
#include<assert.h>
#include"number.h"
#include"numdiff.h"
#ifdef _DMALLOC_
#include <dmalloc.h> /* Useful only for debugging */
#endif
#ifdef BC_A2NUM_DEBUG
#define move_ahead(ptr) out_char_stderr(*ptr), ptr++
#define set(endptr,str) *endptr = (char*)str, fprintf (stderr, "tail: \"%s\"\n", *endptr)
#else
#define move_ahead(ptr) ptr++
#define set(endptr,str) *endptr = (char*)str
#endif
static
bc_num bc_10_raised_to (long expn, int add_d_dig)
{
bc_num pw;
if (expn >= 0)
{
pw = bc_new_num (1 + expn, 0);
pw->n_value[0] = 1;
}
else
{
pw = bc_new_num (1, -expn + add_d_dig);
pw->n_value[-expn] = 1;
}
#ifdef BC_10_DEBUG
fprintf (stderr, "10^%ld = ", expn);
pn_stderr (pw);
#endif
return pw;
}
static
int bc_a2num (bc_num *num, const char *str, char **endptr, int scale,
const struct numfmt* pnf)
{
char *ptr, *nptr;
int digits, strscale;
long expn;
char zero_int;
size_t length;
#ifdef BC_A2NUM_DEBUG
fprintf (stderr, "\n\"%s\"\n", str);
#endif
/* Check for valid number and count digits. */
ptr = (char*)str;
digits = 0;
strscale = 0;
zero_int = FALSE;
length = strlen(pnf->currency);
if ( (*ptr == pnf->pos_sign) || (*ptr == pnf->neg_sign) )
move_ahead(ptr); /* Sign */
if ( length > 0 && strncmp (ptr, pnf->currency, length) == 0 )
ptr += length; /* Skip the currency name, if specified */
if (pnf->grouping > 0)
{
while ( (is_digit((int)*ptr)) || *ptr == pnf->thsep )
{
int first_sep = 1;
if (*ptr == pnf->thsep)
{
unsigned i;
char* ptr2;
if ((first_sep))
{
/* We have to check that before the separator there is */
/* at least one digit but no more than 'pnf->grouping' */
for (i=0, ptr2 = ptr;
ptr2 > str && (is_digit ((int)*(ptr2-1)));
i++, ptr2--);
if (i == 0 || i > pnf->grouping)
{
if ((endptr))
set(endptr,str);
return -1;
}
first_sep = 0;
}
/* We have to check that after the separator */
/* there are exactly 'pnf->grouping' digits */
for (i = 0, ptr2 = ptr + 1; (is_digit((int)*ptr2));
i++, ptr2++);
if (i != pnf->grouping)
{
if ((endptr))
set(endptr,str);
return -1;
}
ptr++;
}
else /* We have just found a new digit */
move_ahead(ptr), digits++;
}
}
else /* pnf->grouping == 0 */
{
while ( (is_digit((int)*ptr)) )
move_ahead(ptr), digits++; /* digits */
}
/* OLD CODE */
/* while ( (is_digit((int)*ptr)) ) */
/* ptr++, digits++; */
if (*ptr == pnf->dp)
move_ahead(ptr); /* decimal point */
while (is_digit((int)*ptr))
move_ahead(ptr), strscale++; /* digits */
if (digits+strscale == 0)
{
if ((endptr))
set(endptr,str);
*num = bc_copy_num (_zero_);
return -1;
}
if (TOLOWER(*ptr) == TOLOWER(pnf->ech) && !is_space (*(ptr+1)))
{
char *tail;
expn = strtol (ptr + 1, &tail, 10);
if (expn < MIN_EXPN && tail != ptr + 1)
{
fprintf (stderr, _("%s: a number with a too small exponent has been found,\nnamely \"%s\".\n"), PACKAGE, str);
fprintf (stderr, _("Exponents smaller than %ld are not accepted,\n"), MIN_EXPN);
fprintf (stderr, _("the execution of the program ends now\n"));
exit (EXIT_TROUBLE);
}
if (expn > MAX_EXPN && tail != ptr + 1)
{
fprintf (stderr, _("%s: a number with a too large exponent has been found,\nnamely \"%s\".\n"), PACKAGE, str);
fprintf (stderr, _("Exponents larger than %ld are not accepted,\n"), MAX_EXPN);
fprintf (stderr, _("the execution of the program ends now\n"));
exit (EXIT_TROUBLE);
}
if ((endptr))
*endptr = tail != ptr + 1 ? tail : ptr;
}
else
{
expn = 0;
if ((endptr))
*endptr = ptr;
}
#ifdef BC_A2NUM_DEBUG
fprintf (stderr, "%c%ld\n", pnf->ech, expn);
#endif
/* Adjust numbers and allocate storage and initialize fields. */
strscale = MIN(strscale, scale);
if (digits == 0)
{
zero_int = TRUE;
digits = 1;
}
*num = bc_new_num (digits, strscale);
/* Build the whole number. */
ptr = (char*)str;
if (*ptr == pnf->neg_sign)
{
(*num)->n_sign = MINUS;
ptr++;
}
else
{
(*num)->n_sign = PLUS;
if (*ptr == pnf->pos_sign) ptr++;
}
if ( length > 0 && strncmp (ptr, pnf->currency, length) == 0 )
ptr += length; /* Skip the currency name, if specified */
nptr = (*num)->n_value;
if ((zero_int))
{
*nptr++ = 0;
digits = 0;
}
for (; digits > 0; ptr++)
{
/*
We have to take into account the
presence of the thousands separator !
*/
if (*ptr != pnf->thsep)
{
/* *ptr is a digit! */
*nptr++ = CH_VAL(*ptr);
digits--;
}
}
/*
OLD CODE (to build the integer part)
for (;digits > 0; digits--)
*nptr++ = CH_VAL(*ptr++);
*/
/* Build the fractional part. */
if (strscale > 0)
{
ptr++; /* skip the decimal point! */
for (;strscale > 0; strscale--)
*nptr++ = CH_VAL(*ptr++);
}
/* Take into account the exponent */
if((expn))
{
bc_num Factor, Prod;
bc_init_num (&Prod);
Factor = expn > 0 ?
bc_10_raised_to (expn, 0) : bc_10_raised_to (expn, scale);
bc_multiply (*num, Factor, &Prod, strscale);
*num = bc_copy_num (Prod);
bc_free_num (&Factor);
bc_free_num (&Prod);
}
#ifdef BC_A2NUM_DEBUG
pn_stderr (*num);
if((endptr))
fprintf (stderr, "tail: \"%s\"\n", *endptr);
#endif
return 0;
}
#define DIM 50
static
void bc_print_num (bc_num num, void (* out_char)(int), int prec)
{
/* The negative sign if needed. */
if (num->n_sign == MINUS) (*out_char) ('-');
/* Output the number. */
if (num->n_value[0] == 10)
{
(*out_char) ('I');
(*out_char) ('n');
(*out_char) ('f');
}
else if (bc_is_zero (num))
{
(*out_char) ('0');
(*out_char) ('.');
for (; prec > 0; prec--)
(*out_char) ('0');
}
else
{
char *nptr;
int index, ndig;
signed char dig_list[DIM], *buffer, *p;
long expn;
assert ((buffer = calloc (prec + 1, sizeof (signed char))));
nptr = num->n_value;
#ifdef __DEBUG__
pv ("nptr", nptr, num->n_len + num->n_scale);
(*out_char)('\n');
#endif
for (expn = num->n_len; expn > 0 && !*nptr; nptr++, expn--);
/* Now expn == number of non-null digits */
/* in the integer part of the number */
if (expn == 0)
{
/* Now nptr points to the first digit */
/* of the fractional part. */
for (index = num->n_scale, expn = -1;
!*nptr;
index--, expn--, nptr++);
/* Now expn is the exponent of the number */
/* in base 10. */
}
else
{
expn--;
/* Now expn is the exponent of the number */
/* in base 10. */
for (index = num->n_len + num->n_scale, nptr = num->n_value;
!*nptr; index--, nptr++);
}
/* First we print the mantissa */
for (p = buffer, ndig = prec + 1;
ndig > 0 && index > 0;
ndig--, index--, p++, nptr++)
*p = *nptr;
if (ndig == 0 && index > 0)
{
/* Rounding */
if (*nptr >= 5)
{
for (--p; p > buffer && *p == 9; *p = 0, p--);
if (p == buffer)
{
if (*p == 9)
{
*p = 1;
expn++;
}
else
(*p)++;
}
else
(*p)++;
}
}
(*out_char)(BCD_CHAR(*buffer));
(*out_char)('.');
for (index = 1; index <= prec; index++)
(*out_char)(BCD_CHAR(buffer[index]));
free((void*)buffer);
/* Now it is the moment to print the exponent... */
(*out_char)('e');
if (expn < 0)
{
(*out_char) ('-');
expn = -expn;
}
else
(*out_char) ('+');
for (index = 0; index < DIM; dig_list[index] = -1, index++);
index = 0;
do
{
dig_list[index] = expn % 10;
expn /= 10;
index++;
} while ((expn));
for (index = 0; dig_list[index] >= 0; index++);
for (--index; index >= 0; index--)
(*out_char)(BCD_CHAR(dig_list[index]));
/* .. done ! */
} /* End of num != 0 */
}
|